| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) |
| 3 |
exit; // Exit if accessed directly |
| 4 |
/** |
| 5 |
Plugin Name: Blog Filter |
| 6 |
Description: Blog Filter For WordPress Blog With Multiple Filters |
| 7 |
Version: 1.8.7 |
| 8 |
Author: A WP Life |
| 9 |
Author URI: http://awplife.com/ |
| 10 |
Text Domain: blog-filter |
| 11 |
Domain Path: /languages |
| 12 |
License: GPLv2 or later |
| 13 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 14 |
**/ |
| 15 |
|
| 16 |
if (!class_exists('Awl_Blog_Filter')) { |
| 17 |
|
| 18 |
class Awl_Blog_Filter |
| 19 |
{ |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->_constants(); |
| 24 |
$this->_hooks(); |
| 25 |
} |
| 26 |
|
| 27 |
protected function _constants() |
| 28 |
{ |
| 29 |
//Plugin Version |
| 30 |
define('BF_PLUGIN_VER', '1.8.7'); |
| 31 |
|
| 32 |
//Plugin Text Domain |
| 33 |
define('BF_TEXT_DOMAIN', 'blog-filter'); |
| 34 |
|
| 35 |
//Plugin Name |
| 36 |
define('BF_PLUGIN_NAME', 'Blog Filter'); |
| 37 |
|
| 38 |
//Plugin Slug |
| 39 |
define('BF_PLUGIN_SLUG', 'awl_blog_filter'); |
| 40 |
|
| 41 |
//Plugin Directory Path |
| 42 |
define('BF_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
| 43 |
|
| 44 |
//Plugin Directory URL |
| 45 |
define('BF_PLUGIN_URL', plugin_dir_url(__FILE__)); |
| 46 |
} // end of constructor function |
| 47 |
|
| 48 |
protected function _hooks() |
| 49 |
{ |
| 50 |
|
| 51 |
//Load text domain |
| 52 |
add_action('plugins_loaded', array($this, 'load_textdomain')); |
| 53 |
|
| 54 |
//add menu item, change menu for multisite |
| 55 |
add_action('admin_menu', array($this, 'blog_filter_menu'), 101); |
| 56 |
|
| 57 |
//Added settings link on plugins page |
| 58 |
$bf_plugin_name = plugin_basename(__FILE__); |
| 59 |
add_filter("plugin_action_links_$bf_plugin_name", array($this, 'bf_plugins_page_settings_link')); |
| 60 |
|
| 61 |
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts')); |
| 62 |
|
| 63 |
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts_in_header')); |
| 64 |
|
| 65 |
add_action('wp_ajax_load_more', array($this, 'load_more_posts')); |
| 66 |
|
| 67 |
add_action('wp_ajax_nopriv_load_more', array($this, 'load_more_posts')); |
| 68 |
|
| 69 |
add_filter('wp_img_tag_add_loading_attr', array($this, 'disable_lazy_load_for_plugin_images'), 10, 3); |
| 70 |
|
| 71 |
add_action('wp_ajax_get_taxonomies_for_post_type', array($this, 'bfg_get_taxonomies_callback')); |
| 72 |
|
| 73 |
add_action('wp_ajax_get_terms_for_taxonomy', array($this, 'bfg_get_terms_for_taxonomy_callback')); |
| 74 |
}// end of hook function |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns an array of all default shortcode attributes. |
| 78 |
* Centralized for easy maintenance. |
| 79 |
*/ |
| 80 |
|
| 81 |
|
| 82 |
public function bfg_get_taxonomies_callback() |
| 83 |
{ |
| 84 |
// First, check for security. |
| 85 |
if (!check_ajax_referer('bfg_admin_nonce', 'security', false)) { |
| 86 |
wp_send_json_error('Invalid security token.', 403); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (!current_user_can('edit_posts') && !current_user_can('manage_options')) { |
| 91 |
wp_send_json_error('Permission denied.', 403); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$post_type = isset($_POST['post_type']) ? sanitize_text_field(wp_unslash($_POST['post_type'])) : 'post'; |
| 96 |
if (empty($post_type)) { |
| 97 |
$post_type = 'post'; |
| 98 |
} |
| 99 |
|
| 100 |
// Get all public taxonomies associated with the post type. |
| 101 |
$taxonomies = get_object_taxonomies($post_type, 'objects'); |
| 102 |
|
| 103 |
$options_html = ''; |
| 104 |
|
| 105 |
if (!empty($taxonomies)) { |
| 106 |
foreach ($taxonomies as $taxonomy) { |
| 107 |
// We only want public taxonomies that can be shown in a UI. |
| 108 |
if ($taxonomy->public && $taxonomy->show_ui) { |
| 109 |
$selected = ($taxonomy->name === 'category') ? ' selected' : ''; |
| 110 |
$options_html .= '<option value="' . esc_attr($taxonomy->name) . '"' . $selected . '>' . esc_html($taxonomy->label) . ' (' . esc_html($taxonomy->name) . ')</option>'; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Check if we actually found any usable taxonomies |
| 116 |
if (empty($options_html)) { |
| 117 |
$options_html = '<option value="none">' . __('No taxonomies found for this post type', 'blog-filter') . '</option>'; |
| 118 |
} |
| 119 |
|
| 120 |
wp_send_json_success($options_html); |
| 121 |
} |
| 122 |
|
| 123 |
public function bfg_get_terms_for_taxonomy_callback() |
| 124 |
{ |
| 125 |
// Security check |
| 126 |
if (!check_ajax_referer('bfg_admin_nonce', 'security', false)) { |
| 127 |
wp_send_json_error('Invalid security token.', 403); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if (!current_user_can('edit_posts') && !current_user_can('manage_options')) { |
| 132 |
wp_send_json_error('Permission denied.', 403); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// --- Prepare data --- |
| 137 |
$taxonomy_name = isset($_POST['taxonomy']) ? sanitize_text_field(wp_unslash($_POST['taxonomy'])) : 'category'; |
| 138 |
if (empty($taxonomy_name) || $taxonomy_name === 'none') { |
| 139 |
wp_send_json_success([ |
| 140 |
'dropdown' => '<option value="all">' . __('All', 'blog-filter') . '</option>', |
| 141 |
'table' => '<p>' . __('No terms available for this taxonomy.', 'blog-filter') . '</p>', |
| 142 |
]); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
$taxonomy_obj = get_taxonomy($taxonomy_name); |
| 147 |
if (!$taxonomy_obj) { |
| 148 |
wp_send_json_error('Invalid taxonomy.', 400); |
| 149 |
return; |
| 150 |
} |
| 151 |
$terms = get_terms(['taxonomy' => $taxonomy_name, 'hide_empty' => false]); |
| 152 |
$dropdown_html = '<option value="all">' . __('All', 'blog-filter') . '</option>'; |
| 153 |
$table_html = ''; |
| 154 |
|
| 155 |
// --- Build HTML for all three elements if terms exist --- |
| 156 |
if (!is_wp_error($terms) && !empty($terms)) { |
| 157 |
// Build Dropdown HTML |
| 158 |
foreach ($terms as $term) { |
| 159 |
$dropdown_html .= '<option value="' . esc_attr($term->term_id) . '">' . esc_html($term->name) . '</option>'; |
| 160 |
} |
| 161 |
|
| 162 |
// Build Inclusion Table HTML |
| 163 |
ob_start(); |
| 164 |
?> |
| 165 |
<div class="bfg-overflow-auto" style="max-height: 415px;"> |
| 166 |
<table class="bfg-w-full bfg-border-collapse bfg-border bfg-border-gray-300"> |
| 167 |
<thead> |
| 168 |
<tr class="bfg-bg-gray-200"> |
| 169 |
<th class="bfg-p-2 bfg-border bfg-border-gray-300"><?php esc_html_e('ID', 'blog-filter'); ?></th> |
| 170 |
<th class="bfg-p-2 bfg-border bfg-border-gray-300"> |
| 171 |
<?php echo esc_html($taxonomy_obj->labels->singular_name); ?> |
| 172 |
</th> |
| 173 |
<th class="bfg-p-2 bfg-border bfg-border-gray-300"><?php esc_html_e('Post Count', 'blog-filter'); ?></th> |
| 174 |
<th class="bfg-p-2 bfg-border bfg-border-gray-300 bfg-text-center"></th> |
| 175 |
</tr> |
| 176 |
</thead> |
| 177 |
<tbody> |
| 178 |
<?php foreach ($terms as $term): ?> |
| 179 |
<tr> |
| 180 |
<td class="bfg-p-2 bfg-border bfg-border-gray-300"><?php echo esc_html($term->term_id); ?></td> |
| 181 |
<td class="bfg-p-2 bfg-border bfg-border-gray-300"><?php echo esc_html($term->name); ?></td> |
| 182 |
<td class="bfg-p-2 bfg-border bfg-border-gray-300"><?php echo esc_html($term->count); ?></td> |
| 183 |
<td class="bfg-p-2 bfg-border bfg-border-gray-300 bfg-text-center"> |
| 184 |
<input type="checkbox" class="bfg-term-checkbox" name="selected_terms[]" |
| 185 |
value="<?php echo esc_attr($term->term_id); ?>"> |
| 186 |
</td> |
| 187 |
</tr> |
| 188 |
<?php endforeach; ?> |
| 189 |
</tbody> |
| 190 |
</table> |
| 191 |
<p><b><?php esc_html_e('Note: Blog Filter Standard supports up to 4 taxonomy as filters', 'blog-filter'); ?></b></p> |
| 192 |
</div> |
| 193 |
<?php |
| 194 |
$table_html = ob_get_clean(); |
| 195 |
} else { |
| 196 |
$table_html = '<p>' . __('No terms found for this taxonomy.', 'blog-filter') . '</p>'; |
| 197 |
} |
| 198 |
|
| 199 |
// Send all three HTML strings back in a single JSON object |
| 200 |
wp_send_json_success([ |
| 201 |
'dropdown' => $dropdown_html, |
| 202 |
'table' => $table_html, |
| 203 |
]); |
| 204 |
} |
| 205 |
|
| 206 |
public function load_more_posts() |
| 207 |
{ |
| 208 |
|
| 209 |
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'load_more_nonce')) { |
| 210 |
wp_die('Invalid security token.', 403); |
| 211 |
} |
| 212 |
|
| 213 |
require(BF_PLUGIN_DIR . 'templates/blog-filter-ajax-get.php'); |
| 214 |
wp_die(); |
| 215 |
} |
| 216 |
|
| 217 |
public function enqueue_scripts_in_header() |
| 218 |
{ |
| 219 |
wp_enqueue_script('jquery'); |
| 220 |
wp_enqueue_script('imagesloaded'); |
| 221 |
} |
| 222 |
|
| 223 |
public function load_textdomain() |
| 224 |
{ |
| 225 |
load_plugin_textdomain('blog-filter', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 226 |
} |
| 227 |
|
| 228 |
public function blog_filter_menu() |
| 229 |
{ |
| 230 |
$filter_settings_menu = add_submenu_page('edit.php', __('Blog Filters Settings', 'blog-filter'), __('Blog Filters Settings', 'blog-filter'), 'administrator', 'blog-filter-settings-page', array($this, 'awl_blog_filter_page')); |
| 231 |
} |
| 232 |
|
| 233 |
public function awl_blog_filter_page() |
| 234 |
{ |
| 235 |
require_once('blog-filter-settings.php'); |
| 236 |
} |
| 237 |
|
| 238 |
public function enqueue_admin_scripts($hook_suffix) |
| 239 |
{ |
| 240 |
if (isset($_GET['page']) && $_GET['page'] === 'blog-filter-settings-page') { |
| 241 |
wp_enqueue_style('awl-blog-filter-settings-css', BF_PLUGIN_URL . 'css/blog-filter-settings.css', array(), BF_PLUGIN_VER); |
| 242 |
wp_enqueue_style('wp-color-picker'); |
| 243 |
|
| 244 |
wp_enqueue_script('jquery'); |
| 245 |
wp_enqueue_script('wp-color-picker'); |
| 246 |
wp_enqueue_script('awl-blog-filter-isotope-js', BF_PLUGIN_URL . 'js/isotope.pkgd.js', array('jquery'), BF_PLUGIN_VER, false); |
| 247 |
|
| 248 |
wp_enqueue_style('blog-filter-tailwind', BF_PLUGIN_URL . 'css/styles.min.css', [], '1.0'); |
| 249 |
|
| 250 |
// Localize nonce on jQuery so it is guaranteed to be available immediately |
| 251 |
wp_localize_script('jquery', 'bfg_admin_ajax', array( |
| 252 |
'nonce' => wp_create_nonce('bfg_admin_nonce') |
| 253 |
)); |
| 254 |
wp_localize_script('awl-blog-filter-isotope-js', 'bfg_admin_ajax', array( |
| 255 |
'nonce' => wp_create_nonce('bfg_admin_nonce') |
| 256 |
)); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
public function disable_lazy_load_for_plugin_images($value, $image, $context) |
| 261 |
{ |
| 262 |
if (strpos($image, 'portfolio_thumbnail') !== false) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
return $value; |
| 266 |
} |
| 267 |
|
| 268 |
public function bf_plugins_page_settings_link($links) |
| 269 |
{ |
| 270 |
$bf_settings_link = '<a href="edit.php?page=blog-filter-settings-page">' . esc_html__('Settings', 'blog-filter') . '</a>'; |
| 271 |
array_unshift($links, $bf_settings_link); |
| 272 |
return $links; |
| 273 |
} |
| 274 |
|
| 275 |
public function bfg_get_shortcode_defaults() |
| 276 |
{ |
| 277 |
return array( |
| 278 |
// General & Post Type Settings |
| 279 |
'post_type' => 'post', |
| 280 |
'blog_direction' => 'ltr', |
| 281 |
'blog_fixed_grid' => 'no', |
| 282 |
'blog_template' => 'template1', |
| 283 |
|
| 284 |
// Columns |
| 285 |
'blog_col_large_desktops' => 'col-lg-4', |
| 286 |
'blog_col_desktops' => 'col-md-4', |
| 287 |
'blog_col_tablets' => 'col-sm-6', |
| 288 |
'blog_col_phones' => 'col-xs-12', |
| 289 |
|
| 290 |
// Image Settings |
| 291 |
'blog_image' => 'yes', |
| 292 |
'blog_image_hover_effect' => 'none', |
| 293 |
'blog_image_quality' => 'large', |
| 294 |
|
| 295 |
// Title Settings |
| 296 |
'blog_title' => 'yes', |
| 297 |
'blog_title_font_size' => 25, |
| 298 |
'blog_title_color' => '#000', |
| 299 |
'blog_title_below_image' => 'no', |
| 300 |
|
| 301 |
// Description Settings |
| 302 |
'blog_desc' => 'yes', |
| 303 |
'blog_desc_characters' => '100', |
| 304 |
'blog_desc_font_size' => 12, |
| 305 |
'blog_desc_color' => '#606060', |
| 306 |
'blog_desc_box_color' => '#EDEEF0', |
| 307 |
'three_dots' => 'yes', |
| 308 |
|
| 309 |
// Links and Display |
| 310 |
'link_on_date' => 'no', |
| 311 |
|
| 312 |
// Read More |
| 313 |
'blog_read_more' => 'yes', |
| 314 |
'blog_read_more_text' => __('Read More', 'blog-filter'), |
| 315 |
|
| 316 |
// Metadata Display |
| 317 |
'blog_date' => 'yes', |
| 318 |
'blog_date_below_image' => 'no', |
| 319 |
'blog_author' => 'yes', |
| 320 |
'blog_author_below_image' => 'no', |
| 321 |
'blog_categories' => 'yes', |
| 322 |
'blog_tags' => 'no', |
| 323 |
|
| 324 |
// Pagination & Load |
| 325 |
'blog_pagination' => 'no', |
| 326 |
'blog_load_more' => 'no', |
| 327 |
'blog_pagination_loadmore_color' => '#58BBEE', |
| 328 |
'blog_per_page_and_init_load' => '12', |
| 329 |
'load_more_text' => __('Load More', 'blog-filter'), |
| 330 |
'no_more_text' => __('No More Posts', 'blog-filter'), |
| 331 |
|
| 332 |
// Filters |
| 333 |
'blog_filters' => 'yes', |
| 334 |
'filter_post_count' => 'no', |
| 335 |
'blog_filter_all' => 'yes', |
| 336 |
'blog_all_text' => __('All', 'blog-filter'), |
| 337 |
'blog_first_filter_selected' => 'no', |
| 338 |
|
| 339 |
// Search |
| 340 |
'blog_search' => 'no', |
| 341 |
'blog_search_text' => __('Search', 'blog-filter'), |
| 342 |
|
| 343 |
// Styling & Colors |
| 344 |
'blog_buttons_color' => '#58BBEE', |
| 345 |
|
| 346 |
// Taxonomy Filtering |
| 347 |
'blog_filtering' => 'category', |
| 348 |
|
| 349 |
'selected_terms' => '', |
| 350 |
|
| 351 |
// Custom CSS |
| 352 |
'custom_css' => '', |
| 353 |
); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
// register bf scripts |
| 358 |
function bf_register_scripts() |
| 359 |
{ |
| 360 |
|
| 361 |
//js |
| 362 |
wp_register_script('awl-bf-filterizr-js', plugin_dir_url(__FILE__) . 'js/jquery.filterizr.js', array('jquery', 'imagesloaded'), BF_PLUGIN_VER, true); |
| 363 |
|
| 364 |
// css |
| 365 |
wp_register_style('awl-bf-filter-output-css', plugin_dir_url(__FILE__) . 'css/blog-filter-output.css', array(), BF_PLUGIN_VER); |
| 366 |
wp_register_style('awl-bf-hover-css', plugin_dir_url(__FILE__) . 'css/hover.css', array(), BF_PLUGIN_VER); |
| 367 |
|
| 368 |
} |
| 369 |
add_action('wp_enqueue_scripts', 'bf_register_scripts'); |
| 370 |
|
| 371 |
// Backward compatibility wrapper for old function name |
| 372 |
if (! function_exists('awplife_bf_register_scripts')) { |
| 373 |
function awplife_bf_register_scripts() |
| 374 |
{ |
| 375 |
bf_register_scripts(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
$bf_post_filter_object = new Awl_Blog_Filter(); |
| 380 |
// Backward compatibility |
| 381 |
$pf_post_filter_object = $bf_post_filter_object; |
| 382 |
|
| 383 |
if (! function_exists('bfg_get_shortcode_defaults')) { |
| 384 |
function bfg_get_shortcode_defaults() |
| 385 |
{ |
| 386 |
global $bf_post_filter_object; |
| 387 |
return $bf_post_filter_object->bfg_get_shortcode_defaults(); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
if (! function_exists('plugins_page_settings_link')) { |
| 392 |
function plugins_page_settings_link($links) |
| 393 |
{ |
| 394 |
global $bf_post_filter_object; |
| 395 |
return $bf_post_filter_object->bf_plugins_page_settings_link($links); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
if (! function_exists('bf_plugins_page_settings_link')) { |
| 400 |
function bf_plugins_page_settings_link($links) |
| 401 |
{ |
| 402 |
global $bf_post_filter_object; |
| 403 |
return $bf_post_filter_object->bf_plugins_page_settings_link($links); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
//Shortcode page |
| 408 |
require_once('blog-filter-shortcode.php'); |
| 409 |
} ?> |