| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetch our TAXOPRESS Autolinks option. |
| 4 |
* |
| 5 |
* @return mixed |
| 6 |
*/ |
| 7 |
function taxopress_get_autolink_data() |
| 8 |
{ |
| 9 |
return array_filter((array)apply_filters('taxopress_get_autolink_data', get_option('taxopress_autolinks', []), |
| 10 |
get_current_blog_id())); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the selected autolink from the $_POST global. |
| 15 |
* |
| 16 |
* @return bool|string False on no result, sanitized autolink if set. |
| 17 |
* @internal |
| 18 |
* |
| 19 |
*/ |
| 20 |
function taxopress_get_current_autolink() |
| 21 |
{ |
| 22 |
|
| 23 |
$autolinks = false; |
| 24 |
|
| 25 |
if (!empty($_GET) && isset($_GET['taxopress_autolinks'])) { |
| 26 |
$autolinks = sanitize_text_field($_GET['taxopress_autolinks']); |
| 27 |
} else { |
| 28 |
$autolinks = taxopress_get_autolink_data(); |
| 29 |
if (!empty($autolinks)) { |
| 30 |
// Will return the first array key. |
| 31 |
$autolinks = key($autolinks); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Filters the current autolink to edit. |
| 37 |
* |
| 38 |
* @param string $autolinks autolink slug. |
| 39 |
*/ |
| 40 |
return apply_filters('taxopress_current_autolink', $autolinks); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Handle the save and deletion of autolink data. |
| 45 |
*/ |
| 46 |
function taxopress_process_autolink() |
| 47 |
{ |
| 48 |
|
| 49 |
if (wp_doing_ajax()) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if (!is_admin()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if(!current_user_can('simple_tags')){ |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if (empty($_GET)) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (!isset($_GET['page'])) { |
| 66 |
return; |
| 67 |
} |
| 68 |
if ('st_autolinks' !== $_GET['page']) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (isset($_GET['new_autolink'])) { |
| 73 |
if ((int)$_GET['new_autolink'] === 1) { |
| 74 |
add_action('admin_notices', "taxopress_autolinks_update_success_admin_notice"); |
| 75 |
add_filter('removable_query_args', 'taxopress_saved_autolink_filter_removable_query_args'); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (isset($_GET['deleted_autolink'])) { |
| 80 |
if ((int)$_GET['deleted_autolink'] === 1) { |
| 81 |
add_action('admin_notices', "taxopress_autolinks_delete_success_admin_notice"); |
| 82 |
add_filter('removable_query_args', 'taxopress_deleted_autolink_filter_removable_query_args'); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
if (!empty($_POST) && isset($_POST['autolink_submit'])) { |
| 88 |
$result = ''; |
| 89 |
if (isset($_POST['autolink_submit'])) { |
| 90 |
check_admin_referer('taxopress_addedit_autolink_nonce_action', |
| 91 |
'taxopress_addedit_autolink_nonce_field'); |
| 92 |
$result = taxopress_update_autolink($_POST); |
| 93 |
} |
| 94 |
|
| 95 |
if ($result) { |
| 96 |
wp_safe_redirect( |
| 97 |
add_query_arg( |
| 98 |
[ |
| 99 |
'page' => 'st_autolinks', |
| 100 |
'add' => 'new_item', |
| 101 |
'action' => 'edit', |
| 102 |
'taxopress_autolinks' => $result, |
| 103 |
'new_autolink' => 1, |
| 104 |
], |
| 105 |
taxopress_admin_url('admin.php') |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
exit(); |
| 110 |
} |
| 111 |
} elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autolink') { |
| 112 |
$nonce = sanitize_text_field($_REQUEST['_wpnonce']); |
| 113 |
if (wp_verify_nonce($nonce, 'autolink-action-request-nonce')) { |
| 114 |
taxopress_action_delete_autolink(sanitize_text_field($_REQUEST['taxopress_autolinks'])); |
| 115 |
} |
| 116 |
add_filter('removable_query_args', 'taxopress_delete_autolink_filter_removable_query_args'); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
add_action('admin_init', 'taxopress_process_autolink', 8); |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* Create default autolink. |
| 125 |
*/ |
| 126 |
function taxopress_create_default_autolink() |
| 127 |
{ |
| 128 |
|
| 129 |
if (wp_doing_ajax()) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if (!is_admin()) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if(!current_user_can('simple_tags')){ |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if ((int)get_option('taxopress_default_autolinks') > 0) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if (count(taxopress_get_autolink_data()) > 0) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$default = []; |
| 150 |
$default['taxopress_autolink']['title'] = 'Auto link'; |
| 151 |
$default['taxopress_autolink']['taxonomy'] = 'post_tag'; |
| 152 |
$default['taxopress_autolink']['autolink_case'] = 'none'; |
| 153 |
$default['taxopress_autolink']['autolink_display'] = 'post_content'; |
| 154 |
$default['taxopress_autolink']['autolink_title_attribute'] = __('Posts tagged with %s', 'simple-tags'); |
| 155 |
$default['taxopress_autolink']['autolink_usage_min'] = '1'; |
| 156 |
$default['taxopress_autolink']['auto_link_exclude'] = ''; |
| 157 |
$default['taxopress_autolink']['autolink_usage_max'] = '10'; |
| 158 |
$default['taxopress_autolink']['autolink_same_usage_max'] = '1'; |
| 159 |
$default['taxopress_autolink']['autolink_min_char'] = ''; |
| 160 |
$default['taxopress_autolink']['autolink_max_char'] = ''; |
| 161 |
$default['taxopress_autolink']['autolink_exclude_class'] = ''; |
| 162 |
$default['taxopress_autolink']['hook_priority'] = '12'; |
| 163 |
$default['taxopress_autolink']['embedded'] = []; |
| 164 |
$default['taxopress_autolink']['html_exclusion'] = []; |
| 165 |
$default['taxopress_autolink']['unattached_terms'] = '0'; |
| 166 |
$default['taxopress_autolink']['ignore_case'] = '1'; |
| 167 |
$default['taxopress_autolink']['ignore_attached'] = '0'; |
| 168 |
$default['taxopress_autolink']['autolink_dom'] = '1'; |
| 169 |
$default['autolink_submit'] = 'Add Auto Links'; |
| 170 |
$default['cpt_tax_status'] = 'new'; |
| 171 |
$result = taxopress_update_autolink($default); |
| 172 |
update_option('taxopress_default_autolinks', $result); |
| 173 |
} |
| 174 |
|
| 175 |
add_action('admin_init', 'taxopress_create_default_autolink', 8); |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Add to or update our TAXOPRESS option with new data. |
| 180 |
* |
| 181 |
* |
| 182 |
* @param array $data Array of autolink data to update. Optional. |
| 183 |
* @return bool|string False on failure, string on success. |
| 184 |
* @internal |
| 185 |
* |
| 186 |
*/ |
| 187 |
function taxopress_update_autolink($data = []) |
| 188 |
{ |
| 189 |
foreach ($data as $key => $value) { |
| 190 |
|
| 191 |
if (is_string($value)) { |
| 192 |
$data[$key] = sanitize_text_field($value); |
| 193 |
} else { |
| 194 |
array_map('sanitize_text_field', $data[$key]); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
$autolinks = taxopress_get_autolink_data(); |
| 199 |
|
| 200 |
$title = $data['taxopress_autolink']['title']; |
| 201 |
$title = str_replace('"', '', htmlspecialchars_decode($title)); |
| 202 |
$title = htmlspecialchars($title, ENT_QUOTES); |
| 203 |
$title = trim($title); |
| 204 |
$data['taxopress_autolink']['title'] = stripslashes_deep($title); |
| 205 |
|
| 206 |
//update seperate post |
| 207 |
$data['taxopress_autolink']['embedded'] = isset($data['embedded']) ? $data['embedded'] : []; |
| 208 |
$data['taxopress_autolink']['html_exclusion'] = isset($data['html_exclusion']) ? $data['html_exclusion'] : []; |
| 209 |
|
| 210 |
//update our custom checkbox value if not checked |
| 211 |
if (!isset($data['taxopress_autolink']['unattached_terms'])) { |
| 212 |
$data['taxopress_autolink']['unattached_terms'] = 0; |
| 213 |
} |
| 214 |
if (!isset($data['taxopress_autolink']['ignore_case'])) {//auto set ignore case to true |
| 215 |
$data['taxopress_autolink']['ignore_case'] = 1; |
| 216 |
} |
| 217 |
if (!isset($data['taxopress_autolink']['ignore_attached'])) { |
| 218 |
$data['taxopress_autolink']['ignore_attached'] = 0; |
| 219 |
} |
| 220 |
if (!isset($data['taxopress_autolink']['autolink_dom'])) { |
| 221 |
$data['taxopress_autolink']['autolink_dom'] = 0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
if (isset($data['edited_autolink'])) { |
| 226 |
$autolink_id = $data['edited_autolink']; |
| 227 |
$autolinks[$autolink_id] = $data['taxopress_autolink']; |
| 228 |
$success = update_option('taxopress_autolinks', $autolinks); |
| 229 |
//return 'update_success'; |
| 230 |
} else { |
| 231 |
$autolink_id = (int)get_option('taxopress_autolink_ids_increament') + 1; |
| 232 |
$data['taxopress_autolink']['ID'] = $autolink_id; |
| 233 |
$autolinks[$autolink_id] = $data['taxopress_autolink']; |
| 234 |
$success = update_option('taxopress_autolinks', $autolinks); |
| 235 |
$update_id = update_option('taxopress_autolink_ids_increament', $autolink_id); |
| 236 |
//return 'add_success'; |
| 237 |
} |
| 238 |
|
| 239 |
return $autolink_id; |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Successful update callback. |
| 245 |
*/ |
| 246 |
function taxopress_autolinks_update_success_admin_notice() |
| 247 |
{ |
| 248 |
echo taxopress_admin_notices_helper(__('Settings updated successfully.', 'simple-tags')); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Successful deleted callback. |
| 253 |
*/ |
| 254 |
function taxopress_autolinks_delete_success_admin_notice() |
| 255 |
{ |
| 256 |
echo taxopress_admin_notices_helper(__('Auto Links successfully deleted.', 'simple-tags'), false); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 261 |
* |
| 262 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 263 |
* |
| 264 |
* @param string[] $args Array of removable query arguments. |
| 265 |
* @return string[] Updated array of removable query arguments. |
| 266 |
*/ |
| 267 |
function taxopress_saved_autolink_filter_removable_query_args(array $args) |
| 268 |
{ |
| 269 |
return array_merge($args, [ |
| 270 |
'new_autolink', |
| 271 |
]); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 276 |
* |
| 277 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 278 |
* |
| 279 |
* @param string[] $args Array of removable query arguments. |
| 280 |
* @return string[] Updated array of removable query arguments. |
| 281 |
*/ |
| 282 |
function taxopress_deleted_autolink_filter_removable_query_args(array $args) |
| 283 |
{ |
| 284 |
return array_merge($args, [ |
| 285 |
'deleted_autolink', |
| 286 |
]); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Filters the list of query arguments which get removed from admin area URLs in WordPress. |
| 291 |
* |
| 292 |
* @link https://core.trac.wordpress.org/ticket/23367 |
| 293 |
* |
| 294 |
* @param string[] $args Array of removable query arguments. |
| 295 |
* @return string[] Updated array of removable query arguments. |
| 296 |
*/ |
| 297 |
function taxopress_delete_autolink_filter_removable_query_args(array $args) |
| 298 |
{ |
| 299 |
return array_merge($args, [ |
| 300 |
'action', |
| 301 |
'taxopress_autolinks', |
| 302 |
'_wpnonce', |
| 303 |
]); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Delete our custom autolink from the array of autolinks. |
| 308 |
* @return bool|string False on failure, string on success. |
| 309 |
*/ |
| 310 |
function taxopress_action_delete_autolink($autolink_id) |
| 311 |
{ |
| 312 |
$autolinks = taxopress_get_autolink_data(); |
| 313 |
|
| 314 |
if (array_key_exists($autolink_id, $autolinks)) { |
| 315 |
unset($autolinks[$autolink_id]); |
| 316 |
$success = update_option('taxopress_autolinks', $autolinks); |
| 317 |
} |
| 318 |
|
| 319 |
if (isset($success)) { |
| 320 |
add_action('admin_notices', "taxopress_taxdeleted_admin_notice"); |
| 321 |
wp_safe_redirect( |
| 322 |
add_query_arg( |
| 323 |
[ |
| 324 |
'page' => 'st_autolinks', |
| 325 |
'deleted_autolink' => 1, |
| 326 |
], |
| 327 |
taxopress_admin_url('admin.php') |
| 328 |
) |
| 329 |
); |
| 330 |
exit(); |
| 331 |
} |
| 332 |
} |
| 333 |
?> |