| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetch our TAXOPRESS Autoterms option. |
| 4 |
* |
| 5 |
* @return mixed |
| 6 |
*/ |
| 7 |
function taxopress_get_autoterm_data() |
| 8 |
{ |
| 9 |
return array_filter((array)apply_filters('taxopress_get_autoterm_data', get_option('taxopress_autoterms', []), |
| 10 |
get_current_blog_id())); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the selected autoterm from the $_POST global. |
| 15 |
* |
| 16 |
* @return bool|string False on no result, sanitized autoterm if set. |
| 17 |
* @internal |
| 18 |
* |
| 19 |
*/ |
| 20 |
function taxopress_get_current_autoterm() |
| 21 |
{ |
| 22 |
|
| 23 |
$autoterms = false; |
| 24 |
|
| 25 |
if (!empty($_GET) && isset($_GET['taxopress_autoterms'])) { |
| 26 |
$autoterms = sanitize_text_field($_GET['taxopress_autoterms']); |
| 27 |
} else { |
| 28 |
$autoterms = taxopress_get_autoterm_data(); |
| 29 |
if (!empty($autoterms)) { |
| 30 |
// Will return the first array key. |
| 31 |
$autoterms = key($autoterms); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Filters the current autoterm to edit. |
| 37 |
* |
| 38 |
* @param string $autoterms autoterm slug. |
| 39 |
*/ |
| 40 |
return apply_filters('taxopress_current_autoterm', $autoterms); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle the save and deletion of autoterm data. |
| 45 |
*/ |
| 46 |
function taxopress_process_autoterm() |
| 47 |
{ |
| 48 |
|
| 49 |
if (wp_doing_ajax()) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (!is_admin()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if (empty($_GET)) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if (!isset($_GET['page'])) { |
| 62 |
return; |
| 63 |
} |
| 64 |
if ('st_autoterms' !== $_GET['page']) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if (isset($_GET['new_autoterm'])) { |
| 69 |
if ((int)$_GET['new_autoterm'] === 1) { |
| 70 |
add_action('admin_notices', "taxopress_autoterms_update_success_admin_notice"); |
| 71 |
add_filter('removable_query_args', 'taxopress_saved_autoterm_filter_removable_query_args'); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if (isset($_GET['deleted_autoterm'])) { |
| 76 |
if ((int)$_GET['deleted_autoterm'] === 1) { |
| 77 |
add_action('admin_notices', "taxopress_autoterms_delete_success_admin_notice"); |
| 78 |
add_filter('removable_query_args', 'taxopress_deleted_autoterm_filter_removable_query_args'); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
if (!empty($_POST) && isset($_POST['autoterm_submit'])) { |
| 84 |
$result = ''; |
| 85 |
if (isset($_POST['autoterm_submit'])) { |
| 86 |
check_admin_referer('taxopress_addedit_autoterm_nonce_action', |
| 87 |
'taxopress_addedit_autoterm_nonce_field'); |
| 88 |
$result = taxopress_update_autoterm($_POST); |
| 89 |
} |
| 90 |
|
| 91 |
if ($result) { |
| 92 |
wp_safe_redirect( |
| 93 |
add_query_arg( |
| 94 |
[ |
| 95 |
'page' => 'st_autoterms', |
| 96 |
'add' => 'new_item', |
| 97 |
'action' => 'edit', |
| 98 |
'taxopress_autoterms' => $result, |
| 99 |
'new_autoterm' => 1, |
| 100 |
], |
| 101 |
taxopress_admin_url('admin.php') |
| 102 |
) |
| 103 |
); |
| 104 |
|
| 105 |
exit(); |
| 106 |
} |
| 107 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm') { |
| 108 |
$nonce = esc_attr($_REQUEST['_wpnonce']); |
| 109 |
if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) { |
| 110 |
taxopress_action_delete_autoterm($_REQUEST['taxopress_autoterms']); |
| 111 |
} |
| 112 |
add_filter('removable_query_args', 'taxopress_delete_autoterm_filter_removable_query_args'); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
add_action('init', 'taxopress_process_autoterm', 8); |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Create default autoterm. |
| 121 |
*/ |
| 122 |
function taxopress_create_default_autoterm() |
| 123 |
{ |
| 124 |
|
| 125 |
if (wp_doing_ajax()) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (!is_admin()) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if ((int)get_option('taxopress_default_autoterms') > 0) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if (count(taxopress_get_autoterm_data()) > 0) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
//create default or export previous |
| 142 |
$create_default = true; |
| 143 |
|
| 144 |
$options = get_option( STAGS_OPTIONS_NAME_AUTO ); |
| 145 |
if(is_array($options) && count($options) > 0 ){ |
| 146 |
foreach($options as $options_post_type => $options_taxdata){ |
| 147 |
if(is_array($options_taxdata) && count($options_taxdata) > 0){ |
| 148 |
$create_default = false; |
| 149 |
foreach($options_taxdata as $options_taxonomy => $options_taxonomy_data){ |
| 150 |
$taxonomy = get_taxonomy($options_taxonomy); |
| 151 |
$default = []; |
| 152 |
$default['taxopress_autoterm']['autoterm_from'] = 'posts'; |
| 153 |
$default['taxopress_autoterm']['title'] = ''. (is_object($taxonomy) ? $taxonomy->labels->name : $options_taxonomy ) .' '. ucwords($options_post_type) .' Auto term'; |
| 154 |
$default['taxopress_autoterm']['taxonomy'] = $options_taxonomy; |
| 155 |
$default['post_types'] = [$options_post_type]; |
| 156 |
$default['taxopress_autoterm']['autoterm_useall'] = isset($options_taxonomy_data['at_all']) ? $options_taxonomy_data['at_all'] : 0; |
| 157 |
$default['taxopress_autoterm']['autoterm_useonly'] = isset($options_taxonomy_data['at_all_no']) ? $options_taxonomy_data['at_all_no'] : 0; |
| 158 |
$default['taxopress_autoterm']['autoterm_target'] = isset($options_taxonomy_data['at_empty']) ? $options_taxonomy_data['at_empty'] : 0; |
| 159 |
$default['taxopress_autoterm']['autoterm_word'] = isset($options_taxonomy_data['only_full_word']) ? $options_taxonomy_data['only_full_word'] : 0; |
| 160 |
$default['taxopress_autoterm']['autoterm_hash'] = isset($options_taxonomy_data['allow_hashtag_format']) ? $options_taxonomy_data['allow_hashtag_format'] : 0; |
| 161 |
$default['specific_terms'] = isset($options_taxonomy_data['auto_list']) ? (array) maybe_unserialize($options_taxonomy_data['auto_list']) : []; |
| 162 |
$default['taxopress_autoterm']['terms_limit'] = '0'; |
| 163 |
|
| 164 |
$result = taxopress_update_autoterm($default); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if($create_default){ |
| 171 |
$default = []; |
| 172 |
$default['taxopress_autoterm']['title'] = 'Auto term'; |
| 173 |
$default['taxopress_autoterm']['taxonomy'] = 'post_tag'; |
| 174 |
$default['post_types'] = ['post']; |
| 175 |
$default['taxopress_autoterm']['autoterm_from'] = 'posts'; |
| 176 |
$default['taxopress_autoterm']['autoterm_useall'] = '0'; |
| 177 |
$default['taxopress_autoterm']['autoterm_useall'] = '0'; |
| 178 |
$default['taxopress_autoterm']['autoterm_useonly'] = '0'; |
| 179 |
$default['taxopress_autoterm']['autoterm_target'] = '0'; |
| 180 |
$default['taxopress_autoterm']['autoterm_word'] = '0'; |
| 181 |
$default['taxopress_autoterm']['autoterm_hash'] = '0'; |
| 182 |
$default['taxopress_autoterm']['terms_limit'] = '0'; |
| 183 |
$default['specific_terms'] = []; |
| 184 |
$result = taxopress_update_autoterm($default); |
| 185 |
} |
| 186 |
update_option('taxopress_default_autoterms', $result); |
| 187 |
} |
| 188 |
|
| 189 |
add_action('init', 'taxopress_create_default_autoterm', 8); |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Add to or update our TAXOPRESS option with new data. |
| 194 |
* |
| 195 |
* |
| 196 |
* @param array $data Array of autoterm data to update. Optional. |
| 197 |
* @return bool|string False on failure, string on success. |
| 198 |
* @internal |
| 199 |
* |
| 200 |
*/ |
| 201 |
function taxopress_update_autoterm($data = []) |
| 202 |
{ |
| 203 |
foreach ($data as $key => $value) { |
| 204 |
|
| 205 |
if (is_string($value)) { |
| 206 |
$data[$key] = sanitize_text_field($value); |
| 207 |
} else { |
| 208 |
array_map('sanitize_text_field', $data[$key]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
$autoterms = taxopress_get_autoterm_data(); |
| 213 |
|
| 214 |
$title = $data['taxopress_autoterm']['title']; |
| 215 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 216 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 217 |
$title = trim($title); |
| 218 |
$data['taxopress_autoterm']['title'] = stripslashes_deep($title); |
| 219 |
|
| 220 |
|
| 221 |
//update other post post |
| 222 |
$data['taxopress_autoterm']['specific_terms'] = isset($data['specific_terms']) ? $data['specific_terms'] : ''; |
| 223 |
$data['taxopress_autoterm']['post_types'] = isset($data['post_types']) ? $data['post_types'] : []; |
| 224 |
|
| 225 |
//update our custom checkbox value if not checked |
| 226 |
if (!isset($data['taxopress_autoterm']['autoterm_useall'])) { |
| 227 |
$data['taxopress_autoterm']['autoterm_useall'] = 0; |
| 228 |
} |
| 229 |
if (!isset($data['taxopress_autoterm']['autoterm_useonly'])) { |
| 230 |
$data['taxopress_autoterm']['autoterm_useonly'] = 0; |
| 231 |
} |
| 232 |
if (!isset($data['taxopress_autoterm']['autoterm_target'])) { |
| 233 |
$data['taxopress_autoterm']['autoterm_target'] = 0; |
| 234 |
} |
| 235 |
if (!isset($data['taxopress_autoterm']['autoterm_word'])) { |
| 236 |
$data['taxopress_autoterm']['autoterm_word'] = 0; |
| 237 |
} |
| 238 |
if (!isset($data['taxopress_autoterm']['autoterm_hash'])) { |
| 239 |
$data['taxopress_autoterm']['autoterm_hash'] = 0; |
| 240 |
} |
| 241 |
|
| 242 |
if (isset($data['edited_autoterm'])) { |
| 243 |
$autoterm_id = $data['edited_autoterm']; |
| 244 |
$autoterms[$autoterm_id] = $data['taxopress_autoterm']; |
| 245 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 246 |
//return 'update_success'; |
| 247 |
} else { |
| 248 |
$autoterm_id = (int)get_option('taxopress_autoterm_ids_increament') + 1; |
| 249 |
$data['taxopress_autoterm']['ID'] = $autoterm_id; |
| 250 |
$autoterms[$autoterm_id] = $data['taxopress_autoterm']; |
| 251 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 252 |
$update_id = update_option('taxopress_autoterm_ids_increament', $autoterm_id); |
| 253 |
//return 'add_success'; |
| 254 |
} |
| 255 |
|
| 256 |
return $autoterm_id; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Successful update callback. |
| 261 |
*/ |
| 262 |
function taxopress_autoterms_update_success_admin_notice() |
| 263 |
{ |
| 264 |
echo taxopress_admin_notices_helper(__('Settings updated successfully.', 'simple-tags')); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Successful deleted callback. |
| 269 |
*/ |
| 270 |
function taxopress_autoterms_delete_success_admin_notice() |
| 271 |
{ |
| 272 |
echo taxopress_admin_notices_helper(__('Auto terms successfully deleted.', 'simple-tags'), false); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 277 |
* |
| 278 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 279 |
* |
| 280 |
* @param string[] $args Array of removable query arguments. |
| 281 |
* @return string[] Updated array of removable query arguments. |
| 282 |
*/ |
| 283 |
function taxopress_saved_autoterm_filter_removable_query_args(array $args) |
| 284 |
{ |
| 285 |
return array_merge($args, [ |
| 286 |
'new_autoterm', |
| 287 |
]); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 292 |
* |
| 293 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 294 |
* |
| 295 |
* @param string[] $args Array of removable query arguments. |
| 296 |
* @return string[] Updated array of removable query arguments. |
| 297 |
*/ |
| 298 |
function taxopress_deleted_autoterm_filter_removable_query_args(array $args) |
| 299 |
{ |
| 300 |
return array_merge($args, [ |
| 301 |
'deleted_autoterm', |
| 302 |
]); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 307 |
* |
| 308 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 309 |
* |
| 310 |
* @param string[] $args Array of removable query arguments. |
| 311 |
* @return string[] Updated array of removable query arguments. |
| 312 |
*/ |
| 313 |
function taxopress_delete_autoterm_filter_removable_query_args(array $args) |
| 314 |
{ |
| 315 |
return array_merge($args, [ |
| 316 |
'action', |
| 317 |
'taxopress_autoterms', |
| 318 |
'_wpnonce', |
| 319 |
]); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Delete our custom autoterm from the array of autoterms. |
| 324 |
* @return bool|string False on failure, string on success. |
| 325 |
*/ |
| 326 |
function taxopress_action_delete_autoterm($autoterm_id) |
| 327 |
{ |
| 328 |
$autoterms = taxopress_get_autoterm_data(); |
| 329 |
|
| 330 |
if (array_key_exists($autoterm_id, $autoterms)) { |
| 331 |
unset($autoterms[$autoterm_id]); |
| 332 |
$success = update_option('taxopress_autoterms', $autoterms); |
| 333 |
} |
| 334 |
|
| 335 |
if (isset($success)) { |
| 336 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 337 |
wp_safe_redirect( |
| 338 |
add_query_arg( |
| 339 |
[ |
| 340 |
'page' => 'st_autoterms', |
| 341 |
'deleted_autoterm' => 1, |
| 342 |
], |
| 343 |
taxopress_admin_url('admin.php') |
| 344 |
) |
| 345 |
); |
| 346 |
exit(); |
| 347 |
} |
| 348 |
} |
| 349 |
?> |