| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions. |
| 4 |
|
| 5 |
class SimpleTags_Admin_Post_Settings |
| 6 |
{ |
| 7 |
private const STATUS_DEFAULT = 'default'; |
| 8 |
private const STATUS_ENABLED = 'enabled'; |
| 9 |
private const STATUS_DISABLED = 'disabled'; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @return void |
| 15 |
* @author WebFactory Ltd |
| 16 |
*/ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
|
| 20 |
if ((1 === (int) SimpleTags_Plugin::get_option_value('active_auto_links')) || (1 === (int) SimpleTags_Plugin::get_option_value('active_auto_terms'))) { |
| 21 |
// Save tags from advanced input |
| 22 |
add_action('save_post', array( __CLASS__, 'save_post' ), 10, 1); |
| 23 |
// Box for advanced tags |
| 24 |
add_action('add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ), 10, 1); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register a new box for TaxoPress settings |
| 30 |
* |
| 31 |
* @param string $post_type |
| 32 |
* |
| 33 |
* @return void |
| 34 |
* @author WebFactory Ltd |
| 35 |
*/ |
| 36 |
public static function add_meta_boxes($post_type) |
| 37 |
{ |
| 38 |
|
| 39 |
$autoterm = taxopress_post_type_autoterms(); |
| 40 |
$autolink = taxopress_post_type_autolink_autolink(); |
| 41 |
|
| 42 |
if (!is_array($autoterm) && !is_array($autolink)) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// Auto terms for this CPT ? |
| 47 |
add_meta_box('simpletags-settings', __('TaxoPress', 'simple-tags'), array( |
| 48 |
__CLASS__, |
| 49 |
'metabox' |
| 50 |
), $post_type, 'side', 'low'); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Build HTML of form |
| 55 |
* |
| 56 |
* @param object $post |
| 57 |
* |
| 58 |
* @return void |
| 59 |
* @author WebFactory Ltd |
| 60 |
*/ |
| 61 |
public static function metabox($post) |
| 62 |
{ |
| 63 |
if (! isset($post->post_type)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
wp_nonce_field('taxopress_post_settings', 'taxopress_post_settings_nonce'); |
| 68 |
|
| 69 |
// Auto terms for this CPT ? |
| 70 |
if (1 === (int) SimpleTags_Plugin::get_option_value('active_auto_terms')) { |
| 71 |
$meta_value = self::get_status($post->ID, '_taxopress_autoterms_status', '_exclude_autotags'); |
| 72 |
echo '<p>' . "\n"; |
| 73 |
echo '<label for="taxopress_autoterms_status">' . esc_html__('Auto Terms', 'simple-tags') . '</label><br />' . "\n"; |
| 74 |
echo '<select id="taxopress_autoterms_status" name="taxopress_autoterms_status">' . "\n"; |
| 75 |
self::status_options($meta_value); |
| 76 |
echo '</select>' . "\n"; |
| 77 |
echo '</p>' . "\n"; |
| 78 |
echo '<input type="hidden" name="_meta_autotags" value="true" />'; |
| 79 |
} |
| 80 |
|
| 81 |
if (1 === (int) SimpleTags_Plugin::get_option_value('active_auto_links')) { |
| 82 |
$meta_value = self::get_status($post->ID, '_taxopress_autolinks_status', '_exclude_autolinks'); |
| 83 |
echo '<p>' . "\n"; |
| 84 |
echo '<label for="taxopress_autolinks_status">' . esc_html__('Auto Links', 'simple-tags') . '</label><br />' . "\n"; |
| 85 |
echo '<select id="taxopress_autolinks_status" name="taxopress_autolinks_status">' . "\n"; |
| 86 |
self::status_options($meta_value); |
| 87 |
echo '</select>' . "\n"; |
| 88 |
echo '</p>' . "\n"; |
| 89 |
echo '<input type="hidden" name="_meta_autolink" value="true" />'; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Save this settings in post meta, delete if no exclude, clean DB :) |
| 95 |
* |
| 96 |
* @param integer $object_id |
| 97 |
* |
| 98 |
* @return void |
| 99 |
* @author WebFactory Ltd |
| 100 |
*/ |
| 101 |
public static function save_post($object_id = 0) |
| 102 |
{ |
| 103 |
if ( |
| 104 |
! isset($_POST['taxopress_post_settings_nonce']) |
| 105 |
|| ! wp_verify_nonce( |
| 106 |
sanitize_text_field(wp_unslash($_POST['taxopress_post_settings_nonce'])), |
| 107 |
'taxopress_post_settings' |
| 108 |
) |
| 109 |
) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || wp_is_post_revision($object_id)) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
if (! current_user_can('edit_post', $object_id)) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
$has_autotags_meta = isset($_POST['_meta_autotags']) && 'true' === sanitize_text_field(wp_unslash($_POST['_meta_autotags'])); |
| 122 |
$has_autolink_meta = isset($_POST['_meta_autolink']) && 'true' === sanitize_text_field(wp_unslash($_POST['_meta_autolink'])); |
| 123 |
|
| 124 |
if ($has_autotags_meta) { |
| 125 |
self::save_status($object_id, 'taxopress_autoterms_status', '_taxopress_autoterms_status', '_exclude_autotags'); |
| 126 |
} |
| 127 |
|
| 128 |
if ($has_autolink_meta) { |
| 129 |
self::save_status($object_id, 'taxopress_autolinks_status', '_taxopress_autolinks_status', '_exclude_autolinks'); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private static function get_status($post_id, $status_meta_key, $legacy_disable_meta_key = '') |
| 134 |
{ |
| 135 |
$status = get_post_meta($post_id, $status_meta_key, true); |
| 136 |
|
| 137 |
if (self::is_valid_status($status)) { |
| 138 |
return $status; |
| 139 |
} |
| 140 |
|
| 141 |
if ($legacy_disable_meta_key && get_post_meta($post_id, $legacy_disable_meta_key, true)) { |
| 142 |
return self::STATUS_DISABLED; |
| 143 |
} |
| 144 |
|
| 145 |
return self::STATUS_DEFAULT; |
| 146 |
} |
| 147 |
|
| 148 |
private static function save_status($post_id, $post_key, $status_meta_key, $legacy_disable_meta_key = '') |
| 149 |
{ |
| 150 |
$status = isset($_POST[$post_key]) ? sanitize_key(wp_unslash($_POST[$post_key])) : self::STATUS_DEFAULT; |
| 151 |
|
| 152 |
if (! self::is_valid_status($status)) { |
| 153 |
$status = self::STATUS_DEFAULT; |
| 154 |
} |
| 155 |
|
| 156 |
if (self::STATUS_DEFAULT === $status) { |
| 157 |
delete_post_meta($post_id, $status_meta_key); |
| 158 |
} else { |
| 159 |
update_post_meta($post_id, $status_meta_key, $status); |
| 160 |
} |
| 161 |
|
| 162 |
if ($legacy_disable_meta_key) { |
| 163 |
if (self::STATUS_DISABLED === $status) { |
| 164 |
update_post_meta($post_id, $legacy_disable_meta_key, true); |
| 165 |
} else { |
| 166 |
delete_post_meta($post_id, $legacy_disable_meta_key); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private static function status_options($current_status) |
| 172 |
{ |
| 173 |
$statuses = [ |
| 174 |
self::STATUS_DEFAULT => esc_html__('Default', 'simple-tags'), |
| 175 |
self::STATUS_ENABLED => esc_html__('Enable', 'simple-tags'), |
| 176 |
self::STATUS_DISABLED => esc_html__('Disable', 'simple-tags'), |
| 177 |
]; |
| 178 |
|
| 179 |
foreach ($statuses as $status => $label) { |
| 180 |
echo '<option value="' . esc_attr($status) . '"' . selected($current_status, $status, false) . '>' . esc_html($label) . '</option>' . "\n"; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
private static function is_valid_status($status) |
| 185 |
{ |
| 186 |
return in_array($status, [self::STATUS_DEFAULT, self::STATUS_ENABLED, self::STATUS_DISABLED], true); |
| 187 |
} |
| 188 |
} |
| 189 |
|