| 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 |
die; |
| 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 |
return apply_filters('mark_posts_excluded_post_types', [ |
| 193 |
'attachment', |
| 194 |
'revision', |
| 195 |
'nav_menu_item', |
| 196 |
'custom_css', |
| 197 |
'customize_changeset', |
| 198 |
'oembed_cache', |
| 199 |
'user_request', |
| 200 |
'wp_block' |
| 201 |
]); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get all available post types. |
| 206 |
* |
| 207 |
* @since 1.0.0 |
| 208 |
*/ |
| 209 |
function mark_posts_get_all_types() |
| 210 |
{ |
| 211 |
$all_post_types = get_post_types(); |
| 212 |
$option = get_option('mark_posts_settings'); |
| 213 |
|
| 214 |
foreach ($all_post_types as $one_post_type) { |
| 215 |
// Filter excluded post types. |
| 216 |
if (!in_array($one_post_type, mark_posts_excluded_post_types())) { |
| 217 |
echo '<p><input name="markertypes[]" type="checkbox" value="'.$one_post_type.'"'; |
| 218 |
if (isset($option['mark_posts_posttypes'])) : |
| 219 |
if (in_array($one_post_type, $option['mark_posts_posttypes'])) : |
| 220 |
echo ' checked="checked"'; |
| 221 |
endif; |
| 222 |
endif; |
| 223 |
echo ' /> '.ucfirst($one_post_type).'</p>'; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get dashboard widget setup. |
| 230 |
* |
| 231 |
* @since 1.0.8 |
| 232 |
*/ |
| 233 |
function mark_posts_dashboard() |
| 234 |
{ |
| 235 |
$option = get_option('mark_posts_settings'); |
| 236 |
echo '<p><input name="markerdashboard[]" type="checkbox" value="dashboard"'; |
| 237 |
if (!empty($option['mark_posts_dashboard'])) : |
| 238 |
echo ' checked="checked"'; |
| 239 |
endif; |
| 240 |
echo ' /> '.__('Dashboard Widget', 'mark-posts').'</p>'; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Display all settings. |
| 245 |
* |
| 246 |
* @since 1.0.0 |
| 247 |
*/ |
| 248 |
function mark_posts_show_settings() |
| 249 |
{ |
| 250 |
|
| 251 |
// get default colors |
| 252 |
$default_colors = mark_posts_get_default_colors(); ?> |
| 253 |
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> |
| 254 |
<?php |
| 255 |
|
| 256 |
// Get Marker terms from DB |
| 257 |
$markers_terms = mark_posts_get_marker_terms(); |
| 258 |
$markers_registered = ''; |
| 259 |
foreach ($markers_terms as $marker_term) { |
| 260 |
$markers_registered .= $marker_term->name; |
| 261 |
$markers_registered .= ', '; |
| 262 |
} |
| 263 |
$markers_registered = rtrim($markers_registered, ', '); // cut trailing comma and space |
| 264 |
|
| 265 |
if (!empty($markers_terms)) { |
| 266 |
echo '<h3 class="title">'.__('Markers', 'mark-posts').'</h3>'; |
| 267 |
|
| 268 |
echo '<table class="form-table"><tbody>'; |
| 269 |
|
| 270 |
$i = 0; |
| 271 |
foreach ($markers_terms as $marker_term) { |
| 272 |
if ($marker_term->description != '') { |
| 273 |
$color = $marker_term->description; |
| 274 |
} else { |
| 275 |
if (isset($default_colors[$i])) { |
| 276 |
$color = $default_colors[$i]; |
| 277 |
} else { |
| 278 |
$i = 0; // reset pointer to 0 start over |
| 279 |
$color = $default_colors[$i]; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
echo '<tr valign="top"><th scope="row"><input type="text" name="markernames[]" value="'.$marker_term->name.'"></th>'; |
| 284 |
echo '<td width="130"><input type="text" name="colors[]" value="'.$color.'" class="my-color-field" data-default-color="'.$color.'"/></td>'; |
| 285 |
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>'; |
| 286 |
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>'; |
| 287 |
echo '<input type="hidden" name="term_ids[]" value="'.$marker_term->term_id.'"/>'; |
| 288 |
$i++; |
| 289 |
} |
| 290 |
|
| 291 |
echo '</tbody></table>'; |
| 292 |
|
| 293 |
submit_button(); |
| 294 |
|
| 295 |
echo '<hr />'; |
| 296 |
} ?> |
| 297 |
|
| 298 |
|
| 299 |
<h3 class="title"><?php _e('Add new Markers', 'mark-posts'); ?></h3> |
| 300 |
|
| 301 |
<p> |
| 302 |
<?php _e('Add new marker (please separate them by comma):', 'mark-posts'); ?> |
| 303 |
</p> |
| 304 |
|
| 305 |
<textarea class="js-add-markers" name="markers" style="width:60%;height:120px;"></textarea> |
| 306 |
|
| 307 |
<div class="new-markers"> |
| 308 |
<span class="js-new-markers-intro"><?php _e('Markers to add:', 'mark-posts'); ?></span> |
| 309 |
<span class="js-new-markers"></span> |
| 310 |
</div> |
| 311 |
|
| 312 |
<?php submit_button(); ?> |
| 313 |
|
| 314 |
<hr /> |
| 315 |
<h3 class="title"><?php _e('Enable/Disable Markers', 'mark-posts'); ?></h3> |
| 316 |
|
| 317 |
<p> |
| 318 |
<?php _e('Enable/Disable markers for specific post types:', 'mark-posts'); ?> |
| 319 |
</p> |
| 320 |
|
| 321 |
<?php |
| 322 |
mark_posts_get_all_types(); |
| 323 |
submit_button(); |
| 324 |
?> |
| 325 |
|
| 326 |
<hr /> |
| 327 |
<h3 class="title"><?php _e('Enable/Disable Dashboard Widget', 'mark-posts'); ?></h3> |
| 328 |
|
| 329 |
<?php |
| 330 |
mark_posts_dashboard(); |
| 331 |
submit_button(); |
| 332 |
?> |
| 333 |
|
| 334 |
</form> |
| 335 |
|
| 336 |
<?php |
| 337 |
} ?> |
| 338 |
|
| 339 |
<div class="wrap"> |
| 340 |
|
| 341 |
<?php mark_posts_misc_functions() ?> |
| 342 |
|
| 343 |
<?php mark_posts_validate_form(); ?> |
| 344 |
|
| 345 |
<h2><?php _e('Mark Posts Options', 'mark-posts'); ?></h2> |
| 346 |
|
| 347 |
<?php mark_posts_show_settings(); ?> |
| 348 |
|
| 349 |
<div class="mark-posts-copy"> |
| 350 |
<hr /> |
| 351 |
Mark Posts | Version: <?php echo WP_MARK_POSTS_VERSION; ?> | © <?php echo date('Y'); ?> |
| 352 |
<a href="http://www.aliquit.de" target="_blank">Michael Schoenrock</a>, |
| 353 |
<a href="https://hofmannsven.com" target="_blank">Sven Hofmann</a> |
| 354 |
</div> |
| 355 |
|
| 356 |
</div> |
| 357 |
|