| 1 |
<?php |
| 2 |
/** |
| 3 |
* Mark Posts. |
| 4 |
* |
| 5 |
* @author Michael Schoenrock <hello@michaelschoenrock.com>, Sven Hofmann <info@hofmannsven.com> |
| 6 |
* @license GPL-2.0+ |
| 7 |
*/ |
| 8 |
|
| 9 |
// If this file is called directly, abort. |
| 10 |
if (!defined('WPINC')) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Plugin class. This class should ideally be used to work with the |
| 16 |
* public-facing side of the WordPress site. |
| 17 |
* |
| 18 |
* If you're interested in introducing administrative or dashboard |
| 19 |
* functionality, then refer to `class-mark-posts-admin.php` |
| 20 |
*/ |
| 21 |
class Mark_Posts |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Instance of this class. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
protected static $instance = null; |
| 31 |
/** |
| 32 |
* Unique identifier for your plugin. |
| 33 |
* |
| 34 |
* |
| 35 |
* The variable name is used as the text domain when internationalizing strings |
| 36 |
* of text. Its value should match the Text Domain file header in the main |
| 37 |
* plugin file. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $plugin_slug = 'mark-posts'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Initialize the plugin by setting localization and loading public scripts |
| 47 |
* and styles. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
*/ |
| 51 |
private function __construct() |
| 52 |
{ |
| 53 |
|
| 54 |
// Create marker taxonomy |
| 55 |
add_action('init', [$this, 'mark_posts_create_taxonomies']); |
| 56 |
|
| 57 |
// Activate plugin when new blog is added |
| 58 |
add_action('wpmu_new_blog', [$this, 'mark_posts_activate_new_site']); |
| 59 |
|
| 60 |
// Register settings |
| 61 |
add_action('admin_init', [$this, 'mark_posts_register_settings']); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Return an instance of this class. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* |
| 69 |
* @return object A single instance of this class. |
| 70 |
*/ |
| 71 |
public static function get_instance() |
| 72 |
{ |
| 73 |
|
| 74 |
// If the single instance hasn't been set, set it now. |
| 75 |
if (null == self::$instance) { |
| 76 |
self::$instance = new self(); |
| 77 |
} |
| 78 |
|
| 79 |
return self::$instance; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Fired when the plugin is activated. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* |
| 87 |
* @param bool $network_wide True if WPMU superadmin uses |
| 88 |
* "Network Activate" action, false if |
| 89 |
* WPMU is disabled or plugin is |
| 90 |
* activated on an individual blog. |
| 91 |
*/ |
| 92 |
public static function activate($network_wide) |
| 93 |
{ |
| 94 |
if (function_exists('is_multisite') && is_multisite()) { |
| 95 |
if ($network_wide) { |
| 96 |
|
| 97 |
// Get all blog ids |
| 98 |
$blog_ids = self::get_blog_ids(); |
| 99 |
|
| 100 |
foreach ($blog_ids as $blog_id) { |
| 101 |
switch_to_blog($blog_id); |
| 102 |
self::single_activate(); |
| 103 |
} |
| 104 |
|
| 105 |
restore_current_blog(); |
| 106 |
} else { |
| 107 |
self::single_activate(); |
| 108 |
} |
| 109 |
} else { |
| 110 |
self::single_activate(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get all blog ids of blogs in the current network that are: |
| 116 |
* - not archived |
| 117 |
* - not spam |
| 118 |
* - not deleted. |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
* |
| 122 |
* @return array|false The blog ids, false if no matches. |
| 123 |
*/ |
| 124 |
private static function get_blog_ids() |
| 125 |
{ |
| 126 |
global $wpdb; |
| 127 |
|
| 128 |
// get an array of blog ids |
| 129 |
$sql = "SELECT blog_id FROM $wpdb->blogs |
| 130 |
WHERE archived = '0' AND spam = '0' |
| 131 |
AND deleted = '0'"; |
| 132 |
|
| 133 |
return $wpdb->get_col($sql); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Fired for each blog when the plugin is activated. |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* @updated 1.1.0 |
| 141 |
*/ |
| 142 |
private static function single_activate() |
| 143 |
{ |
| 144 |
add_option( |
| 145 |
'mark_posts_settings', |
| 146 |
[ |
| 147 |
'mark_posts_posttypes' => ['post', 'page'], |
| 148 |
'mark_posts_dashboard' => ['dashboard'], |
| 149 |
] |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Fired when the plugin is deactivated. |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
* |
| 158 |
* @param bool $network_wide True if WPMU superadmin uses |
| 159 |
* "Network Deactivate" action, false if |
| 160 |
* WPMU is disabled or plugin is |
| 161 |
* deactivated on an individual blog. |
| 162 |
*/ |
| 163 |
public static function deactivate($network_wide) |
| 164 |
{ |
| 165 |
if (function_exists('is_multisite') && is_multisite()) { |
| 166 |
if ($network_wide) { |
| 167 |
|
| 168 |
// Get all blog ids |
| 169 |
$blog_ids = self::get_blog_ids(); |
| 170 |
|
| 171 |
foreach ($blog_ids as $blog_id) { |
| 172 |
switch_to_blog($blog_id); |
| 173 |
self::single_deactivate(); |
| 174 |
} |
| 175 |
|
| 176 |
restore_current_blog(); |
| 177 |
} else { |
| 178 |
self::single_deactivate(); |
| 179 |
} |
| 180 |
} else { |
| 181 |
self::single_deactivate(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Fired for each blog when the plugin is deactivated. |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
*/ |
| 190 |
private static function single_deactivate() |
| 191 |
{ |
| 192 |
// @TODO: Define deactivation functionality here |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Return the plugin slug. |
| 197 |
* |
| 198 |
* @since 1.0.0 |
| 199 |
* |
| 200 |
* @return Plugin slug variable. |
| 201 |
*/ |
| 202 |
public function get_plugin_slug() |
| 203 |
{ |
| 204 |
return $this->plugin_slug; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Fired when a new site is activated with a WPMU environment. |
| 209 |
* |
| 210 |
* @since 1.0.0 |
| 211 |
* |
| 212 |
* @param int $blog_id ID of the new blog. |
| 213 |
*/ |
| 214 |
public function mark_posts_activate_new_site($blog_id) |
| 215 |
{ |
| 216 |
if (1 !== did_action('wpmu_new_blog')) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
switch_to_blog($blog_id); |
| 221 |
self::single_activate(); |
| 222 |
restore_current_blog(); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Register settings. |
| 227 |
* |
| 228 |
* @since 1.0.0 |
| 229 |
*/ |
| 230 |
public function mark_posts_register_settings() |
| 231 |
{ |
| 232 |
$option_name = 'plugin_mark_posts_settings'; |
| 233 |
|
| 234 |
register_setting( |
| 235 |
'general', |
| 236 |
$option_name, |
| 237 |
[$this, 'mark_posts_settings_validate'] |
| 238 |
); |
| 239 |
add_settings_section( |
| 240 |
$option_name, |
| 241 |
__('Mark Posts Options', 'plugin_mark_posts_settings'), |
| 242 |
'__return_false', |
| 243 |
$this->plugin_slug // plugin slug |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Validate settings. |
| 249 |
* |
| 250 |
* @since 1.0.0 |
| 251 |
*/ |
| 252 |
public function mark_posts_settings_validate($input) |
| 253 |
{ |
| 254 |
// todo: sanitize user input |
| 255 |
return $input; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Create marker taxonomy. |
| 260 |
* |
| 261 |
* @since 1.0.0 |
| 262 |
*/ |
| 263 |
public function mark_posts_create_taxonomies() |
| 264 |
{ |
| 265 |
|
| 266 |
// Add new marker taxonomy |
| 267 |
$labels = [ |
| 268 |
'name' => __('Marker', 'mark-posts'), |
| 269 |
'singular_name' => __('Marker', 'mark-posts'), |
| 270 |
'search_items' => __('Search Marker', 'mark-posts'), |
| 271 |
'all_items' => __('All Markers', 'mark-posts'), |
| 272 |
'parent_item' => __('Parent Marker', 'mark-posts'), |
| 273 |
'parent_item_colon' => __('Parent Marker:', 'mark-posts'), |
| 274 |
'edit_item' => __('Edit Marker', 'mark-posts'), |
| 275 |
'update_item' => __('Update Marker', 'mark-posts'), |
| 276 |
'add_new_item' => __('Add New Marker', 'mark-posts'), |
| 277 |
'new_item_name' => __('New Marker Name', 'mark-posts'), |
| 278 |
'menu_name' => __('Marker', 'mark-posts'), |
| 279 |
]; |
| 280 |
|
| 281 |
$args = [ |
| 282 |
'hierarchical' => true, |
| 283 |
'labels' => $labels, |
| 284 |
'show_ui' => true, |
| 285 |
'show_admin_column' => true, |
| 286 |
'query_var' => true, |
| 287 |
'rewrite' => ['slug' => 'marker'], |
| 288 |
'update_count_callback' => 'marker_update_count_callback', |
| 289 |
]; |
| 290 |
|
| 291 |
/** |
| 292 |
* Function for updating the marker taxonomy count. |
| 293 |
* |
| 294 |
* See the _update_post_term_count() function in WordPress or http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress for more info. |
| 295 |
* |
| 296 |
* @since 1.0.7 |
| 297 |
* |
| 298 |
* @param array $terms List of Term taxonomy IDs |
| 299 |
* @param object $taxonomy Current taxonomy object of terms |
| 300 |
*/ |
| 301 |
function marker_update_count_callback($terms, $taxonomy) |
| 302 |
{ |
| 303 |
global $wpdb; |
| 304 |
|
| 305 |
foreach ((array) $terms as $term) { |
| 306 |
$count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term)); |
| 307 |
|
| 308 |
do_action('edit_term_taxonomy', $term, $taxonomy); |
| 309 |
$wpdb->update($wpdb->term_taxonomy, compact('count'), ['term_taxonomy_id' => $term]); |
| 310 |
do_action('edited_term_taxonomy', $term, $taxonomy); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
* null - Setting explicitly to null registers the taxonomy but doesn't |
| 316 |
* associate it with any objects, so it won't be directly available within |
| 317 |
* the Admin UI. You will need to manually register it using the 'taxonomy' |
| 318 |
* parameter (passed through $args) when registering a custom post_type |
| 319 |
* (see register_post_type()), or using register_taxonomy_for_object_type(). |
| 320 |
* |
| 321 |
* see http://codex.wordpress.org/Function_Reference/register_taxonomy |
| 322 |
*/ |
| 323 |
|
| 324 |
register_taxonomy('marker', 'null', $args); |
| 325 |
} |
| 326 |
} |
| 327 |
|