| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions. |
| 4 |
|
| 5 |
class SimpleTags_Posts |
| 6 |
{ |
| 7 |
public const MENU_SLUG = 'st_options'; |
| 8 |
|
| 9 |
// class instance |
| 10 |
public static $instance; |
| 11 |
|
| 12 |
// WP_List_Table object |
| 13 |
public $posts_table; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
* |
| 18 |
* @return void |
| 19 |
* @author Olatechpro |
| 20 |
*/ |
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
|
| 24 |
global $hook_suffix; //avoid warning outputs |
| 25 |
if (!isset($hook_suffix)) { |
| 26 |
$hook_suffix = ''; |
| 27 |
} |
| 28 |
|
| 29 |
add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3); |
| 30 |
// Admin menu |
| 31 |
add_action('admin_menu', [$this, 'admin_menu']); |
| 32 |
// Javascript |
| 33 |
add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'], 11); |
| 34 |
|
| 35 |
add_action('wp_ajax_taxopress_filter_term_search', [__CLASS__, 'handle_filter_term_search']); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Init somes JS and CSS need for this feature |
| 40 |
* |
| 41 |
* @return void |
| 42 |
* @author Olatechpro |
| 43 |
*/ |
| 44 |
public static function admin_enqueue_scripts() |
| 45 |
{ |
| 46 |
|
| 47 |
// add JS for manage click tags |
| 48 |
if (isset($_GET['page']) && $_GET['page'] == 'st_posts') { |
| 49 |
wp_enqueue_style('st-taxonomies-css'); |
| 50 |
wp_enqueue_script('admin-tags'); |
| 51 |
wp_enqueue_script('inline-edit-tax'); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Handle an ajax request to search filter available terms |
| 57 |
*/ |
| 58 |
public static function handle_filter_term_search() |
| 59 |
{ |
| 60 |
header('Content-Type: application/javascript'); |
| 61 |
|
| 62 |
if ( |
| 63 |
empty($_GET['nonce']) |
| 64 |
|| !wp_verify_nonce(sanitize_key(wp_unslash($_GET['nonce'])), 'taxopress-term-search') |
| 65 |
) { |
| 66 |
wp_send_json_error(null, 403); |
| 67 |
} |
| 68 |
|
| 69 |
if (! current_user_can('simple_tags')) { |
| 70 |
wp_send_json_error(null, 403); |
| 71 |
} |
| 72 |
|
| 73 |
$search = !empty($_GET['q']) ? sanitize_text_field(wp_unslash($_GET['q'])) : ''; |
| 74 |
$field = !empty($_GET['field']) ? sanitize_text_field(wp_unslash($_GET['field'])) : 'slug'; |
| 75 |
$filter_format = SimpleTags_Plugin::get_option_value('post_terms_filter_format'); |
| 76 |
$taxonomy_type = SimpleTags_Plugin::get_option_value('post_terms_taxonomy_type'); |
| 77 |
|
| 78 |
$terms = self::get_possible_terms_for_search($search, $taxonomy_type); |
| 79 |
$results = []; |
| 80 |
|
| 81 |
foreach ($terms as $term) { |
| 82 |
$text = $term->name; |
| 83 |
if ($filter_format === 'term_name_taxonomy_name') { |
| 84 |
$taxonomy = get_taxonomy($term->taxonomy); |
| 85 |
$text .= ' (' . $taxonomy->labels->singular_name . ')'; |
| 86 |
} elseif ($filter_format === 'term_name_taxonomy_slug') { |
| 87 |
$text .= ' (' . $term->taxonomy . ')'; |
| 88 |
} |
| 89 |
$results[] = [ |
| 90 |
'id' => $term->$field, |
| 91 |
'text' => $text, |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
$response = [ |
| 96 |
'results' => $results, |
| 97 |
]; |
| 98 |
echo wp_json_encode($response); |
| 99 |
exit; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the possible terms for a given search query. |
| 104 |
* |
| 105 |
* @param string $search Search query. |
| 106 |
* |
| 107 |
* @return object |
| 108 |
*/ |
| 109 |
public static function get_possible_terms_for_search($search, $taxonomy_type = 'public') |
| 110 |
{ |
| 111 |
if ($taxonomy_type === 'public') { |
| 112 |
$taxonomies = array_keys(get_taxonomies(['public' => true])); |
| 113 |
} elseif ($taxonomy_type === 'private') { |
| 114 |
$taxonomies = array_keys(get_taxonomies(['public' => false])); |
| 115 |
} else { |
| 116 |
$taxonomies = array_keys(get_taxonomies()); |
| 117 |
} |
| 118 |
|
| 119 |
$term_args = [ |
| 120 |
'taxonomy' => $taxonomies, |
| 121 |
'hide_empty' => true, |
| 122 |
'number' => apply_filters('taxopress_terms_search_result_limit', 20), |
| 123 |
'order_by' => 'name', |
| 124 |
]; |
| 125 |
|
| 126 |
if (!empty($search)) { |
| 127 |
$search = str_replace(['\"', "\'"], '', $search); |
| 128 |
$term_args['search'] = $search; |
| 129 |
} |
| 130 |
|
| 131 |
$terms = get_terms($term_args); |
| 132 |
|
| 133 |
return $terms; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static function set_screen($status, $option, $value) |
| 139 |
{ |
| 140 |
return $value; |
| 141 |
} |
| 142 |
|
| 143 |
/** Singleton instance */ |
| 144 |
public static function get_instance() |
| 145 |
{ |
| 146 |
if (!isset(self::$instance)) { |
| 147 |
self::$instance = new self(); |
| 148 |
} |
| 149 |
|
| 150 |
return self::$instance; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Add WP admin menu for Tags |
| 155 |
* |
| 156 |
* @return void |
| 157 |
* @author Olatechpro |
| 158 |
*/ |
| 159 |
public function admin_menu() |
| 160 |
{ |
| 161 |
$hook = add_submenu_page( |
| 162 |
self::MENU_SLUG, |
| 163 |
esc_html__('Posts', 'simple-tags'), |
| 164 |
esc_html__('Posts', 'simple-tags'), |
| 165 |
'simple_tags', |
| 166 |
'st_posts', |
| 167 |
[ |
| 168 |
$this, |
| 169 |
'page_manage_posts', |
| 170 |
] |
| 171 |
); |
| 172 |
|
| 173 |
if (taxopress_is_screen_main_page()) { |
| 174 |
add_action("load-$hook", [$this, 'screen_option']); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Screen options |
| 180 |
*/ |
| 181 |
public function screen_option() |
| 182 |
{ |
| 183 |
|
| 184 |
$option = 'per_page'; |
| 185 |
$args = [ |
| 186 |
'label' => esc_html__('Number of items per page', 'simple-tags'), |
| 187 |
'default' => 20, |
| 188 |
'option' => 'st_posts_per_page' |
| 189 |
]; |
| 190 |
|
| 191 |
add_screen_option($option, $args); |
| 192 |
|
| 193 |
$this->posts_table = new Taxopress_Posts_List(); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Method for build the page HTML manage tags |
| 198 |
* |
| 199 |
* @return void |
| 200 |
* @author Olatechpro |
| 201 |
*/ |
| 202 |
public function page_manage_posts() |
| 203 |
{ |
| 204 |
// Default order |
| 205 |
if (!isset($_GET['order'])) { |
| 206 |
$_GET['order'] = 'name-asc'; |
| 207 |
} |
| 208 |
|
| 209 |
settings_errors(__CLASS__); |
| 210 |
|
| 211 |
?> |
| 212 |
<div class="wrap st_wrap st-manage-taxonomies-page manage-taxopress-posts"> |
| 213 |
|
| 214 |
<div id=""> |
| 215 |
<h1 class="wp-heading-inline"><?php esc_html_e('Posts', 'simple-tags'); ?></h1> |
| 216 |
<div class="taxopress-description"> |
| 217 |
<?php esc_html_e('This feature allows you to search for terms and see all the posts attached to that term.', 'simple-tags'); ?> |
| 218 |
</div> |
| 219 |
|
| 220 |
|
| 221 |
<?php |
| 222 |
if (isset($_REQUEST['s']) && $search = esc_attr(sanitize_text_field(wp_unslash($_REQUEST['s'])))) { |
| 223 |
echo '<span class="subtitle__">'; |
| 224 |
printf( |
| 225 |
/* translators: %s: Search query. */ |
| 226 |
esc_html__('Search results for: %s'), |
| 227 |
'<strong>' . esc_html($search) . '</strong>' |
| 228 |
); |
| 229 |
echo '</span>'; |
| 230 |
} |
| 231 |
?> |
| 232 |
<?php |
| 233 |
|
| 234 |
//the posts table instance |
| 235 |
$this->posts_table->prepare_items(); |
| 236 |
?> |
| 237 |
|
| 238 |
|
| 239 |
<hr class="wp-header-end"> |
| 240 |
<div id="ajax-response"></div> |
| 241 |
<form class="search-form wp-clearfix st-taxonomies-search-form" method="get"> |
| 242 |
<?php $this->posts_table->search_box(esc_html__('Search Posts', 'simple-tags'), 'post'); ?> |
| 243 |
</form> |
| 244 |
<div class="clear"></div> |
| 245 |
|
| 246 |
<div id="col-container" class="wp-clearfix"> |
| 247 |
|
| 248 |
<div class="col-wrap"> |
| 249 |
<form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post"> |
| 250 |
<?php $this->posts_table->display(); //Display the table |
| 251 |
?> |
| 252 |
</form> |
| 253 |
<div class="form-wrap edit-post-notes"> |
| 254 |
<p><?php esc_html__('Description here.', 'simple-tags') ?></p> |
| 255 |
</div> |
| 256 |
</div> |
| 257 |
|
| 258 |
|
| 259 |
</div> |
| 260 |
|
| 261 |
|
| 262 |
</div> |
| 263 |
<?php $this->posts_table->inline_edit(); ?> |
| 264 |
<?php SimpleTags_Admin::printAdminFooter(); ?> |
| 265 |
</div> |
| 266 |
<?php |
| 267 |
} |
| 268 |
} |
| 269 |
|