File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/popular-posts.tar
class-popular-posts-helper.php 0000644 00000003102 15154212037 0012461 0 ustar 00 <?php
/**
* This is the helper class for the Popular Posts output functionality.
* We will call this class or its methods when necessary.
*
* @package MonsterInsights
*/
/**
* Class MonsterInsights_Popular_Posts_Helper
* We will create helper methods as static.
*/
class MonsterInsights_Popular_Posts_Helper {
/**
* Store self object.
*/
private static $instance;
/**
* Instentiate the class.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Class constructor.
*/
public function __construct() {
add_action( 'transition_post_status', array( $this, 'transition_post_status' ), 10, 2 );
}
/**
* Here we are clearing cache for popular post.
*/
private function clear_cache() {
// Delete Popular Posts cache.
MonsterInsights_Popular_Posts_Inline()->get_cache()->delete_data();
MonsterInsights_Popular_Posts_Widget()->get_cache()->delete_data();
if ( monsterinsights_is_pro_version() ) {
MonsterInsights_Popular_Posts_Products()->get_cache()->delete_data();
}
}
/**
* Trigger when post status is changed.
*/
public function transition_post_status( $new_status, $old_status ) {
// A post goes to any status from published.
if ( $old_status == 'publish' && $new_status != 'publish' ) {
$this->clear_cache();
}
// A post is being to published.
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$this->clear_cache();
}
}
}
// Initiate here so that we can run hooks.
MonsterInsights_Popular_Posts_Helper::get_instance();
class-popular-posts-themes.php 0000644 00000060357 15154212037 0012506 0 ustar 00 <?php
/**
* Popular posts theme-specific functionality.
*
* @package MonsterInsights
*/
/**
* Class MonsterInsights_Popular_Posts_Themes
*/
class MonsterInsights_Popular_Posts_Themes {
/**
* The type of widget to load themes for (inline, widget, products).
*
* @var string
*/
public $type;
/**
* Holds the array of themes specific to the current type loaded.
*
* @var array
*/
public $themes = array();
/**
* Holds the data for the currently selected theme (if indicated in the constructor).
*
* @var array
*/
public $theme = array();
/**
* The current theme instance with styles from settings applied.
*
* @var array
*/
public $styled_theme;
/**
* The theme options key used to store specific theme styles.
*
* @var string
*/
private $themes_styles_key = 'monsterinsights_popular_posts_theme_settings';
/**
* Variable for the theme settings.
*
* @var array
*/
private $themes_styles;
/**
* MonsterInsights_Popular_Posts_Themes constructor.
*
* @param string $type The type of widget: inline/widget/products.
* @param string $theme The current theme to load details for.
*/
public function __construct( $type = 'inline', $theme = '' ) {
$this->type = $type;
if ( method_exists( $this, 'get_themes_' . $type ) ) {
$this->themes = call_user_func( array( $this, 'get_themes_' . $type ) );
if ( ! empty( $theme ) ) {
$this->theme = isset( $this->themes[ $theme ] ) ? $this->themes[ $theme ] : array();
return $this->theme;
} else {
return $this->themes;
}
}
return false;
}
/**
* Get the current theme details with the option to load properties already styled.
*
* @return array|mixed
*/
public function get_theme() {
return $this->theme;
}
/**
* Get the currently loaded themes for the widget type.
*
* @return array|mixed
*/
public function get_themes() {
return $this->themes;
}
public function get_theme_stored_styles() {
if ( ! isset( $this->themes_styles ) ) {
$this->themes_styles = get_option( $this->themes_styles_key, array() );
}
return $this->themes_styles;
}
/**
* Go through the themes and apply styles from the stored settings.
*
* @var string $type The instance type: inline/widget/products.
* @var array $themes The themes to process/apply styles for.
*/
public function process_themes_styles( $type, $themes ) {
$settings = $this->get_theme_stored_styles();
if ( ! empty( $settings[ $type ] ) ) {
foreach ( $themes as $theme_key => $theme_values ) {
if ( ! empty( $settings[ $type ][ $theme_key ] ) ) {
foreach ( $themes[ $theme_key ]['styles'] as $object => $props ) {
if ( ! empty( $settings[ $type ][ $theme_key ][ $object ] ) ) {
foreach ( $props as $style_key => $style_value ) {
if ( ! empty( $settings[ $type ][ $theme_key ][ $object ][ $style_key ] ) ) {
$themes[ $theme_key ]['styles'][ $object ][ $style_key ] = $settings[ $type ][ $theme_key ][ $object ][ $style_key ];
}
}
}
}
}
}
}
return $themes;
}
/**
* Get the themes for the inline widget type.
*
* @return array
*/
public function get_themes_inline() {
$themes = array(
'alpha' => array(
'label' => __( 'Alpha', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 18,
'text' => __( '15 Proven Ways to Repurpose Content on Your WordPress Site', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'color' => '#F0F2F4',
),
),
'level' => 'lite',
),
'beta' => array(
'label' => __( 'Beta', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 18,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'border' => '#F0F2F4',
),
'image' => 'theme-preview-beta.png',
),
'image' => true,
'level' => 'lite',
),
'charlie' => array(
'label' => __( 'Charlie', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
),
'label' => array(
'color' => '#393f4c',
'text' => __( 'Popular Stories Right now', 'google-analytics-for-wordpress' ),
),
'border' => array(
'color' => '#D3D7DE',
),
),
'list' => array(
__( '15 Proven Ways to Repurpose Content on Your WordPress Site', 'google-analytics-for-wordpress' ),
__( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
__( 'How to Set Up Online Ordering for Your Restaurant Website', 'google-analytics-for-wordpress' ),
),
'posts' => 3,
'level' => 'lite',
),
'delta' => array(
'label' => __( 'Delta', 'google-analytics-for-wordpress' ),
'styles' => array(
'icon' => array(
'color' => '#EB5757',
),
'title' => array(
'color' => '#393f4c',
'size' => 16,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'border' => '#F0F2F4',
),
),
'level' => 'plus',
),
'echo' => array(
'label' => __( 'Echo', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( '15 Proven Ways to Repurpose Content on Your WordPress Site', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending:', 'google-analytics-for-wordpress' ),
),
'background' => array(
'color' => '#F0F2F4',
),
),
'level' => 'plus',
),
'foxtrot' => array(
'label' => __( 'Foxtrot', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( 'How to Build an Email List in WordPress – Email Marketing 101', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'image' => 'theme-preview-image.jpg',
),
'image' => true,
'level' => 'plus',
),
'golf' => array(
'label' => __( 'Golf', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
'text' => __( '15 Proven Ways to Repurpose Content on Your WordPress Site', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Popular now', 'google-analytics-for-wordpress' ),
),
'border' => array(
'color' => '#EB5757',
'color2' => '#E2E4E9',
),
),
'level' => 'plus',
),
'hotel' => array(
'label' => __( 'Hotel', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'icon' => array(
'color' => '#EB5757',
),
),
'level' => 'plus',
),
'india' => array(
'label' => __( 'India', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 14,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending:', 'google-analytics-for-wordpress' ),
),
'border' => array(
'color' => '#EB5757',
),
'background' => array(
'color' => '#f0f2f4',
),
),
'level' => 'plus',
),
'juliett' => array(
'label' => __( 'Juliett', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( 'How to Build an Email List in WordPress – Email Marketing 101', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#393f4c',
'background' => '#e2e4e9',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'border' => array(
'color' => '#e2e4e9',
),
),
'image' => true,
'level' => 'plus',
),
'kilo' => array(
'label' => __( 'Kilo', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Popular now', 'google-analytics-for-wordpress' ),
),
'border' => array(
'color' => '#e2e4e9',
'color2' => '#e2e4e9',
),
),
'level' => 'plus',
),
'lima' => array(
'label' => __( 'Lima', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( '15 Proven Ways to Repurpose Content on Your WordPress Site', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#EB5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'color' => '#f0f2f4',
),
'image' => 'theme-preview-image-2.jpg',
),
'image' => true,
'level' => 'plus',
),
'mike' => array(
'label' => __( 'Mike', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
'text' => __( 'How to Build an Email List in WordPress – Email Marketing 101', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#fff',
'background' => '#f2994a',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'color' => '#f0f2f4',
),
'image' => 'theme-preview-image.jpg',
),
'image' => true,
'level' => 'plus',
),
'november' => array(
'label' => __( 'November', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
'text' => __( 'How to Use Google Trends to Boost Traffic and Sales (9 Simple Ways)', 'google-analytics-for-wordpress' ),
),
'label' => array(
'color' => '#eb5757',
'text' => __( 'Trending', 'google-analytics-for-wordpress' ),
),
'background' => array(
'border' => '#f0f2f4',
),
'icon' => array(
'background' => '#eb5757',
'color' => '#fff',
),
),
'level' => 'plus',
),
);
return $this->process_themes_styles( 'inline', $themes );
}
/**
* Get the themese for the widget instance.
*
* @return array
*/
public function get_themes_widget() {
$themes = array(
'alpha' => array(
'label' => __( 'Alpha', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 16,
),
'background' => array(
'color' => '#F0F2F4',
),
),
'list' => array(
'items' => array(
__( 'How to Set Up WordPress User Activity Tracking in 3 Easy Steps', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested)', 'google-analytics-for-wordpress' ),
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
),
),
'level' => 'lite',
),
'beta' => array(
'label' => __( 'Beta', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 16,
),
'background' => array(
'border' => '#1EC185',
),
),
'list' => array(
'items' => array(
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested)', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
),
),
'level' => 'lite',
),
'charlie' => array(
'label' => __( 'Charlie', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
),
'background' => array(
'color' => '#F0F2F4',
'border' => '#338EEF',
),
),
'list' => array(
'items' => array(
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested)', 'google-analytics-for-wordpress' ),
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
),
),
'level' => 'lite',
),
'delta' => array(
'label' => __( 'Delta', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 18,
),
'background' => array(
'border' => '#D3D7DE',
),
'meta' => array(
'color' => '#99A1B3',
'author' => 'on',
'date' => 'on',
'separator' => '●',
),
),
'list' => array(
'items' => array(
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested)', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-widget-1.jpg',
'theme-widget-2.jpg',
'theme-widget-3.jpg',
'theme-widget-4.jpg',
),
),
'image' => true,
'level' => 'pro',
),
'echo' => array(
'label' => __( 'Echo', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
),
'meta' => array(
'color' => '#99A1B3',
'size' => 12,
'author' => 'on',
'date' => 'on',
'comments' => 'on',
'separator' => 'on',
),
'comments' => array(
'color' => '#393F4C',
),
),
'list' => array(
'items' => array(
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested)', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-widget-5.jpg',
'theme-widget-5.jpg',
'theme-widget-5.jpg',
'theme-widget-5.jpg',
),
),
'image' => true,
'level' => 'pro',
),
'foxtrot' => array(
'label' => __( 'Foxtrot', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
),
'meta' => array(
'color' => '#99A1B3',
'size' => 12,
'author' => 'on',
'date' => 'on',
'comments' => 'on',
'separator' => '|',
),
'comments' => array(
'color' => '#393F4C',
),
),
'list' => array(
'items' => array(
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested) ', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
),
),
'level' => 'pro',
),
'golf' => array(
'label' => __( 'Golf', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 16,
),
'label' => array(
'color' => '#fff',
'background' => '#EB5757',
'text' => __( 'Trending:', 'google-analytics-for-wordpress' ),
'editable' => true,
),
'meta' => array(
'color' => '#99A1B3',
'size' => 12,
'author' => 'on',
'date' => 'on',
'comments' => 'on',
'separator' => '|',
),
'comments' => array(
'color' => '#393F4C',
),
),
'list' => array(
'items' => array(
__( '9 Proven Ways to Get Google to Index Your Website Right Away', 'google-analytics-for-wordpress' ),
__( '12 Best Social Media Analytics Tools for Marketers (Tried & Tested) ', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
__( 'How to Share Your Google Analytics Reports with Others (5 Easy Ways)', 'google-analytics-for-wordpress' ),
),
),
'level' => 'pro',
),
'hotel' => array(
'label' => __( 'Hotel', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#fff',
'size' => 16,
),
'meta' => array(
'color' => '#fff',
'size' => 12,
'author' => 'on',
'date' => 'on',
),
),
'list' => array(
'items' => array(
__( 'How to Allow WordPress to Upload All File Types (The Easy Way)', 'google-analytics-for-wordpress' ),
__( '14 Handy Google Search Operators for SEO (A Complete List)', 'google-analytics-for-wordpress' ),
__( 'How to Write Irresistible Meta Descriptions for SEO & More Clicks?', 'google-analytics-for-wordpress' ),
__( 'Uncover How Much Traffic Does a Website Get (5 Effortless Ways)', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-widget-5.jpg',
'theme-widget-6.jpg',
'theme-widget-7.jpg',
'theme-widget-8.jpg',
),
),
'image' => true,
'level' => 'pro',
),
);
return $this->process_themes_styles( 'widget', $themes );
}
/**
* Get the themes for the products widget.
*
* @return array
*/
public function get_themes_products() {
$themes = array(
'alpha' => array(
'label' => __( 'Alpha', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 16,
),
'background' => array(
'border' => '#d3d7de',
),
'price' => array(
'color' => '#393F4C',
'size' => 12,
),
'rating' => array(
'color' => '#EB5757',
),
'meta' => array(
'price' => 'on',
'rating' => 'on',
'image' => 'on',
),
),
'list' => array(
'items' => array(
__( 'WPBeginner 10-Year Anniversary Gray T-Shirt', 'google-analytics-for-wordpress' ),
__( 'WPForms Small White Logo T-Shirt', 'google-analytics-for-wordpress' ),
__( 'OptinMonster White Text Color Mascot T-Shirt', 'google-analytics-for-wordpress' ),
__( 'WPForms Make Things Simple Gray T-Shirt', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-products-1.jpg',
'theme-products-2.jpg',
'theme-products-3.jpg',
'theme-products-4.jpg',
),
'prices' => array(
'$59.99',
'$28.00',
'$65.00',
'$59.50',
),
),
),
'beta' => array(
'label' => __( 'Beta', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393F4C',
'size' => 16,
),
'background' => array(
'color' => '#F0F2F4',
),
'price' => array(
'color' => '#4C5566',
'size' => 12,
),
'rating' => array(
'color' => '#F2D74A',
),
'meta' => array(
'price' => 'on',
'rating' => 'on',
'image' => 'on',
),
),
'list' => array(
'items' => array(
__( 'Admin WPBeginner Black T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Black WP Beginner logo T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Groovy White T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Code Comment Black T-Shirt', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-products-5.jpg',
'theme-products-7.jpg',
'theme-products-6.jpg',
'theme-products-8.jpg',
),
'prices' => array(
'$29.50',
'$28.00',
'$65.00',
'$59.50',
),
),
),
'charlie' => array(
'label' => __( 'Charlie', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#fff',
'size' => 16,
),
'rating' => array(
'color' => '#F2D74A',
),
'price' => array(
'color' => '#fff',
'size' => 12,
),
'meta' => array(
'price' => 'on',
'rating' => 'on',
),
),
'list' => array(
'items' => array(
__( 'Admin WPBeginner Black T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Black WP Beginner logo T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Groovy White T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Code Comment Black T-Shirt', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-products-5.jpg',
'theme-products-7.jpg',
'theme-products-6.jpg',
'theme-products-8.jpg',
),
'prices' => array(
'$29.50',
'$28.00',
'$65.00',
'$59.50',
),
),
'image' => true,
),
'delta' => array(
'label' => __( 'Delta', 'google-analytics-for-wordpress' ),
'styles' => array(
'title' => array(
'color' => '#393f4c',
'size' => 14,
),
'rating' => array(
'color' => '#F2D74A',
),
'price' => array(
'color' => '#4C5566',
'size' => 12,
),
'meta' => array(
'price' => 'on',
'rating' => 'on',
'image' => 'on',
),
),
'list' => array(
'items' => array(
__( 'Admin WPBeginner Black T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Black WP Beginner logo T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Groovy White T-Shirt', 'google-analytics-for-wordpress' ),
__( 'Technically Awesome Code Comment Black T-Shirt', 'google-analytics-for-wordpress' ),
),
'images' => array(
'theme-products-5.jpg',
'theme-products-7.jpg',
'theme-products-6.jpg',
'theme-products-8.jpg',
),
'prices' => array(
'$29.50',
'$28.00',
'$65.00',
'$59.50',
),
),
),
);
return $this->process_themes_styles( 'products', $themes );
}
}
class-popular-posts.php 0000644 00000043300 15154212037 0011210 0 ustar 00 <?php
/**
* This is the base class for the Popular Posts output functionality.
* Each actual Popular Posts option extends this class (inline, widget, products).
*
* @package MonsterInsights
*/
/**
* Class MonsterInsights_Popular_Posts
*/
class MonsterInsights_Popular_Posts {
/**
* The key prefix used to store the settings for the magic __get method.
*
* @var string
*/
protected $settings_key;
/**
* Name of the shortcode
*
* @var string
*/
protected $shortcode_key;
/**
* The popular posts object type, by default inline, widget or products.
*
* @var string
*/
protected $type;
/**
* An array of posts used in the query process.
*
* @var array
*/
public $posts = array();
/**
* An array of posts already displayed. Used to avoid duplicate posts on the same page.
*
* @var array
*/
public $shown_posts = array();
/**
* The inline styles string with theme specifics from the Vue settings.
* Each instance should append to this variable so we print styles for all the instances in the same place.
*
* @var string
*/
public static $inline_styles = '';
/**
* Stores the option to use ajax to display the popular posts widgets on the frontend.
*
* @var string
*/
public $ajaxify;
/**
* Stores the cache instance, specific to the plugin version.
*
* @var MonsterInsights_Popular_Posts_Cache
*/
public $cache;
/**
* Holds the class object.
*
* @since 7.13.0
* @access public
* @var array
*/
public static $instances = array();
/**
* @var MonsterInsights_Popular_Posts_Themes
*/
protected $theme_props;
/**
* Indicator that inline styles have been printed to avoid duplicates.
*
* @var bool
*/
private static $styles_printed = false;
/**
* Number of posts to query from the db. Not all queried posts are used for display in the same widget.
*
* @var int
*/
public $posts_count = 15;
/**
* MonsterInsights_Popular_Posts constructor.
*/
public function __construct() {
$this->hooks();
$this->register_shortcode();
$this->ajaxify = monsterinsights_get_option( 'popular_posts_ajaxify', false );
}
/**
* Magic get for different types of popular posts.
*
* @param $name
*
* @return string|array|mixed
*/
public function __get( $name ) {
return monsterinsights_get_option( $this->settings_key . '_' . $name );
}
/**
* Add hooks needed for the output.
*/
public function hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_load_ajaxify_script' ) );
$this->add_inline_styles();
}
/**
* Add inline styles for each widget type to a single variable for printing.
*/
protected function add_inline_styles() {
if ( 'no_styles' !== $this->styling ) {
self::$inline_styles .= $this->build_inline_styles();
}
}
/**
* Should return object-specific inline styles.
*
* @return string
*/
public function build_inline_styles() {
return '';
}
/**
* Register the shortcode for the specific class.
*/
public function register_shortcode() {
if ( ! empty( $this->shortcode_key ) ) {
add_shortcode( $this->shortcode_key, array( $this, 'render_shortcode' ) );
}
}
/**
* Load the frontend styles if they are enabled.
*/
public function load_frontend_styles() {
// Only load our styles if enabled.
if ( apply_filters( 'monsterinsights_popular_posts_styles_output', 'no_styles' === $this->styling, $this ) ) {
return;
}
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
$url = apply_filters(
'monsterinsights_frontend_style_url',
plugins_url( 'assets/css/frontend' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE )
);
// Load Popular Posts styles.
wp_register_style( 'monsterinsights-editor-frontend-style', $url, array(), monsterinsights_get_asset_version() );
$this->add_theme_specific_styles();
}
/**
* If the Ajaxify option is enabled, print needed scripts.
*/
public function maybe_load_ajaxify_script() {
if ( ! $this->ajaxify ) {
return;
}
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_register_script( 'monsterinsights-popular-posts-js', plugins_url( 'assets/js/popular-posts' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version(), true );
wp_enqueue_script( 'monsterinsights-popular-posts-js' );
monsterinsights_localize_script( 'monsterinsights-popular-posts-js', 'monsterinsights_pp', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'post_id' => get_the_ID(),
'nonce' => wp_create_nonce('mi-popular-posts'),
) );
}
/**
* Add inline styles based on customizations from the vue panel.
*/
public function add_theme_specific_styles() {
if ( ! self::$styles_printed ) {
wp_add_inline_style( 'monsterinsights-editor-frontend-style', $this->get_inline_styles() );
self::$styles_printed = true;
}
}
/**
* We have a single static variable for inline styles shared by all instances so we print just once.
*
* @return string
*/
public function get_inline_styles() {
return self::$inline_styles;
}
/**
* Rendering the shortcode.
*
* @return string
*/
public function render_shortcode( $args ) {
if ( empty( $args ) || ! is_array( $args ) ) {
$args = array();
}
return apply_filters( 'monsterinsights_popular_posts_shortcode_output', $this->shortcode_output( $args ), $args, $this );
}
/**
* Output of shortcode based on settings.
*
* @param array $args Arguments from shortcode/block.
*
* @return string
*/
public function shortcode_output( $args ) {
// Load frontend.css file when shortcode is available
wp_enqueue_style( 'monsterinsights-editor-frontend-style' );
if ( $this->ajaxify ) {
return $this->get_ajax_json_data( $args );
} else {
return $this->get_rendered_html( $args );
}
}
/**
* Print inline JSON data that with settings that get processed using an AJAX call. Acts similar to printing out
* a shortcode with its settings but actually loading the output for that after the page was loaded, with AJAX.
*
* @param array $args Arguments from shortcode/block.
*
* @return string
*/
public function get_ajax_json_data( $args ) {
$args['type'] = $this->type;
$data = '<div><script type="application/json" class="monsterinsights-popular-posts-widget-json">';
$data .= wp_json_encode( $args );
$data .= '</script></div>';
return $data;
}
/**
* This is replaced with actual HTML output in child classes.
*
* @param array $args Arguments used to build specific html.
*
* @return string
*/
public function get_rendered_html( $args ) {
return '';
}
/**
* Get the cache instance for the set type.
*
* @return MonsterInsights_Popular_Posts_Cache
*/
public function get_cache() {
if ( ! isset( $this->cache ) ) {
$this->cache = new MonsterInsights_Popular_Posts_Cache( $this->type );
}
return $this->cache;
}
/**
* Use the query args to grab posts from the database.
*/
public function get_posts() {
$posts_args = $this->get_query_args();
$posts = $this->get_cache()->get_cached_posts( $posts_args );
if ( empty( $posts ) ) {
if ( isset( $posts_args['post__in'] ) && empty( $posts_args['post__in'] ) ) {
$this->posts = array();
return $this->posts;
}
$posts = get_posts( $posts_args );
$this->get_cache()->save_posts_to_cache( $posts_args, $posts );
}
$posts = $this->process_posts( $posts );
return apply_filters( 'monsterinsights_popular_posts_posts', $posts );
}
/**
* Go through posts from a WP Query and prepare them for output.
*
* @param array $posts Array of posts from WP Query or similar, also supports array of ids.
*
* @return array
*/
private function process_posts( $posts ) {
$processed_posts = array();
foreach ( $posts as $post ) {
if ( is_int( $post ) ) {
$post = get_post( $post );
}
$post_thumbnail = get_post_thumbnail_id( $post->ID );
$post_image = '';
$post_image_srcset = '';
if ( ! empty( $post_thumbnail ) ) {
$post_image = wp_get_attachment_image_src( $post_thumbnail, 'medium' );
if ( is_array( $post_image ) && ! empty( $post_image[0] ) ) {
$post_image = $post_image[0];
}
$post_image_srcset = wp_get_attachment_image_srcset( $post_thumbnail, 'medium' );
}
$author_data = get_userdata( $post->post_author );
$processed_posts[] = array(
'id' => $post->ID,
'title' => get_the_title( $post->ID ),
'link' => get_permalink( $post->ID ),
'image' => $post_image,
'srcset' => $post_image_srcset,
'image_id' => $post_thumbnail,
'author' => $post->post_author,
'author_name' => $author_data->display_name,
'date' => get_the_date( '', $post->ID ),
'comments' => get_comments_number( $post->ID ),
);
}
return $processed_posts;
}
/**
* Get the query args for grabbing the posts. This should probably get overwritten in child classes.
*
* @return mixed|void
*/
private function get_query_args() {
$args = array(
'numberposts' => 25,
'ignore_sticky_posts' => true,
'fields' => 'ids',
'orderby' => 'rand', // phpcs:ignore WordPressVIPMinimum.Performance.OrderByRand.orderby_orderby
);
$args = wp_parse_args( $this->query_args(), $args );
return apply_filters( 'monsterinsights_popular_posts_query_args', $args );
}
/**
* Set the query args specific to this instance.
*
* @return array
*/
protected function query_args() {
if ( 'comments' === $this->sort ) {
return $this->get_query_args_comments();
} elseif ( 'sharedcount' === $this->sort ) {
return $this->get_query_args_sharedcount();
} elseif ( 'curated' === $this->sort ) {
return $this->get_query_args_curated();
}
}
/**
* Get the query args for ordering by comments.
*
* @return array
*/
protected function get_query_args_comments() {
return array(
'orderby' => 'comment_count',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '-1 month',
'inclusive' => true,
),
)
);
}
/**
* Get the query args for ordering by sharedcount.
*
* @return array
*/
protected function get_query_args_sharedcount() {
$query_args = array(
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_monsterinsights_sharedcount_total',
);
return $query_args;
}
/**
* Build the query args for the curated option from the settings in the panel.
*
* @return array
*/
protected function get_query_args_curated() {
$posts = $this->curated;
$post_in = array();
if ( ! empty( $posts ) && is_array( $posts ) ) {
foreach ( $posts as $post ) {
if ( ! empty( $post['id'] ) ) {
$post_in[] = intval( $post['id'] );
}
}
}
$query_args = array(
'post__in' => $post_in,
);
return $query_args;
}
/**
* Load theme props for the specific instance.
*
* @param string $theme Theme key.
*
* @return MonsterInsights_Popular_Posts_Themes
*/
public function get_theme_props( $theme = '' ) {
if ( empty( $theme ) ) {
$theme = $this->theme;
}
$theme_props = new MonsterInsights_Popular_Posts_Themes( $this->type, $theme );
return $theme_props;
}
/**
* Marks a post as already displayed, by id.
*
* @param $id
*/
public function set_post_shown( $id ) {
if ( ! in_array( $id, $this->shown_posts, true ) ) {
$this->shown_posts[] = $id;
}
}
/**
* Returns an array of posts that were already displayed on the current page.
*
* @return array
*/
public function get_shown_posts() {
return $this->shown_posts;
}
/**
* Generic helper function to build style attributes for elements based on shortcode/block parameters.
*
* @param string $theme The theme for which we're building the style.
* @param string $object Object we're styling like title, label, background, etc.
* @param array $atts Attributes passed from shortcode/block.
* @param string $key The key of the style we're going to output.
*
* @return string
*/
public function get_element_style( $theme, $object, $atts, $key = '' ) {
if ( 'no_styles' === $this->styling ) {
// If no styles is selected don't output any styles.
return '';
}
if ( empty( $theme ) ) {
$theme = $this->theme;
}
// Find theme-specific available options and check if our attributes have those set.
$theme_styles = $this->get_theme_props( $theme )->get_theme();
$style_css = '';
if ( ! empty( $theme_styles['styles'] ) ) {
foreach ( $theme_styles['styles'] as $element => $options ) {
if ( $object !== $element ) {
continue;
}
foreach ( $options as $style_key => $value ) {
$atts_key = $element . '_' . $style_key;
if ( ! empty( $key ) && $key !== $style_key ) {
// Allow output for just a specific key.
continue;
}
if ( ! empty( $atts[ $atts_key ] ) ) {
if ( is_bool( $atts[ $atts_key ] ) || 'on' === $atts[ $atts_key ] ) {
continue;
}
if ( 'size' === $style_key ) {
$style_key = 'font-size';
$atts[ $atts_key ] .= 'px';
}
if ( 'background' === $style_key || 'background' === $element && 'color' === $style_key ) {
$style_key = 'background-color';
}
if ( 'border' === $element || 'border' === $style_key ) {
$style_key = 'border-color';
}
$style_css .= $style_key . ':' . $atts[ $atts_key ] . ';';
}
}
}
}
return $style_css;
}
/**
* Get the current instance based on the called class.
*
* @return mixed
*/
public static function get_instance() {
if ( ! function_exists( 'get_called_class' ) ) {
return false;
}
$class = get_called_class();
if ( ! isset( self::$instances[ $class ] ) ) {
self::$instances[ $class ] = new $class;
}
return self::$instances[ $class ];
}
/**
* Check if the post is excluded from loading the widget.
*
* @param null|WP_Post $post The post to check if it's excluded.
*
* @return bool
*/
public function is_post_excluded( $post = null ) {
if ( is_null( $post ) ) {
$post = get_post( get_the_ID() );
}
$excluded = false;
$posts_to_exclude = $this->exclude_posts;
if ( ! empty( $posts_to_exclude ) ) {
$post_ids = array();
foreach ( $posts_to_exclude as $exclude_post ) {
if ( ! empty( $exclude_post['id'] ) ) {
$post_ids[] = intval( $exclude_post['id'] );
}
}
if ( in_array( $post->ID, $post_ids, true ) ) {
$excluded = true;
}
}
return $excluded;
}
/**
* Build a wrapper class based on theme, instance and some settings.
*
* @param array $atts Attributes of the shortcode/instance to process for output.
*
* @return string
*/
public function get_wrapper_class( $atts ) {
$theme = $this->theme;
if ( ! empty( $atts['theme'] ) ) {
$theme = $atts['theme'];
}
$columns = ! empty( $atts['columns'] ) ? $atts['columns'] : $this->theme_columns;
$classes = array(
'monsterinsights-' . $this->type . '-popular-posts',
'monsterinsights-' . $this->type . '-popular-posts-' . $theme,
'no_styles' !== $this->styling ? 'monsterinsights-popular-posts-styled' : '',
);
if ( $columns ) {
$classes[] = 'monsterinsights-' . $this->type . '-popular-posts-columns-' . $columns;
}
if ( isset( $atts['className'] ) ) {
$classes[] = $atts['className'];
}
$classname = implode( ' ', $classes );
return $classname;
}
/**
* Check if the id is of the currently displayed post. Compatible with the Ajaxify functionality.
*
* @param $id
*
* @return bool
*/
public function is_current_post( $id ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$current_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing
return $id === $current_id;
}
// Only run this check for singular pages.
if ( ! is_singular() ) {
return false;
}
return get_the_ID() === absint( $id );
}
/**
* Helper function that checks if a post should be displayed on the current page.
*
* @param int $id
*
* @return bool
*/
public function should_display_post( $id ) {
$shown = $this->get_shown_posts();
if ( in_array( $id, $shown, true ) ) {
return false;
}
if ( $this->is_current_post( $id ) ) {
return false;
}
return true;
}
/**
* This function grabs the posts from the cache or a fresh query and runs them through a check if they should be
* displayed on the current page to avoid duplicates.
*
* @return array
*/
public function get_posts_to_display() {
$posts = $this->get_posts();
$returned_posts = array();
foreach ( $posts as $post ) {
if ( $this->should_display_post( $post['id'] ) ) {
$returned_posts[] = $post;
}
}
if ( apply_filters( 'monsterinsights_popular_posts_show_duplicates', true ) && count( $posts ) > 0 && count( $this->shown_posts ) > 0 && count( $returned_posts ) === 0 ) {
$this->shown_posts = array(); // Reset shown posts.
return $this->get_posts_to_display(); // Run the function to grab the same posts again.
}
return $returned_posts;
}
/**
* Check if the current instance has any posts available to display.
*
* @param array $posts Posts array to check if still available for display.
*
* @return bool
*/
public function has_posts_to_show( $posts ) {
foreach ( $posts as $post ) {
if ( $this->should_display_post( $post['id'] ) ) {
return true;
}
}
return false;
}
/**
* Only inline styles that were customized for the specific instance.
*
* @return array
*/
public function get_themes_styles_for_output() {
$stored_styles = $this->get_theme_props()->get_theme_stored_styles();
$themes = ! empty( $stored_styles[ $this->type ] ) && is_array( $stored_styles[ $this->type ] ) ? $stored_styles[ $this->type ] : array();
return $themes;
}
}
class-popular-posts-ajax.php 0000644 00000014246 15154256707 0012154 0 ustar 00 <?php
/**
* Used to handle ajax requests specific to popular posts.
*
* @package MonsterInsights
*/
/**
* Class MonsterInsights_Popular_Posts_Ajax
*/
class MonsterInsights_Popular_Posts_Ajax {
/**
* MonsterInsights_Popular_Posts_Ajax constructor.
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_api_endpoints' ) );
add_action( 'wp_ajax_monsterinsights_popular_posts_empty_cache', array( $this, 'empty_cache' ) );
add_action( 'wp_ajax_monsterinsights_popular_posts_get_widget_output', array( $this, 'get_ajax_output' ) );
add_action( 'wp_ajax_nopriv_monsterinsights_popular_posts_get_widget_output', array(
$this,
'get_ajax_output'
) );
add_action( 'wp_ajax_monsterinsights_get_popular_posts_themes', array( $this, 'ajax_get_themes' ) );
}
/**
* Register the wp-json API endpoints for the Gutenberg blocks.
*/
public function register_api_endpoints() {
register_rest_route( 'monsterinsights/v1', '/popular-posts/themes/(?P<type>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => array( $this, 'get_gutenberg_themes' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => array(
'type' => array(),
),
) );
register_rest_route( 'monsterinsights/v1', '/terms/(?P<slug>[a-zA-Z0-9-_]+)', array(
'methods' => 'GET',
'callback' => array( $this, 'get_taxonomy_terms' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => array(
'slug' => array(),
),
) );
register_rest_route( 'monsterinsights/v1', '/taxonomy/(?P<slug>[a-zA-Z0-9-_]+)', array(
'methods' => 'GET',
'callback' => array( $this, 'get_taxonomy' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => array(
'slug' => array(),
),
) );
}
/**
* Get the themes for Gutenberg (use the specific nonce).
*/
public function get_gutenberg_themes( $data ) {
$type = ! empty( $data['type'] ) ? $data['type'] : 'inline';
return $this->get_themes_by_type( $type );
}
/**
* Get the themes for a specific type using ajax.
*/
public function ajax_get_themes() {
check_ajax_referer( 'mi-admin-nonce', 'nonce' );
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : 'inline';
wp_send_json_success( $this->get_themes_by_type( $type, false ) );
}
/**
* Helper to get themes by type.
*
* @param string $type The widget type: inline/widget/products.
* @param bool $styled Whether to style the selected theme or not.
*
* @return array
*/
public function get_themes_by_type( $type, $styled = true ) {
$theme = '';
if ( $styled ) {
if ( 'inline' === $type ) {
$theme = MonsterInsights_Popular_Posts_Inline()->theme;
}
if ( 'widget' === $type ) {
$theme = MonsterInsights_Popular_Posts_Widget()->theme;
}
}
$themes = $this->get_themes( $type, $theme );
$themes_array = $themes->themes;
if ( isset( $themes_array[ $theme ] ) && $styled ) {
$themes_array[ $theme ] = $themes->get_theme();
}
$response = array(
'themes' => $themes_array,
'selected' => $theme,
);
return $response;
}
/**
* Get themes by type.
*
* @param string $type The widget type: inline/widget/products.
* @param string $theme The selected theme.
*
* @return MonsterInsights_Popular_Posts_Themes
*/
public function get_themes( $type, $theme ) {
$themes_object = new MonsterInsights_Popular_Posts_Themes( $type, $theme );
return $themes_object;
}
/**
* Get a specific theme details.
*
* @param string $type The widget type: inline/widget/products.
* @param string $theme The selected theme.
*
* @return array|mixed
*/
public function get_theme_details( $type, $theme ) {
$themes = new MonsterInsights_Popular_Posts_Themes( $type, $theme );
return $themes->get_theme();
}
/**
* Handler for loading taxonomy terms using a custom endpoint.
*
* @param array $data Data passed from the request.
*
* @return array
*/
public function get_taxonomy_terms( $data ) {
$slug = ! empty( $data['slug'] ) ? $data['slug'] : 'category';
$terms = get_terms( array(
'taxonomy' => $slug,
) );
$return = array();
if ( ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$return[] = array(
'id' => $term->term_id,
'name' => $term->name,
'parent' => $term->parent,
);
}
}
return $return;
}
/**
* Get details for a taxonomy so we can use it in the Gutenberg block.
*
* @param array $data Data passed from the request.
*
* @return false|WP_Taxonomy
*/
public function get_taxonomy( $data ) {
$slug = ! empty( $data['slug'] ) ? $data['slug'] : 'category';
return get_taxonomy( $slug );
}
/**
* Ajax handler to empty the Popular Posts cache for all instances.
*/
public function empty_cache() {
check_ajax_referer( 'mi-admin-nonce', 'nonce' );
if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
return;
}
$types = array(
'inline',
'widget',
'products',
);
foreach ( $types as $type ) {
delete_option( 'monsterinsights_popular_posts_cache_' . $type );
}
wp_send_json_success();
}
/**
* Ajax handler to get the output for Popular Posts widgets from the JSON data on the frontend.
*/
public function get_ajax_output() {
check_ajax_referer( 'mi-popular-posts' );
if ( empty( $_POST['data'] ) || ! is_array( $_POST['data'] ) ) {
return;
}
$html = array();
$widgets_args = $_POST['data']; // phpcs:ignore
foreach ( $widgets_args as $args ) {
$args = json_decode( sanitize_text_field( wp_unslash( $args ) ), true );
if ( ! empty( $args['type'] ) ) {
$type = ucfirst( $args['type'] );
$widget_function = function_exists( 'MonsterInsights_Popular_Posts_' . $type ) ? call_user_func( 'MonsterInsights_Popular_Posts_' . $type ) : false;
if ( $widget_function ) {
$html[] = $widget_function->get_rendered_html( $args );
}
}
}
wp_send_json( $html );
}
}
new MonsterInsights_Popular_Posts_Ajax();
class-popular-posts-cache.php 0000644 00000006602 15154256707 0012271 0 ustar 00 <?php
/**
* This class is used for handling Popular Posts caching.
*
* @package MonsterInsights
*/
/**
* Class MonsterInsights_Popular_Posts_Cache
*/
class MonsterInsights_Popular_Posts_Cache {
/**
* Instance type (inline/widget/products).
*
* @var string
*/
public $type;
/**
* MonsterInsights_Popular_Posts_Cache constructor.
*
* @param string $type The instance type (inline/widget/products).
*/
public function __construct( $type ) {
$this->type = $type;
}
/**
* Build an unique key from the arguments so we can cache different instances.
* This way, the Gutenberg block or the sidebar widget get cached with their own query settings
* if they are different from the ones set in Vue.
*
* @param $args
*
* @return string
*/
public function get_args_key( $args ) {
return md5( wp_json_encode( $args ) );
}
/**
* Get the specific options key for the set type.
*
* @return string
*/
public function get_cache_key() {
return 'monsterinsights_popular_posts_cache_' . $this->type;
}
/**
* Get cached posts data and check expiration. Each query result is stored with the timestamp and that
* is used to compare to the current settings for expiration.
*
* @param array $args This is an array with query parameters for WP_Query used to identify if this query has been cached.
*
* @return array
*/
public function get_cached_posts( $args ) {
$cache_refresh_days = $this->get_cache_interval();
$cached_data = get_option( $this->get_cache_key(), array() );
$args_key = $this->get_args_key( $args ); // Generate an unique key based on the instance settings.
if ( isset( $cached_data[ $args_key ] ) && isset( $cached_data[ $args_key ]['saved_at'] ) ) {
$time_since = time() - $cached_data[ $args_key ]['saved_at'];
if ( $time_since < intval( $cache_refresh_days ) * DAY_IN_SECONDS ) {
return $cached_data[ $args_key ]['posts'];
} else {
// It's expired so let's delete it.
unset( $cached_data[ $args_key ] );
update_option( $this->get_cache_key(), $cached_data );
}
}
return array();
}
/**
* Get the option set in the settings for cache expiration.
*
* @return int
*/
private function get_cache_interval() {
$cache_refresh_days = monsterinsights_get_option( 'popular_posts_caching_refresh', 7 );
// If they downgraded and previously used a custom interval use the default 7 until the update the option.
if ( 'custom' === $cache_refresh_days ) {
$cache_refresh_days = 7;
}
return intval( $cache_refresh_days );
}
/**
* Store a query result in the cache along with the arguments used to grab them.
*
* @param array $args Arguments used in WP_Query used to build an unique key for the loaded data.
* @param array $posts An array of posts that resulted from the query to be saved in the cache.
*/
public function save_posts_to_cache( $args, $posts ) {
if ( empty( $posts ) ) {
// Don't save empty posts.
return;
}
$args_key = md5( wp_json_encode( $args ) ); // Generate an unique key based on the instance settings.
$cached_data = get_option( $this->get_cache_key(), array() );
$cached_data[ $args_key ] = array(
'saved_at' => time(),
'posts' => $posts,
);
update_option( $this->get_cache_key(), $cached_data );
}
/**
* Delete database option for cache.
*/
public function delete_data() {
delete_option( $this->get_cache_key() );
}
}
class-popular-posts-inline.php 0000644 00000023754 15154256707 0012513 0 ustar 00 <?php
/**
* Code specific to the inline Popular Posts widget type.
*/
/**
* Class MonsterInsights_Popular_Posts_Inline
*/
class MonsterInsights_Popular_Posts_Inline extends MonsterInsights_Popular_Posts {
/**
* Used to load the setting specific for this class.
*
* @var string
*/
protected $settings_key = 'popular_posts_inline';
/**
* Used for registering the shortcode specific to this class.
*
* @var string
*/
protected $shortcode_key = 'monsterinsights_popular_posts_inline';
/**
* The instance type. Used for loading specific settings.
*
* @var string
*/
protected $type = 'inline';
/**
* Inline-specific hooks.
*/
public function hooks() {
parent::hooks();
add_action( 'wp', array( $this, 'maybe_auto_insert' ) );
}
/**
* Get the rendered HTML for output.
*
* @param array $atts These are attributes used to build the specific instance, they can be either shortcode
* attributes or Gutenberg block props.
*
* @return string
*/
public function get_rendered_html( $atts ) {
$theme = $this->theme;
if ( ! empty( $atts['theme'] ) ) {
$theme = $atts['theme'];
}
$theme = $this->is_theme_available( $theme );
$posts = $this->get_posts_to_display();
if ( empty( $posts ) ) {
return '';
}
if ( 'curated' === $this->sort && apply_filters( 'monsterinsights_popular_posts_inline_curated_shuffle', true ) ) {
// Randomize the order.
shuffle( $posts );
}
$theme_styles = $this->get_theme_props( $theme )->get_theme();
$limit = ! empty( $theme_styles['posts'] ) ? $theme_styles['posts'] : 1;
$label_text = '';
if ( isset( $theme_styles['styles']['label'] ) ) {
$label_text = isset( $atts['labelText'] ) ? $atts['labelText'] : $theme_styles['styles']['label']['text'];
}
// Wrap in a P tag to keep the same spacing.
$html = '<div class="' . esc_attr($this->get_wrapper_class( $atts )) . '" ';
$html .= ! empty( $this->get_element_style( $theme, 'background', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'background', $atts ) ) . '"' : '';
$html .= '>';
if ( ! empty( $theme_styles['image'] ) && ! empty( $posts[0]['image'] ) ) {
$html .= '<div class="monsterinsights-inline-popular-posts-image">';
$html .= '<img src="' . esc_url($posts[0]['image']) . '" srcset=" ' . esc_attr($posts[0]['srcset']) . ' " alt="' . esc_attr( $posts[0]['title'] ) . '" />';
$html .= '</div>';
}
$html .= '<div class="monsterinsights-inline-popular-posts-text">';
if ( ! empty( $theme_styles['styles']['icon'] ) ) {
$html .= '<span class="monsterinsights-inline-popular-posts-icon" style=""><svg width="14" height="19" viewBox="0 0 14 19" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7.875 0.899463C7.875 1.59183 8.0816 2.24711 8.49479 2.8653C8.93229 3.48349 9.44271 4.06458 10.026 4.60859C10.6337 5.15259 11.2292 5.73369 11.8125 6.35188C12.4201 6.97007 12.9306 7.76135 13.3438 8.72572C13.7812 9.66537 14 10.7163 14 11.8785C14 13.832 13.3073 15.5011 11.9219 16.8858C10.5608 18.2953 8.92014 19 7 19C5.07986 19 3.42708 18.2953 2.04167 16.8858C0.680556 15.5011 0 13.832 0 11.8785C0 9.94973 0.668403 8.28062 2.00521 6.87116C2.27257 6.57443 2.58854 6.50024 2.95312 6.64861C3.31771 6.79697 3.5 7.08134 3.5 7.50171V10.6545C3.5 11.3221 3.71875 11.8908 4.15625 12.3607C4.61806 12.8305 5.16493 13.0654 5.79688 13.0654C6.45312 13.0654 7.01215 12.8428 7.47396 12.3978C7.93576 11.9279 8.16667 11.3592 8.16667 10.6916C8.16667 10.2712 8.04514 9.86318 7.80208 9.46754C7.58333 9.0719 7.31597 8.71336 7 8.3919C6.68403 8.07044 6.34375 7.73662 5.97917 7.39043C5.63889 7.04425 5.34722 6.66097 5.10417 6.2406C4.88542 5.82024 4.73958 5.35041 4.66667 4.83114C4.59375 4.31186 4.67882 3.68131 4.92188 2.93948C5.18924 2.17293 5.63889 1.33219 6.27083 0.417277C6.51389 0.0463641 6.84201 -0.0772735 7.25521 0.0463641C7.6684 0.170002 7.875 0.454368 7.875 0.899463Z" fill="#EB5757"></path></svg></span>';
}
if ( ! empty( $theme_styles['styles']['label'] ) ) {
$html .= '<span class="monsterinsights-inline-popular-posts-label" ';
$html .= ! empty( $this->get_element_style( $theme, 'label', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'label', $atts ) ) . '"' : '';
$html .= '>' . esc_html( $label_text ) . '</span>';
}
if ( ! empty( $theme_styles['styles']['border'] ) ) {
$html .= '<span class="monsterinsights-inline-popular-posts-border" ';
$html .= ! empty( $this->get_element_style( $theme, 'border', $atts, 'color' ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'border', $atts, 'color' ) ) . '"' : '';
$html .= '></span>';
}
if ( ! empty( $theme_styles['styles']['border']['color2'] ) ) {
$html .= '<span class="monsterinsights-inline-popular-posts-border-2" ';
$html .= ! empty( $this->get_element_style( $theme, 'border', $atts, 'color2' ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'border', $atts, 'color2' ) ) . '"' : '';
$html .= '></span>';
}
$display_count = 0;
foreach ( $posts as $post ) {
$display_count ++;
if ( $display_count > $limit ) {
break;
}
$this->set_post_shown( $post['id'] );
$html .= '<div class="monsterinsights-inline-popular-posts-post">';
$html .= '<a class="monsterinsights-inline-popular-posts-title" ';
$html .= ! empty( $this->get_element_style( $theme, 'title', $atts ) ) ? ' style="' . esc_attr( $this->get_element_style( $theme, 'title', $atts ) ) . '" ' : '';
$html .= ' href="' . esc_url( $post['link'] ) . '">' . esc_html( $post['title'] );
$html .= '</a>';
$html .= '</div>';
}
$html .= '</div>';// Text div.
$html .= '</div><p></p>';// Main div.
return $html;
}
/**
* Specific inline styles based on theme settings.
*
* @return string
*/
public function build_inline_styles() {
$themes = $this->get_themes_styles_for_output();
$styles = '';
foreach ( $themes as $theme_key => $theme_styles ) {
if ( ! empty( $theme_styles['background'] ) ) {
$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' {';
if ( ! empty( $theme_styles['background']['color'] ) ) {
$styles .= 'background-color:' . esc_attr($theme_styles['background']['color']) . ';';
}
if ( ! empty( $theme_styles['background']['border'] ) ) {
$styles .= 'border-color:' . esc_attr($theme_styles['background']['border']) . ';';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['label'] ) ) {
$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-label {';
if ( ! empty( $theme_styles['label']['color'] ) ) {
$styles .= 'color:' . esc_attr($theme_styles['label']['color']) . ';';
}
if ( ! empty( $theme_styles['label']['background'] ) ) {
$styles .= 'background-color:' . esc_attr($theme_styles['label']['background']) . ';';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['title'] ) ) {
$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-title {';
if ( ! empty( $theme_styles['title']['color'] ) ) {
$styles .= 'color:' . esc_attr($theme_styles['title']['color']) . ';';
}
if ( ! empty( $theme_styles['title']['size'] ) ) {
$styles .= 'font-size:' . esc_attr($theme_styles['title']['size']) . 'px;';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['border'] ) ) {
$styles .= '.monsterinsights-inline-popular-posts.monsterinsights-popular-posts-styled.monsterinsights-inline-popular-posts-' . $theme_key . ' .monsterinsights-inline-popular-posts-border {';
if ( ! empty( $theme_styles['border']['color'] ) ) {
$styles .= 'border-color:' . esc_attr($theme_styles['border']['color']) . ';';
}
$styles .= '}';
}
}
return $styles;
}
/**
* Check if we should attempt to automatically insert the inline widget.
*/
public function maybe_auto_insert() {
$post_types = $this->post_types;
if ( ! empty( $post_types ) && is_singular( $post_types ) && 'automatic' === $this->placement ) {
add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) );
}
}
/**
* Insert the widget in the content.
*
* @param string $content The post content.
*
* @return string
*/
public function add_inline_posts_to_content(
$content
) {
if ( $this->is_post_excluded() ) {
return $content;
}
$words_count = str_word_count( $content );
$after_count = intval( $this->after_count );
// Insert only if there are more words then the insert after value.
if ( $words_count > $after_count ) {
$words = explode( ' ', $content );
$count = 0;
foreach ( $words as $index => $word ) {
$count ++;
if ( $count > $after_count ) {
$p_index = strpos( $word, '</p>' );
// Make sure the paragraph tag is not wrapped in another element like a blockquote.
if ( false !== $p_index && false === strpos( $word, '</p></' ) ) {
$words[ $index ] = substr_replace( $word, $this->shortcode_output( array() ), $p_index + 4, 0 );
$this->posts = array();
break;
}
}
}
$content = implode( ' ', $words );
}
return $content;
}
/**
* Check if the selected theme is available with the current license to avoid showing a theme not available.
* Returns the default 'alpha' theme if not available.
*
* @param string $theme Theme slug for which we are checking.
*
* @return string
*/
public function is_theme_available( $theme ) {
$theme_props = $this->get_theme_props( $theme )->get_theme();
if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) {
return $theme;
}
return 'alpha';
}
}
/**
* Get the current class in a function.
*
* @return MonsterInsights_Popular_Posts_Inline Instance of the current class.
*/
function MonsterInsights_Popular_Posts_Inline() {
return MonsterInsights_Popular_Posts_Inline::get_instance();
}
MonsterInsights_Popular_Posts_Inline();
class-popular-posts-widget-sidebar.php 0000644 00000036145 15154256707 0014125 0 ustar 00 <?php
/**
* Class used to add the Popular Posts widget to the Appearance > Widget area.
*/
/**
* Class MonsterInsights_Popular_Posts_Widget_Sidebar
*/
class MonsterInsights_Popular_Posts_Widget_Sidebar extends WP_Widget {
/**
* Hold widget settings defaults, populated in constructor.
*
* @since 7.12.0
*
* @var array
*/
protected $defaults;
/**
* Hold widget options that are theme specific.
*
* @since 7.12.0
*
* @var array
*/
protected $conditional_options;
/**
* Constructor
*
* @since 7.12.0
*/
public function __construct() {
// Widget defaults.
$this->defaults = array(
'title' => '',
'display_title' => 'on',
'post_count' => 5,
'theme' => 'alpha',
'title_color' => '#393F4C',
'title_size' => 12,
'label_color' => '#EB5757',
'label_text' => 'Trending',
'meta_color' => '#99A1B3',
'meta_size' => '12',
'meta_author' => 'on',
'meta_date' => 'on',
'meta_comments' => 'on',
'background_color' => '#F0F2F4',
'border_color' => '#D3D7DE',
'columns' => '1',
);
$this->conditional_options = array(
'title_color' => array( 'title', 'color' ),
'title_size' => array( 'title', 'size' ),
'label_color' => array( 'label', 'color' ),
'label_text' => array( 'label', 'text' ),
'background_color' => array( 'background', 'color' ),
'background_border' => array( 'background', 'border' ),
'meta_color' => array( 'meta', 'color' ),
'meta_size' => array( 'meta', 'size' ),
'meta_author' => array( 'meta', 'author' ),
'meta_date' => array( 'meta', 'date' ),
'meta_comments' => array( 'meta', 'comments' ),
'comments_color' => array( 'comments', 'color' ),
);
// Widget Slug.
$widget_slug = 'monsterinsights-popular-posts-widget';
// Widget basics.
$widget_ops = array(
'classname' => $widget_slug,
'description' => esc_html_x( 'Display popular posts.', 'Widget', 'google-analytics-for-wordpress' ),
);
// Widget controls.
$control_ops = array(
'id_base' => $widget_slug,
);
$this->add_scripts();
// Load widget.
parent::__construct( $widget_slug, esc_html_x( 'Popular Posts - MonsterInsights', 'Widget', 'google-analytics-for-wordpress' ), $widget_ops, $control_ops );
}
/**
* Output the HTML for this widget.
*
* @param array $args An array of standard parameters for widgets in this theme.
* @param array $instance An array of settings for this widget instance.
*
* @since 7.12.0
*
*/
public function widget( $args, $instance ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['before_widget'];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
if ( $instance['display_title'] && ! empty( $instance['title'] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['before_title'];
echo wp_kses_post( $title );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['after_title'];
}
$atts = array(
'theme' => $instance['theme'],
'post_count' => $instance['post_count'],
'columns' => 1, // Sidebar is not wide so we always use the 1 column layout.
'widget_title' => false, // Override this in favor of sidebar-specific markup above.
);
foreach ( $this->conditional_options as $key => $default ) {
if ( ! empty( $instance[ $key ] ) ) {
$atts[ $key ] = $instance[ $key ];
}
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo MonsterInsights_Popular_Posts_Widget()->shortcode_output( $atts );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['after_widget'];
}
/**
* Deal with the settings when they are saved by the admin. Here is
* where any validation should be dealt with.
*
* @param array $new_instance An array of new settings as submitted by the admin.
* @param array $old_instance An array of the previous settings.
*
* @return array The validated and (if necessary) amended settings
* @since 7.12.0
*
*/
public function update( $new_instance, $old_instance ) {
$new_instance['title'] = wp_strip_all_tags( $new_instance['title'] );
$new_instance['theme'] = wp_strip_all_tags( $new_instance['theme'] );
$new_instance['display_title'] = isset( $new_instance['display_title'] ) ? wp_strip_all_tags( $new_instance['display_title'] ) : '';
$new_instance['post_count'] = absint( $new_instance['post_count'] );
// Theme-dependant options.
$themes = new MonsterInsights_Popular_Posts_Themes( 'widget', ! empty( $old_instance['theme'] ) ? $old_instance['theme'] : '' );
$theme = $themes->get_theme();
foreach ( $this->conditional_options as $key => $obj ) {
$new_instance = $this->maybe_remove_option( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ), $key, $new_instance );
}
return $new_instance;
}
/**
* Process dynamic and checkbox values so they are stored correctly and specific to the current theme.
*
* @param bool $is_used A check if this property is used in the currently selected theme.
* @param string $key The key of the property we're checking.
* @param array $instance The current widget instance, new instance.
*
* @return mixed
*/
public function maybe_remove_option( $is_used, $key, $instance ) {
$checkboxes = array(
'meta_author',
'meta_date',
'meta_comments',
);
if ( $is_used && ! isset( $instance[ $key ] ) && in_array( $key, $checkboxes ) ) {
$instance[ $key ] = 'off';
} elseif ( ! $is_used && isset( $instance[ $key ] ) ) {
unset( $instance[ $key ] );
} elseif ( $is_used && isset( $instance[ $key ] ) ) {
$instance[ $key ] = wp_strip_all_tags( $instance[ $key ] );
}
return $instance;
}
/**
* Display the form for this widget on the Widgets page of the WP Admin area.
*
* @param array $instance An array of the current settings for this widget.
*
* @since 7.12.0
*/
public function form( $instance ) {
// Merge with defaults but use theme settings from Vue as defaults.
$theme_name = empty( $instance['theme'] ) ? $this->defaults['theme'] : $instance['theme'];
$themes = new MonsterInsights_Popular_Posts_Themes( 'widget', $theme_name );
$theme = $themes->get_theme();
$this->prepare_defaults_from_theme( $theme );
$instance = wp_parse_args( (array) $instance, $this->defaults );
$title_font_sizes = apply_filters( 'monsterinsights_popular_posts_widget_title_sizes', range( 10, 35 ) );
$this->text_input( 'title', _x( 'Widget Title:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
$categories = array(
array(
'name' => 'News',
'value' => '0',
),
array(
'name' => 'Technology',
'value' => '1',
),
);
?>
<p>
<input type="checkbox"
id="<?php echo esc_attr( $this->get_field_id( 'display_title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'display_title' ) ); ?>"
value="on" <?php checked( $instance['display_title'], 'on' ); ?> />
<label for="<?php echo esc_attr( $this->get_field_id( 'display_title' ) ); ?>">
<?php echo esc_html( _x( 'Display Widget Title', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
</label>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'post_count' ) ); ?>">
<?php echo esc_html( _x( 'Number of posts to display:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
</label>
<select id="<?php echo esc_attr( $this->get_field_id( 'post_count' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'post_count' ) ); ?>">
<option value="5" <?php selected( $instance['post_count'], 5 ); ?>>5</option>
<option value="10" <?php selected( $instance['post_count'], 10 ); ?>>10</option>
</select>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'theme' ) ); ?>">
<?php echo esc_html( _x( 'Theme:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
</label>
<select id="<?php echo esc_attr( $this->get_field_id( 'theme' ) ); ?>"
class="widefat monsterinsights-save-on-change"
name="<?php echo esc_attr( $this->get_field_name( 'theme' ) ); ?>">
<?php foreach ( $themes->themes as $key => $details ) {
if ( 'lite' !== $details['level'] ) {
continue;
}
?>
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $instance['theme'], $key ); ?>>
<?php echo esc_html( ucfirst( $key ) ); ?>
</option>
<?php } ?>
</select>
</p>
<div class="monsterinsights-widget-theme-preview">
<span class="monsterinsights-widget-theme-preview-label">
<?php esc_html_e( 'Theme Preview', 'google-analytics-for-wordpress' ); ?>
</span>
<div
class="monsterinsights-widget-theme-preview-icon monsterinsights-widget-theme-preview-icon-<?php echo esc_attr( $instance['theme'] ); ?>"></div>
</div>
<?php if ( ! empty( $theme['styles']['title']['color'] ) ) {
$this->color_input( 'title_color', _x( 'Title Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
?>
<?php
}
if ( ! empty( $theme['styles']['title']['size'] ) ) {
$this->size_input( 'title_size', _x( 'Title Font Size:', 'Widget', 'google-analytics-for-wordpress' ), $instance, $title_font_sizes );
}
if ( ! empty( $theme['styles']['label']['color'] ) ) {
$this->color_input( 'label_color', _x( 'Label Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
}
if ( ! empty( $theme['styles']['label']['editable'] ) && ! empty( $theme['styles']['label']['text'] ) ) {
$this->text_input( 'label_text', _x( 'Label Text:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
}
if ( ! empty( $theme['styles']['background']['border'] ) ) {
$this->color_input( 'background_border', _x( 'Border Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
}
if ( ! empty( $theme['styles']['background']['color'] ) ) {
$this->color_input( 'background_color', _x( 'Background Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
}
if ( ! empty( $theme['styles']['comments']['color'] ) ) {
$this->color_input( 'comments_color', _x( 'Comments Count Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance );
}
?>
<p>
<label class="monsterinsights-label-block">
<?php echo esc_html( _x( 'Only Show Posts from These Categories:', 'Widget', 'google-analytics-for-wordpress' ) ); ?>
<span class="monsterinsights-pro-pill">PRO</span>
</label>
<div
class="select300 select300-container select300-container--default select300-container--disabled select300-container--focus"
dir="ltr" data-select300-id="2" style="width: auto;">
<div class="selection">
<div class="select300-selection select300-selection--multiple">
<ul class="select300-selection__rendered">
<li class="select300-selection__choice" title="News" data-select300-id="5">
<span class="select300-selection__choice__remove" role="presentation">×</span>News
</li>
<li class="select300-selection__choice" title="Technology" data-select300-id="6">
<span class="select300-selection__choice__remove" role="presentation">×</span>Technology
</li>
</ul>
</div>
</div>
</div>
</p>
<?php
}
/**
* Colorpicker input element.
*
* @param string $name Name of the input, for saving/loading.
* @param string $label Label of the element.
* @param array $instance The current widget instance.
*/
public function color_input( $name, $label, $instance ) {
?>
<p>
<label class="monsterinsights-label-block"
for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
<?php echo esc_html( $label ); ?>
</label>
<input type="text"
id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
value="<?php echo esc_attr( $instance[ $name ] ); ?>"
class="widefat monsterinsights-color-field"/>
</p>
<?php
}
/**
* Regular text input.
*
* @param string $name Name of the input, for saving/loading.
* @param string $label Label of the element.
* @param array $instance The current widget instance.
*/
public function text_input( $name, $label, $instance ) {
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
<?php echo esc_html( $label ); ?>
</label>
<input type="text"
id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>"
value="<?php echo esc_attr( $instance[ $name ] ); ?>" class="widefat"/>
</p>
<?php
}
/**
* Size input - used for font size inputs.
*
* @param string $name Name of the input, for saving/loading.
* @param string $label Label of the element.
* @param array $instance The current widget instance.
* @param array $range The options available to select.
*/
public function size_input( $name, $label, $instance, $range = array() ) {
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>">
<?php echo esc_html( $label ); ?>
</label>
<select id="<?php echo esc_attr( $this->get_field_id( $name ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( $name ) ); ?>" class="widefat">
<?php foreach ( $range as $font_size ) { ?>
<option value="<?php echo absint( $font_size ); ?>" <?php selected( $instance[ $name ], $font_size ); ?>>
<?php printf( esc_html_x( '%dpx', 'google-analytics-for-wordpress' ), $font_size ); // phpcs:ignore ?>
</option>
<?php } ?>
</select>
</p>
<?php
}
/**
* Prepare theme specific options.
*
* @param array $theme The theme options.
*/
public function prepare_defaults_from_theme( $theme ) {
foreach ( $this->conditional_options as $key => $obj ) {
if ( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ) ) {
$this->defaults[ $key ] = $theme['styles'][ $obj[0] ][ $obj[1] ];
}
}
}
/**
* Load specific widget scripts in the admin.
*/
public function add_scripts() {
add_action( 'admin_enqueue_scripts', array( $this, 'load_widget_scripts' ) );
}
/**
* Load admin-specific widget scripts.
*/
public function load_widget_scripts() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
if ( ! isset( $screen->id ) || 'widgets' !== $screen->id ) {
return;
}
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_style( 'monsterinsights-admin-widget-setting-styles', plugins_url( 'assets/css/admin-widget-settings' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(
'wp-color-picker',
), monsterinsights_get_asset_version() );
wp_register_script( 'monsterinsights-admin-widget-settings', plugins_url( 'assets/js/admin-widget-settings' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array(
'jquery',
'wp-color-picker',
), monsterinsights_get_asset_version(), true );
wp_enqueue_script( 'monsterinsights-admin-widget-settings' );
wp_localize_script( 'monsterinsights-admin-widget-settings', 'monsterinsights_pp', array(
'nonce' => wp_create_nonce( 'mi-admin-nonce' ),
) );
}
}
class-popular-posts-widget.php 0000644 00000020641 15154256707 0012510 0 ustar 00 <?php
/**
* Code specific to the widget Popular Posts widget type.
*/
/**
* Class MonsterInsights_Popular_Posts_Widget
*/
class MonsterInsights_Popular_Posts_Widget extends MonsterInsights_Popular_Posts {
/**
* The instance type. Used for loading specific settings.
*
* @var string
*/
protected $type = 'widget';
/**
* Used to load the setting specific for this class.
*
* @var string
*/
protected $settings_key = 'popular_posts_widget';
/**
* Used for registering the shortcode specific to this class.
*
* @var string
*/
protected $shortcode_key = 'monsterinsights_popular_posts_widget';
/**
* Widget-specific hooks.
*/
public function hooks() {
parent::hooks();
add_action( 'wp', array( $this, 'maybe_auto_insert' ) );
add_action( 'widgets_init', array( $this, 'register_widget' ) );
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array(
$this,
'remove_widget_from_legacy_widgets'
) );
}
/**
* Register Popular Posts widget.
*/
public function register_widget() {
register_widget( 'MonsterInsights_Popular_Posts_Widget_Sidebar' );
}
/**
* Get the rendered HTML for output.
*
* @param array $atts These are attributes used to build the specific instance, they can be either shortcode
* attributes or Gutenberg block props.
*
* @return string
*/
public function get_rendered_html( $atts ) {
$theme = $this->theme;
if ( ! empty( $atts['theme'] ) ) {
$theme = $atts['theme'];
}
$theme = $this->is_theme_available( $theme );
if ( ! empty( $atts['post_count'] ) ) {
$limit = intval( $atts['post_count'] );
} else {
$limit = $this->count;
}
$posts = $this->get_posts_to_display();
if ( empty( $posts ) ) {
return '';
}
if ( 'curated' === $this->sort && apply_filters( 'monsterinsights_popular_posts_widget_curated_shuffle', true ) ) {
// Randomize the order.
shuffle( $posts );
}
$theme_styles = $this->get_theme_props( $theme )->get_theme();
$label_text = '';
if ( isset( $theme_styles['styles']['label'] ) ) {
$label_text = isset( $atts['label_text'] ) ? esc_html($atts['label_text']) : esc_html($theme_styles['styles']['label']['text']);
}
if ( isset( $atts['widget_title'] ) ) {
$show_title = (bool) $atts['widget_title'];
$title_text = empty( $atts['widget_title_text'] ) ? '' : $atts['widget_title_text'];
} else {
$show_title = $this->title;
$title_text = $this->title_text;
}
$html = '<div class="' . esc_attr($this->get_wrapper_class( $atts )) . '">';
if ( $show_title ) {
$html .= '<h2 class="monsterinsights-widget-popular-posts-widget-title">' . esc_html( $title_text ) . '</h2>';
}
$html .= '<ul class="monsterinsights-widget-popular-posts-list">';
$display_count = 0;
foreach ( $posts as $post ) {
$display_count ++;
if ( $display_count > $limit ) {
break;
}
$this->set_post_shown( $post['id'] );
$html .= '<li ';
$html .= ! empty( $this->get_element_style( $theme, 'background', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'background', $atts ) ) . '"' : '';
$html .= '>';
$html .= '<a href="' . esc_url($post['link']) . '">';
if ( ! empty( $theme_styles['image'] ) && ! empty( $post['image'] ) ) {
$html .= '<div class="monsterinsights-widget-popular-posts-image">';
$html .= '<img src="' . esc_url($post['image']) . '" srcset=" ' . esc_attr($post['srcset']) . ' " alt="' . esc_attr( $post['title'] ) . '" />';
$html .= '</div>';
}
$html .= '<div class="monsterinsights-widget-popular-posts-text">';
if ( isset( $theme_styles['styles']['label'] ) ) {
$html .= '<span class="monsterinsights-widget-popular-posts-label" ';
$html .= ! empty( $this->get_element_style( $theme, 'label', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'label', $atts ) ) . '"' : '';
$html .= '>' . esc_html( $label_text ) . '</span>';
}
$html .= '<span class="monsterinsights-widget-popular-posts-title" ';
$html .= ! empty( $this->get_element_style( $theme, 'title', $atts ) ) ? 'style="' . esc_attr( $this->get_element_style( $theme, 'title', $atts ) ) . '"' : '';
$html .= '>' . esc_html( $post['title'] ) . '</span>';
$html .= '</div>'; // monsterinsights-widget-popular-posts-text.
$html .= '</a>';
$html .= '</li>';
}
$html .= '</ul></div><p></p>';// Main div.
return $html;
}
/**
* Add widget-specific styles based on theme settings.
*/
public function build_inline_styles() {
$themes = $this->get_themes_styles_for_output();
$styles = '';
foreach ( $themes as $theme_key => $theme_styles ) {
if ( ! empty( $theme_styles['background'] ) ) {
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-list li {';
if ( ! empty( $theme_styles['background']['color'] ) ) {
$styles .= 'background-color:' . esc_attr($theme_styles['background']['color']) . ';';
}
if ( ! empty( $theme_styles['background']['border'] ) ) {
$styles .= 'border-color:' . esc_attr($theme_styles['background']['border']) . ';';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['label'] ) ) {
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-label {';
if ( ! empty( $theme_styles['label']['color'] ) ) {
$styles .= 'color:' . esc_attr($theme_styles['label']['color']) . ';';
}
if ( ! empty( $theme_styles['label']['background'] ) ) {
$styles .= 'background-color:' . esc_attr($theme_styles['label']['background']) . ';';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['title'] ) ) {
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-widget-popular-posts-list li .monsterinsights-widget-popular-posts-title {';
if ( ! empty( $theme_styles['title']['color'] ) ) {
$styles .= 'color:' . esc_attr($theme_styles['title']['color']) . ';';
}
if ( ! empty( $theme_styles['title']['size'] ) ) {
$styles .= 'font-size:' . esc_attr($theme_styles['title']['size']) . 'px;';
}
$styles .= '}';
}
if ( ! empty( $theme_styles['border'] ) ) {
$styles .= '.monsterinsights-popular-posts-styled.monsterinsights-widget-popular-posts-' . esc_attr($theme_key) . ' .monsterinsights-inline-popular-posts-border {';
if ( ! empty( $theme_styles['border']['color'] ) ) {
$styles .= 'border-color:' . esc_attr($theme_styles['border']['color']) . ';';
}
$styles .= '}';
}
}
return $styles;
}
/**
* Check if we should attempt to automatically insert the inline widget.
*/
public function maybe_auto_insert() {
$post_types = $this->post_types;
if ( ! empty( $post_types ) && is_singular( $post_types ) && $this->automatic ) {
add_filter( 'the_content', array( $this, 'add_inline_posts_to_content' ) );
}
}
/**
* Insert the widget in the content.
*
* @param string $content The post content.
*
* @return string
*/
public function add_inline_posts_to_content( $content ) {
if ( $this->is_post_excluded() ) {
return $content;
}
$content .= $this->shortcode_output( array() );
return $content;
}
/**
* Check if the selected theme is available with the current license to avoid showing a theme not available.
* Returns the default 'alpha' theme if not available.
*
* @param string $theme Theme slug for which we are checking.
*
* @return string
*/
public function is_theme_available( $theme ) {
$theme_props = $this->get_theme_props( $theme )->get_theme();
if ( ! empty( $theme_props['level'] ) && 'lite' === $theme_props['level'] ) {
return $theme;
}
return 'alpha';
}
/**
* Remove this widget from legacy widgets not to have duplications.
*
* @param string[] $widgets An array of excluded widget-type IDs.
*
* @return mixed
*/
public function remove_widget_from_legacy_widgets( $widgets ) {
// $widgets[] = 'monsterinsights-popular-posts-widget';
return $widgets;
}
}
/**
* Get the current class in a function.
*
* @return MonsterInsights_Popular_Posts_Widget Instance of the current class.
*/
function MonsterInsights_Popular_Posts_Widget() {
return MonsterInsights_Popular_Posts_Widget::get_instance();
}
MonsterInsights_Popular_Posts_Widget();