| 1 |
<?php |
| 2 |
/** |
| 3 |
* Represents the view for the administration dashboard. |
| 4 |
* |
| 5 |
* This includes the header, options, and other information that should provide |
| 6 |
* The User Interface to the end user. |
| 7 |
* |
| 8 |
* @author Michael Schoenrock <hello@michaelschoenrock.com>, Sven Hofmann <info@hofmannsven.com> |
| 9 |
* @license GPL-2.0+ |
| 10 |
*/ |
| 11 |
|
| 12 |
// If this file is called directly, abort. |
| 13 |
if (!defined('WPINC')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Declare default colors. |
| 19 |
* |
| 20 |
* @since 1.0.1 |
| 21 |
*/ |
| 22 |
function mark_posts_get_default_colors() |
| 23 |
{ |
| 24 |
$default_colors = ['#96D754', '#FFFA74', '#FF7150', '#9ABADC', '#FFA74C', '#158A61']; |
| 25 |
|
| 26 |
return $default_colors; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get marker terms. |
| 31 |
* |
| 32 |
* @since 1.0.1 |
| 33 |
*/ |
| 34 |
function mark_posts_get_marker_terms() |
| 35 |
{ |
| 36 |
$marker_terms = get_terms('marker', 'orderby=id&hide_empty=0'); |
| 37 |
|
| 38 |
return $marker_terms; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Misc functions. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @updated 1.0.8 |
| 46 |
*/ |
| 47 |
function mark_posts_misc_functions() |
| 48 |
{ |
| 49 |
|
| 50 |
// mark all posts |
| 51 |
if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['mark-all-posts-term-id'])) { |
| 52 |
$term_id = $_GET['mark-all-posts-term-id']; /* TODO:: SECURITY */ |
| 53 |
|
| 54 |
// set color only for selected post types |
| 55 |
$get_mark_posts_settings = get_option('mark_posts_settings'); |
| 56 |
foreach ($get_mark_posts_settings['mark_posts_posttypes'] as $post_type) { |
| 57 |
$args = [ |
| 58 |
'posts_per_page' => -1, |
| 59 |
'post_type' => $post_type, ]; |
| 60 |
|
| 61 |
// get all posts |
| 62 |
$all_posts = get_posts($args); |
| 63 |
|
| 64 |
foreach ($all_posts as $post) { |
| 65 |
// Sanitize the user input. |
| 66 |
$mydata = sanitize_text_field($term_id); |
| 67 |
$myterm = get_term($term_id, 'marker'); |
| 68 |
|
| 69 |
// Update the meta field. |
| 70 |
update_post_meta($post->ID, 'mark_posts_term_id', $mydata); |
| 71 |
// Update taxonomy count |
| 72 |
wp_set_object_terms($post->ID, $myterm->name, 'marker'); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
echo mark_posts_display_settings_updated(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Save form data. |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
*/ |
| 85 |
function mark_posts_validate_form() |
| 86 |
{ |
| 87 |
|
| 88 |
// get default colors |
| 89 |
$default_colors = mark_posts_get_default_colors(); |
| 90 |
|
| 91 |
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])) { |
| 92 |
|
| 93 |
// get marker posttypes |
| 94 |
if (isset($_POST['markertypes'])) { |
| 95 |
$markertypes = $_POST['markertypes']; |
| 96 |
} else { |
| 97 |
$markertypes = []; |
| 98 |
} |
| 99 |
|
| 100 |
// update post type settings |
| 101 |
$get_mark_posts_settings = get_option('mark_posts_settings'); |
| 102 |
$set_mark_posts_settings = $markertypes; |
| 103 |
$get_mark_posts_settings['mark_posts_posttypes'] = $set_mark_posts_settings; |
| 104 |
|
| 105 |
update_option('mark_posts_settings', $get_mark_posts_settings); |
| 106 |
|
| 107 |
// get marker dashboard |
| 108 |
if (isset($_POST['markerdashboard'])) { |
| 109 |
$markerdashboard = $_POST['markerdashboard']; |
| 110 |
} else { |
| 111 |
$markerdashboard = []; |
| 112 |
} |
| 113 |
|
| 114 |
// update dashboard settings |
| 115 |
$get_mark_posts_settings = get_option('mark_posts_settings'); |
| 116 |
$set_mark_posts_settings = $markerdashboard; |
| 117 |
$get_mark_posts_settings['mark_posts_dashboard'] = $set_mark_posts_settings; |
| 118 |
|
| 119 |
update_option('mark_posts_settings', $get_mark_posts_settings); |
| 120 |
|
| 121 |
// news markers |
| 122 |
$markers = explode(',', $_POST['markers']); |
| 123 |
$count_markers = count(mark_posts_get_marker_terms()); |
| 124 |
if ($count_markers) { |
| 125 |
$i = $count_markers; |
| 126 |
} // define $i for default color |
| 127 |
else { |
| 128 |
$i = 0; |
| 129 |
} |
| 130 |
foreach ($markers as $marker) { |
| 131 |
$marker = trim($marker); |
| 132 |
$color = $default_colors[$i]; // define default color |
| 133 |
wp_insert_term($marker, 'marker', [ |
| 134 |
'name' => $marker, |
| 135 |
'slug' => sanitize_title($marker), |
| 136 |
'description' => $color, |
| 137 |
]); |
| 138 |
if ($i > 5) { |
| 139 |
$i = 0; |
| 140 |
} // reset $i to color count so the next color is first color again etc. |
| 141 |
else { |
| 142 |
$i++; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// update markers |
| 147 |
$i = 0; |
| 148 |
if (isset($_POST['markernames'])) { |
| 149 |
foreach ($_POST['markernames'] as $markername) { |
| 150 |
wp_update_term($_POST['term_ids'][$i], 'marker', [ |
| 151 |
'name' => $markername, |
| 152 |
'slug' => sanitize_title($markername), |
| 153 |
'description' => $_POST['colors'][$i], |
| 154 |
]); |
| 155 |
$i++; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// delete markers |
| 160 |
if (isset($_POST['delete'])) { |
| 161 |
foreach ($_POST['delete'] as $term_id) { |
| 162 |
wp_delete_term($term_id, 'marker'); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
echo mark_posts_display_settings_updated(); |
| 167 |
|
| 168 |
// Clear transient dashboard stats |
| 169 |
delete_transient('marker_posts_stats'); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Show update notice. |
| 175 |
* |
| 176 |
* @since 1.0.0 |
| 177 |
*/ |
| 178 |
function mark_posts_display_settings_updated() |
| 179 |
{ |
| 180 |
return '<div id="message" class="updated"><p>'.__('Settings saved', 'mark-posts').'</p></div>'; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* List of excluded post types. |
| 185 |
* |
| 186 |
* @since 1.2.1 |
| 187 |
* @docs https://github.com/hofmannsven/mark-posts/wiki/Reset-Custom-Post-Types |
| 188 |
* |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
function mark_posts_excluded_post_types() |
| 192 |
{ |
| 193 |
return apply_filters('mark_posts_excluded_post_types', [ |
| 194 |
'attachment', |
| 195 |
'revision', |
| 196 |
'nav_menu_item', |
| 197 |
'custom_css', |
| 198 |
'customize_changeset', |
| 199 |
'oembed_cache', |
| 200 |
'user_request', |
| 201 |
'wp_block', |
| 202 |
'tcb_symbol', |
| 203 |
'td_nm_notification', |
| 204 |
'tve_lead_group', |
| 205 |
'tve_lead_shortcode', |
| 206 |
'tve_lead_2s_lightbox', |
| 207 |
'tve_lead_1c_signup', |
| 208 |
'tve_form_type', |
| 209 |
'tcb_content_template', |
| 210 |
]); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get the readable name of the custom post type. |
| 215 |
* |
| 216 |
* @since 1.2.3 |
| 217 |
* |
| 218 |
* @param string $key |
| 219 |
* |
| 220 |
* @return string |
| 221 |
*/ |
| 222 |
function mark_posts_get_post_type_name(string $key) |
| 223 |
{ |
| 224 |
$cpt = get_post_type_object($key); |
| 225 |
|
| 226 |
return is_object($cpt) ? $cpt->labels->name : ucfirst($key); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get all available post types. |
| 231 |
* |
| 232 |
* @since 1.0.0 |
| 233 |
*/ |
| 234 |
function mark_posts_get_all_types() |
| 235 |
{ |
| 236 |
$all_post_types = get_post_types(); |
| 237 |
$option = get_option('mark_posts_settings'); |
| 238 |
|
| 239 |
foreach ($all_post_types as $one_post_type) { |
| 240 |
// Filter excluded post types. |
| 241 |
if (!in_array($one_post_type, mark_posts_excluded_post_types())) { |
| 242 |
echo '<p><input name="markertypes[]" type="checkbox" value="'.$one_post_type.'"'; |
| 243 |
if (isset($option['mark_posts_posttypes'])) { |
| 244 |
if (in_array($one_post_type, $option['mark_posts_posttypes'])) { |
| 245 |
echo ' checked="checked"'; |
| 246 |
} |
| 247 |
} |
| 248 |
echo ' /> '.mark_posts_get_post_type_name($one_post_type).'</p>'; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Get dashboard widget setup. |
| 255 |
* |
| 256 |
* @since 1.0.8 |
| 257 |
*/ |
| 258 |
function mark_posts_dashboard() |
| 259 |
{ |
| 260 |
$option = get_option('mark_posts_settings'); |
| 261 |
echo '<p><input name="markerdashboard[]" type="checkbox" value="dashboard"'; |
| 262 |
if (!empty($option['mark_posts_dashboard'])) { |
| 263 |
echo ' checked="checked"'; |
| 264 |
} |
| 265 |
echo ' /> '.__('Dashboard Widget', 'mark-posts').'</p>'; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Display all settings. |
| 270 |
* |
| 271 |
* @since 1.0.0 |
| 272 |
*/ |
| 273 |
function mark_posts_show_settings() |
| 274 |
{ |
| 275 |
|
| 276 |
// get default colors |
| 277 |
$default_colors = mark_posts_get_default_colors(); ?> |
| 278 |
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> |
| 279 |
<?php |
| 280 |
|
| 281 |
// Get Marker terms from DB |
| 282 |
$markers_terms = mark_posts_get_marker_terms(); |
| 283 |
$markers_registered = ''; |
| 284 |
foreach ($markers_terms as $marker_term) { |
| 285 |
$markers_registered .= $marker_term->name; |
| 286 |
$markers_registered .= ', '; |
| 287 |
} |
| 288 |
$markers_registered = rtrim($markers_registered, ', '); // cut trailing comma and space |
| 289 |
|
| 290 |
if (!empty($markers_terms)) { |
| 291 |
echo '<h3 class="title">'.__('Markers', 'mark-posts').'</h3>'; |
| 292 |
|
| 293 |
echo '<table class="form-table"><tbody>'; |
| 294 |
|
| 295 |
$i = 0; |
| 296 |
foreach ($markers_terms as $marker_term) { |
| 297 |
if ($marker_term->description != '') { |
| 298 |
$color = $marker_term->description; |
| 299 |
} else { |
| 300 |
if (isset($default_colors[$i])) { |
| 301 |
$color = $default_colors[$i]; |
| 302 |
} else { |
| 303 |
$i = 0; // reset pointer to 0 start over |
| 304 |
$color = $default_colors[$i]; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
echo '<tr valign="top"><th scope="row"><input type="text" name="markernames[]" value="'.$marker_term->name.'"></th>'; |
| 309 |
echo '<td width="130"><input type="text" name="colors[]" value="'.$color.'" class="my-color-field" data-default-color="'.$color.'"/></td>'; |
| 310 |
echo '<td><input type="checkbox" name="delete[]" id="delete_'.$marker_term->term_id.'" value="'.$marker_term->term_id.'"> <label for="delete_'.$marker_term->term_id.'">'.__('delete', 'mark-posts').'?</label>'; |
| 311 |
echo '<a href="javascript:void(0);" class="mark-posts-initial" data-confirm-msg="'.__('Do you really want to mark all posts with this marker? Note: This will override all your previous set markers. This will only effect the enabled post types.', 'mark-posts').'" data-term-id="'.$marker_term->term_id.'">'.__('Mark all posts with this marker', 'mark-posts').'</a></td>'; |
| 312 |
echo '<input type="hidden" name="term_ids[]" value="'.$marker_term->term_id.'"/>'; |
| 313 |
$i++; |
| 314 |
} |
| 315 |
|
| 316 |
echo '</tbody></table>'; |
| 317 |
|
| 318 |
submit_button(); |
| 319 |
|
| 320 |
echo '<hr />'; |
| 321 |
} ?> |
| 322 |
|
| 323 |
|
| 324 |
<h3 class="title"><?php _e('Add new Markers', 'mark-posts'); ?></h3> |
| 325 |
|
| 326 |
<p> |
| 327 |
<?php _e('Add new marker (please separate them by comma):', 'mark-posts'); ?> |
| 328 |
</p> |
| 329 |
|
| 330 |
<textarea class="js-add-markers" name="markers" style="width:60%;height:120px;"></textarea> |
| 331 |
|
| 332 |
<div class="new-markers"> |
| 333 |
<span class="js-new-markers-intro"><?php _e('Markers to add:', 'mark-posts'); ?></span> |
| 334 |
<span class="js-new-markers"></span> |
| 335 |
</div> |
| 336 |
|
| 337 |
<?php submit_button(); ?> |
| 338 |
|
| 339 |
<hr /> |
| 340 |
<h3 class="title"><?php _e('Enable/Disable Markers', 'mark-posts'); ?></h3> |
| 341 |
|
| 342 |
<p> |
| 343 |
<?php _e('Enable/Disable markers for specific post types:', 'mark-posts'); ?> |
| 344 |
</p> |
| 345 |
|
| 346 |
<?php |
| 347 |
mark_posts_get_all_types(); |
| 348 |
submit_button(); ?> |
| 349 |
|
| 350 |
<hr /> |
| 351 |
<h3 class="title"><?php _e('Enable/Disable Dashboard Widget', 'mark-posts'); ?></h3> |
| 352 |
|
| 353 |
<?php |
| 354 |
mark_posts_dashboard(); |
| 355 |
submit_button(); ?> |
| 356 |
|
| 357 |
</form> |
| 358 |
|
| 359 |
<?php |
| 360 |
} ?> |
| 361 |
|
| 362 |
<div class="wrap"> |
| 363 |
|
| 364 |
<?php mark_posts_misc_functions() ?> |
| 365 |
|
| 366 |
<?php mark_posts_validate_form(); ?> |
| 367 |
|
| 368 |
<h2><?php _e('Mark Posts Options', 'mark-posts'); ?></h2> |
| 369 |
|
| 370 |
<?php mark_posts_show_settings(); ?> |
| 371 |
|
| 372 |
<div class="mark-posts-copy"> |
| 373 |
<hr /> |
| 374 |
Mark Posts | Version: <?php echo WP_MARK_POSTS_VERSION; ?> | © <?php echo date('Y'); ?> |
| 375 |
<a href="http://www.aliquit.de" target="_blank">Michael Schoenrock</a>, |
| 376 |
<a href="https://hofmannsven.com" target="_blank">Sven Hofmann</a> |
| 377 |
</div> |
| 378 |
|
| 379 |
</div> |
| 380 |
|