Posts.php
2 years ago
Shortcode.php
2 years ago
ShortcodeLoader.php
2 years ago
ViewsCount.php
2 years ago
Posts.php
240 lines
| 1 | <?php |
| 2 | namespace WordPressPopularPosts\Shortcode; |
| 3 | |
| 4 | use WordPressPopularPosts\{ Helper, Output }; |
| 5 | use WordPressPopularPosts\Shortcode\Shortcode; |
| 6 | use WordPressPopularPosts\Traits\QueriesPosts; |
| 7 | |
| 8 | class Posts extends Shortcode { |
| 9 | |
| 10 | use QueriesPosts; |
| 11 | |
| 12 | /** |
| 13 | * Admin settings. |
| 14 | * |
| 15 | * @since 6.3.0 |
| 16 | * @var array |
| 17 | */ |
| 18 | private $config = []; |
| 19 | |
| 20 | /** |
| 21 | * Output object. |
| 22 | * |
| 23 | * @since 6.3.0 |
| 24 | * @var \WordPressPopularPosts\Output $output |
| 25 | * @access private |
| 26 | */ |
| 27 | private $output; |
| 28 | |
| 29 | /** |
| 30 | * Construct. |
| 31 | * |
| 32 | * @param array $admin_options |
| 33 | * @param \WordPressPopularPosts\Output $output Output class. |
| 34 | */ |
| 35 | public function __construct(array $admin_options, Output $output) |
| 36 | { |
| 37 | $this->config = $admin_options; |
| 38 | $this->output = $output; |
| 39 | $this->tag = 'wpp'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Handles the HTML output of the shortcode. |
| 44 | * |
| 45 | * @since 6.3.0 |
| 46 | * @param mixed $attributes Array of attributes passed to the shortcode, or an empty string if nothing is passed |
| 47 | * @return string Views count |
| 48 | */ |
| 49 | public function handle($attributes = []) : string |
| 50 | { |
| 51 | /** |
| 52 | * @var string $header |
| 53 | * @var int $limit |
| 54 | * @var int $offset |
| 55 | * @var string $range |
| 56 | * @var bool $freshness |
| 57 | * @var string $order_by |
| 58 | * @var string $post_type |
| 59 | * @var string $pid |
| 60 | * @var string $cat |
| 61 | * @var string $author |
| 62 | * @var int $title_length |
| 63 | * @var int $title_by_words |
| 64 | * @var int $excerpt_length |
| 65 | * @var int $excerpt_format |
| 66 | * @var int $excerpt_by_words |
| 67 | * @var int $thumbnail_width |
| 68 | * @var int $thumbnail_height |
| 69 | * @var string $thumbnail_build |
| 70 | * @var bool $rating |
| 71 | * @var bool $stats_comments |
| 72 | * @var bool $stats_views |
| 73 | * @var bool $stats_author |
| 74 | * @var bool $stats_date |
| 75 | * @var string $stats_date_format |
| 76 | * @var bool $stats_category |
| 77 | * @var string $wpp_start |
| 78 | * @var string $wpp_end |
| 79 | * @var string $header_start |
| 80 | * @var string $header_end |
| 81 | * @var string $post_html |
| 82 | */ |
| 83 | extract(shortcode_atts([ |
| 84 | 'header' => '', |
| 85 | 'limit' => 10, |
| 86 | 'offset' => 0, |
| 87 | 'range' => 'daily', |
| 88 | 'time_unit' => 'hour', |
| 89 | 'time_quantity' => 24, |
| 90 | 'freshness' => false, |
| 91 | 'order_by' => 'views', |
| 92 | 'post_type' => 'post', |
| 93 | 'pid' => '', |
| 94 | 'cat' => '', |
| 95 | 'taxonomy' => 'category', |
| 96 | 'term_id' => '', |
| 97 | 'author' => '', |
| 98 | 'title_length' => 0, |
| 99 | 'title_by_words' => 0, |
| 100 | 'excerpt_length' => 0, |
| 101 | 'excerpt_format' => 0, |
| 102 | 'excerpt_by_words' => 0, |
| 103 | 'thumbnail_width' => 0, |
| 104 | 'thumbnail_height' => 0, |
| 105 | 'thumbnail_build' => 'manual', |
| 106 | 'rating' => false, |
| 107 | 'stats_comments' => false, |
| 108 | 'stats_views' => true, |
| 109 | 'stats_author' => false, |
| 110 | 'stats_date' => false, |
| 111 | 'stats_date_format' => 'F j, Y', |
| 112 | 'stats_category' => false, |
| 113 | 'stats_taxonomy' => false, |
| 114 | 'wpp_start' => '<ul class="wpp-list">', |
| 115 | 'wpp_end' => '</ul>', |
| 116 | 'header_start' => '<h2>', |
| 117 | 'header_end' => '</h2>', |
| 118 | 'post_html' => '', |
| 119 | 'theme' => '' |
| 120 | ], $attributes, 'wpp')); |
| 121 | |
| 122 | // possible values for "Time Range" and "Order by" |
| 123 | $time_units = ['minute', 'hour', 'day', 'week', 'month']; |
| 124 | $range_values = ['daily', 'last24hours', 'weekly', 'last7days', 'monthly', 'last30days', 'all', 'custom']; |
| 125 | $order_by_values = ['comments', 'views', 'avg']; |
| 126 | |
| 127 | $shortcode_ops = [ |
| 128 | 'title' => strip_tags($header), |
| 129 | 'limit' => ( ! empty($limit ) && Helper::is_number($limit) && $limit > 0 ) ? $limit : 10, |
| 130 | 'offset' => ( ! empty($offset) && Helper::is_number($offset) && $offset >= 0 ) ? $offset : 0, |
| 131 | 'range' => ( in_array($range, $range_values) ) ? $range : 'daily', |
| 132 | 'time_quantity' => ( ! empty($time_quantity ) && Helper::is_number($time_quantity) && $time_quantity > 0 ) ? $time_quantity : 24, |
| 133 | 'time_unit' => ( in_array($time_unit, $time_units) ) ? $time_unit : 'hour', |
| 134 | 'freshness' => empty($freshness) ? false : $freshness, |
| 135 | 'order_by' => ( in_array($order_by, $order_by_values) ) ? $order_by : 'views', |
| 136 | 'post_type' => empty($post_type) ? 'post' : $post_type, |
| 137 | 'pid' => rtrim(preg_replace('|[^0-9,]|', '', $pid), ','), |
| 138 | 'cat' => rtrim(preg_replace('|[^0-9,-]|', '', $cat), ','), |
| 139 | 'taxonomy' => empty($taxonomy) ? 'category' : $taxonomy, |
| 140 | 'term_id' => rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ','), |
| 141 | 'author' => rtrim(preg_replace('|[^0-9,]|', '', $author), ','), |
| 142 | 'shorten_title' => [ |
| 143 | 'active' => ( ! empty($title_length) && Helper::is_number($title_length) && $title_length > 0 ), |
| 144 | 'length' => ( ! empty($title_length) && Helper::is_number($title_length) ) ? $title_length : 0, |
| 145 | 'words' => ( ! empty($title_by_words) && Helper::is_number($title_by_words) && $title_by_words > 0 ), |
| 146 | ], |
| 147 | 'post-excerpt' => [ |
| 148 | 'active' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) && $excerpt_length > 0 ), |
| 149 | 'length' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) ) ? $excerpt_length : 0, |
| 150 | 'keep_format' => ( ! empty($excerpt_format) && Helper::is_number($excerpt_format) && $excerpt_format > 0 ), |
| 151 | 'words' => ( ! empty($excerpt_by_words) && Helper::is_number($excerpt_by_words) && $excerpt_by_words > 0 ), |
| 152 | ], |
| 153 | 'thumbnail' => [ |
| 154 | 'active' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ), |
| 155 | 'build' => 'predefined' === $thumbnail_build ? 'predefined' : 'manual', |
| 156 | 'width' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ) ? $thumbnail_width : 0, |
| 157 | 'height' => ( ! empty($thumbnail_height) && Helper::is_number($thumbnail_height) && $thumbnail_height > 0 ) ? $thumbnail_height : 0, |
| 158 | ], |
| 159 | 'rating' => empty($rating) ? false : $rating, |
| 160 | 'stats_tag' => [ |
| 161 | 'comment_count' => empty($stats_comments) ? false : $stats_comments, |
| 162 | 'views' => empty($stats_views) ? false : $stats_views, |
| 163 | 'author' => empty($stats_author) ? false : $stats_author, |
| 164 | 'date' => [ |
| 165 | 'active' => empty($stats_date) ? false : $stats_date, |
| 166 | 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format |
| 167 | ], |
| 168 | 'category' => empty($stats_category) ? false : $stats_category, |
| 169 | 'taxonomy' => [ |
| 170 | 'active' => empty($stats_taxonomy) ? false : $stats_taxonomy, |
| 171 | 'name' => empty($taxonomy) ? 'category' : $taxonomy, |
| 172 | ] |
| 173 | ], |
| 174 | 'markup' => [ |
| 175 | 'custom_html' => true, |
| 176 | 'wpp-start' => empty($wpp_start) ? '' : $wpp_start, |
| 177 | 'wpp-end' => empty($wpp_end) ? '' : $wpp_end, |
| 178 | 'title-start' => empty($header_start) ? '' : $header_start, |
| 179 | 'title-end' => empty($header_end) ? '' : $header_end, |
| 180 | 'post-html' => empty($post_html) ? '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>' : $post_html |
| 181 | ], |
| 182 | 'theme' => [ |
| 183 | 'name' => trim($theme) |
| 184 | ] |
| 185 | ]; |
| 186 | |
| 187 | // Post / Page / CTP filter |
| 188 | $ids = array_filter(explode(',', $shortcode_ops['pid']), 'is_numeric'); |
| 189 | // Got no valid IDs, clear |
| 190 | if ( empty($ids) ) { |
| 191 | $shortcode_ops['pid'] = ''; |
| 192 | } |
| 193 | |
| 194 | // Category filter |
| 195 | $ids = array_filter(explode(',', $shortcode_ops['cat']), 'is_numeric'); |
| 196 | // Got no valid IDs, clear |
| 197 | if ( empty($ids) ) { |
| 198 | $shortcode_ops['cat'] = ''; |
| 199 | } |
| 200 | |
| 201 | // Author filter |
| 202 | $ids = array_filter(explode( ',', $shortcode_ops['author']), 'is_numeric'); |
| 203 | // Got no valid IDs, clear |
| 204 | if ( empty($ids) ) { |
| 205 | $shortcode_ops['author'] = ''; |
| 206 | } |
| 207 | |
| 208 | $shortcode_content = ''; |
| 209 | $cached = false; |
| 210 | |
| 211 | // is there a title defined by user? |
| 212 | if ( |
| 213 | ! empty($header) |
| 214 | && ! empty($header_start) |
| 215 | && ! empty($header_end) |
| 216 | ) { |
| 217 | $shortcode_content .= htmlspecialchars_decode($header_start, ENT_QUOTES) . $header . htmlspecialchars_decode($header_end, ENT_QUOTES); |
| 218 | } |
| 219 | |
| 220 | $isAdmin = isset($_GET['isSelected']) ? $_GET['isSelected'] : false; |
| 221 | |
| 222 | if ( $this->config['tools']['ajax'] && ! is_customize_preview() && ! $isAdmin ) { |
| 223 | $shortcode_content .= '<div class="wpp-shortcode">'; |
| 224 | $shortcode_content .= '<script type="application/json">' . wp_json_encode($shortcode_ops) . '</script>'; |
| 225 | $shortcode_content .= '<div class="wpp-shortcode-placeholder"></div>'; |
| 226 | $shortcode_content .= '</div>'; |
| 227 | } else { |
| 228 | $popular_posts = $this->maybe_query($shortcode_ops); |
| 229 | |
| 230 | $this->output->set_data($popular_posts->get_posts()); |
| 231 | $this->output->set_public_options($shortcode_ops); |
| 232 | $this->output->build_output(); |
| 233 | |
| 234 | $shortcode_content .= $this->output->get_output(); |
| 235 | } |
| 236 | |
| 237 | return $shortcode_content; |
| 238 | } |
| 239 | } |
| 240 |