| 1 |
<?php |
| 2 |
/** |
| 3 |
* Alerts feature class. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Alerts |
| 12 |
* |
| 13 |
* @package WP_Stream |
| 14 |
*/ |
| 15 |
class Alerts { |
| 16 |
/** |
| 17 |
* Alerts post type slug |
| 18 |
*/ |
| 19 |
const POST_TYPE = 'wp_stream_alerts'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Triggered Alerts meta key for Records |
| 23 |
*/ |
| 24 |
const ALERTS_TRIGGERED_META_KEY = 'wp_stream_alerts_triggered'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Hold Plugin class |
| 28 |
* |
| 29 |
* @var Plugin |
| 30 |
*/ |
| 31 |
public $plugin; |
| 32 |
|
| 33 |
/** |
| 34 |
* Post meta prefix |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $meta_prefix = 'wp_stream'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Alert Types |
| 42 |
* |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
public $alert_types = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Alert Triggers |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
public $alert_triggers = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Class constructor. |
| 56 |
* |
| 57 |
* @param Plugin $plugin The main Plugin class. |
| 58 |
*/ |
| 59 |
public function __construct( $plugin ) { |
| 60 |
$this->plugin = $plugin; |
| 61 |
|
| 62 |
// Register custom post type. |
| 63 |
add_action( 'init', array( $this, 'register_post_type' ) ); |
| 64 |
|
| 65 |
// Add custom post type to menu. |
| 66 |
add_action( 'wp_stream_admin_menu', array( $this, 'register_menu' ) ); |
| 67 |
|
| 68 |
// Add scripts to post screens. |
| 69 |
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) ); |
| 70 |
|
| 71 |
add_action( 'network_admin_menu', array( $this, 'change_menu_link_url' ), 99 ); |
| 72 |
|
| 73 |
add_filter( 'wp_stream_record_inserted', array( $this, 'check_records' ), 10, 2 ); |
| 74 |
|
| 75 |
add_action( 'wp_ajax_load_alerts_settings', array( $this, 'load_alerts_settings' ) ); |
| 76 |
add_action( 'wp_ajax_get_actions', array( $this, 'get_actions' ) ); |
| 77 |
add_action( 'wp_ajax_save_new_alert', array( $this, 'save_new_alert' ) ); |
| 78 |
add_action( 'wp_ajax_get_new_alert_triggers_notifications', array( |
| 79 |
$this, |
| 80 |
'get_new_alert_triggers_notifications', |
| 81 |
) ); |
| 82 |
|
| 83 |
$this->load_alert_types(); |
| 84 |
$this->load_alert_triggers(); |
| 85 |
|
| 86 |
add_filter( 'wp_stream_action_links_posts', array( $this, 'change_alert_action_links' ), 11, 2 ); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Load alert_type classes |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
function load_alert_types() { |
| 96 |
$alert_types = array( |
| 97 |
'none', |
| 98 |
'highlight', |
| 99 |
'email', |
| 100 |
'ifttt', |
| 101 |
); |
| 102 |
|
| 103 |
$classes = array(); |
| 104 |
foreach ( $alert_types as $alert_type ) { |
| 105 |
$file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-type-' . $alert_type . '.php'; |
| 106 |
if ( file_exists( $file_location ) ) { |
| 107 |
include_once $file_location; |
| 108 |
$class_name = sprintf( '\WP_Stream\Alert_Type_%s', str_replace( '-', '_', $alert_type ) ); |
| 109 |
if ( ! class_exists( $class_name ) ) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
$class = new $class_name( $this->plugin ); |
| 113 |
if ( ! property_exists( $class, 'slug' ) ) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
$classes[ $class->slug ] = $class; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Allows for adding additional alert_types via classes that extend Notifier. |
| 122 |
* |
| 123 |
* @param array $classes An array of Notifier objects. In the format alert_type_slug => Notifier_Class() |
| 124 |
*/ |
| 125 |
$this->alert_types = apply_filters( 'wp_stream_alert_types', $classes ); |
| 126 |
|
| 127 |
// Ensure that all alert_types extend Notifier. |
| 128 |
foreach ( $this->alert_types as $key => $alert_type ) { |
| 129 |
if ( ! $this->is_valid_alert_type( $alert_type ) ) { |
| 130 |
unset( $this->alert_types[ $key ] ); |
| 131 |
trigger_error( |
| 132 |
sprintf( |
| 133 |
esc_html__( 'Registered alert_type %s does not extend WP_Stream\Alert_Type.', 'stream' ), |
| 134 |
esc_html( get_class( $alert_type ) ) |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Load alert_type classes |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
function load_alert_triggers() { |
| 147 |
$alert_triggers = array( |
| 148 |
'author', |
| 149 |
'context', |
| 150 |
'action', |
| 151 |
); |
| 152 |
|
| 153 |
$classes = array(); |
| 154 |
foreach ( $alert_triggers as $alert_trigger ) { |
| 155 |
$file_location = $this->plugin->locations['dir'] . '/alerts/class-alert-trigger-' . $alert_trigger . '.php'; |
| 156 |
if ( file_exists( $file_location ) ) { |
| 157 |
include_once $file_location; |
| 158 |
$class_name = sprintf( '\WP_Stream\Alert_Trigger_%s', str_replace( '-', '_', $alert_trigger ) ); |
| 159 |
if ( ! class_exists( $class_name ) ) { |
| 160 |
continue; |
| 161 |
} |
| 162 |
$class = new $class_name( $this->plugin ); |
| 163 |
if ( ! property_exists( $class, 'slug' ) ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
$classes[ $class->slug ] = $class; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Allows for adding additional alert_triggers via classes that extend Notifier. |
| 172 |
* |
| 173 |
* @param array $classes An array of Notifier objects. In the format alert_trigger_slug => Notifier_Class() |
| 174 |
*/ |
| 175 |
$this->alert_triggers = apply_filters( 'wp_stream_alert_triggers', $classes ); |
| 176 |
|
| 177 |
// Ensure that all alert_triggers extend Notifier. |
| 178 |
foreach ( $this->alert_triggers as $key => $alert_trigger ) { |
| 179 |
if ( ! $this->is_valid_alert_trigger( $alert_trigger ) ) { |
| 180 |
unset( $this->alert_triggers[ $key ] ); |
| 181 |
trigger_error( |
| 182 |
sprintf( |
| 183 |
esc_html__( 'Registered alert_trigger %s does not extend WP_Stream\Alert_Trigger.', 'stream' ), |
| 184 |
esc_html( get_class( $alert_trigger ) ) |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Checks whether a Alert Type class is valid |
| 193 |
* |
| 194 |
* @param Alert_Type $alert_type The class to check. |
| 195 |
* |
| 196 |
* @return bool |
| 197 |
*/ |
| 198 |
public function is_valid_alert_type( $alert_type ) { |
| 199 |
if ( ! is_a( $alert_type, 'WP_Stream\Alert_Type' ) ) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! method_exists( $alert_type, 'is_dependency_satisfied' ) || ! $alert_type->is_dependency_satisfied() ) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Checks whether a Alert Trigger class is valid |
| 212 |
* |
| 213 |
* @param Alert_Trigger $alert_trigger The class to check. |
| 214 |
* |
| 215 |
* @return bool |
| 216 |
*/ |
| 217 |
public function is_valid_alert_trigger( $alert_trigger ) { |
| 218 |
if ( ! is_a( $alert_trigger, 'WP_Stream\Alert_Trigger' ) ) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
if ( ! method_exists( $alert_trigger, 'is_dependency_satisfied' ) || ! $alert_trigger->is_dependency_satisfied() ) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Checks record being processed against active alerts. |
| 231 |
* |
| 232 |
* @filter wp_stream_record_inserted |
| 233 |
* |
| 234 |
* @param int $record_id The record being processed. |
| 235 |
* @param array $recordarr Record data. |
| 236 |
* |
| 237 |
* @return array |
| 238 |
*/ |
| 239 |
function check_records( $record_id, $recordarr ) { |
| 240 |
$args = array( |
| 241 |
'post_type' => self::POST_TYPE, |
| 242 |
'post_status' => 'wp_stream_enabled', |
| 243 |
); |
| 244 |
|
| 245 |
$alerts = new \WP_Query( $args ); |
| 246 |
foreach ( $alerts->posts as $alert ) { |
| 247 |
$alert = $this->get_alert( $alert->ID ); |
| 248 |
|
| 249 |
$status = $alert->check_record( $record_id, $recordarr ); |
| 250 |
if ( $status ) { |
| 251 |
$alert->send_alert( $record_id, $recordarr ); // @todo send_alert expects int, not array. |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return $recordarr; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Register scripts for page load |
| 261 |
* |
| 262 |
* @action admin_enqueue_scripts |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
function register_scripts() { |
| 267 |
$screen = get_current_screen(); |
| 268 |
if ( 'edit-wp_stream_alerts' === $screen->id ) { |
| 269 |
wp_register_script( 'wp-stream-alerts', $this->plugin->locations['url'] . 'ui/js/alerts.js', array( |
| 270 |
'wp-stream-select2', |
| 271 |
'jquery', |
| 272 |
'inline-edit-post', |
| 273 |
) ); |
| 274 |
wp_localize_script( 'wp-stream-alerts', 'streamAlerts', |
| 275 |
array( |
| 276 |
'any' => __( 'Any', 'stream' ), |
| 277 |
'anyContext' => __( 'Any Context', 'stream' ), |
| 278 |
) |
| 279 |
); |
| 280 |
wp_enqueue_script( 'wp-stream-alerts' ); |
| 281 |
wp_enqueue_style( 'wp-stream-select2' ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Register custom post type |
| 287 |
* |
| 288 |
* @action init |
| 289 |
* |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
public function register_post_type() { |
| 293 |
$labels = array( |
| 294 |
'name' => _x( 'Alerts', 'post type general name', 'stream' ), |
| 295 |
'singular_name' => _x( 'Alert', 'post type singular name', 'stream' ), |
| 296 |
'menu_name' => _x( 'Alerts', 'admin menu', 'stream' ), |
| 297 |
'name_admin_bar' => _x( 'Alert', 'add new on admin bar', 'stream' ), |
| 298 |
'add_new' => _x( 'Add New', 'book', 'stream' ), |
| 299 |
'add_new_item' => __( 'Add New Alert', 'stream' ), |
| 300 |
'new_item' => __( 'New Alert', 'stream' ), |
| 301 |
'edit_item' => __( 'Edit Alert', 'stream' ), |
| 302 |
'view_item' => __( 'View Alert', 'stream' ), |
| 303 |
'all_items' => __( 'Alerts', 'stream' ), |
| 304 |
'search_items' => __( 'Search Alerts', 'stream' ), |
| 305 |
'parent_item_colon' => __( 'Parent Alerts:', 'stream' ), |
| 306 |
'not_found' => __( 'No alerts found.', 'stream' ), |
| 307 |
'not_found_in_trash' => __( 'No alerts found in Trash.', 'stream' ), |
| 308 |
); |
| 309 |
|
| 310 |
$args = array( |
| 311 |
'labels' => $labels, |
| 312 |
'description' => __( 'Alerts for Stream.', 'stream' ), |
| 313 |
'public' => false, |
| 314 |
'publicly_queryable' => false, |
| 315 |
'exclude_from_search' => true, |
| 316 |
'show_ui' => true, |
| 317 |
'show_in_menu' => false, // @see modify_admin_menu |
| 318 |
'supports' => false, |
| 319 |
'capabilities' => array( |
| 320 |
'publish_posts' => 'manage_options', |
| 321 |
'edit_others_posts' => 'manage_options', |
| 322 |
'delete_posts' => 'manage_options', |
| 323 |
'delete_others_posts' => 'manage_options', |
| 324 |
'read_private_posts' => 'manage_options', |
| 325 |
'edit_post' => 'manage_options', |
| 326 |
'delete_post' => 'manage_options', |
| 327 |
'read_post' => 'manage_options', |
| 328 |
), |
| 329 |
); |
| 330 |
|
| 331 |
register_post_type( self::POST_TYPE, $args ); |
| 332 |
|
| 333 |
$args = array( |
| 334 |
'label' => _x( 'Enabled', 'alert', 'stream' ), |
| 335 |
'public' => false, |
| 336 |
'show_in_admin_all_list' => true, |
| 337 |
'show_in_admin_status_list' => true, |
| 338 |
'label_count' => _n_noop( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', 'stream' ), |
| 339 |
); |
| 340 |
|
| 341 |
register_post_status( 'wp_stream_enabled', $args ); |
| 342 |
|
| 343 |
$args = array( |
| 344 |
'label' => _x( 'Disabled', 'alert', 'stream' ), |
| 345 |
'public' => false, |
| 346 |
'internal' => false, |
| 347 |
'show_in_admin_all_list' => true, |
| 348 |
'show_in_admin_status_list' => true, |
| 349 |
'label_count' => _n_noop( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', 'stream' ), |
| 350 |
); |
| 351 |
|
| 352 |
register_post_status( 'wp_stream_disabled', $args ); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Return alert object of the given ID |
| 357 |
* |
| 358 |
* @param string $post_id Post ID for the alert. |
| 359 |
* |
| 360 |
* @return Alert |
| 361 |
*/ |
| 362 |
public function get_alert( $post_id = '' ) { |
| 363 |
if ( ! $post_id ) { |
| 364 |
$obj = new Alert( null, $this->plugin ); |
| 365 |
return $obj; |
| 366 |
} |
| 367 |
|
| 368 |
$post = get_post( $post_id ); |
| 369 |
|
| 370 |
$alert_type = get_post_meta( $post_id, 'alert_type', true ); |
| 371 |
$alert_meta = get_post_meta( $post_id, 'alert_meta', true ); |
| 372 |
|
| 373 |
$obj = (object) array( |
| 374 |
'ID' => $post->ID, |
| 375 |
'status' => $post->post_status, |
| 376 |
'date' => $post->post_date, |
| 377 |
'author' => $post->post_author, |
| 378 |
'alert_type' => $alert_type, |
| 379 |
'alert_meta' => $alert_meta, |
| 380 |
); |
| 381 |
|
| 382 |
return new Alert( $obj, $this->plugin ); |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Add custom post type to menu |
| 388 |
* |
| 389 |
* @action admin_menu |
| 390 |
* |
| 391 |
* @return void |
| 392 |
*/ |
| 393 |
function register_menu() { |
| 394 |
add_submenu_page( |
| 395 |
$this->plugin->admin->records_page_slug, |
| 396 |
__( 'Alerts', 'stream' ), |
| 397 |
__( 'Alerts', 'stream' ), |
| 398 |
'manage_options', |
| 399 |
'edit.php?post_type=wp_stream_alerts' |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Modify the Stream > Alerts Network Admin Menu link. |
| 405 |
* |
| 406 |
* In self::register_menu(), the Alerts submenu item |
| 407 |
* is essentially set to go to the Site's admin area. |
| 408 |
* |
| 409 |
* However, on the Network admin, we need to redirect |
| 410 |
* it to the first site in the network, as this is |
| 411 |
* where the true Network Alerts settings page is located. |
| 412 |
* |
| 413 |
* @action network_admin_menu |
| 414 |
* @return bool |
| 415 |
*/ |
| 416 |
function change_menu_link_url() { |
| 417 |
global $submenu; |
| 418 |
|
| 419 |
$parent = 'wp_stream'; |
| 420 |
$page = 'edit.php?post_type=wp_stream_alerts'; |
| 421 |
|
| 422 |
// If we're not on the Stream menu item, return. |
| 423 |
if ( ! isset( $submenu[ $parent ] ) ) { |
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
// Get the first existing Site in the Network. |
| 428 |
$sites = wp_stream_get_sites( |
| 429 |
array( |
| 430 |
'limit' => 5, // Limit the size of the query. |
| 431 |
) |
| 432 |
); |
| 433 |
|
| 434 |
$site_id = '1'; |
| 435 |
|
| 436 |
// Function wp_get_sites() can return an empty array if the network is too large. |
| 437 |
if ( ! empty( $sites ) && ! empty( $sites[0]->blog_id ) ) { |
| 438 |
$site_id = $sites[0]->blog_id; |
| 439 |
} |
| 440 |
|
| 441 |
$new_url = get_admin_url( $site_id, $page ); |
| 442 |
|
| 443 |
foreach ( $submenu[ $parent ] as $key => $value ) { |
| 444 |
|
| 445 |
// Set correct URL for the menu item. |
| 446 |
if ( $page === $value[2] ) { |
| 447 |
$submenu[ $parent ][ $key ][2] = $new_url; |
| 448 |
break; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Display Alert Type Meta Box |
| 457 |
* |
| 458 |
* @param \WP_Post|array $post Post object for current alert. |
| 459 |
* |
| 460 |
* @return void |
| 461 |
*/ |
| 462 |
function display_notification_box( $post = array() ) { |
| 463 |
$alert_type = 'none'; |
| 464 |
if ( is_object( $post ) ) { |
| 465 |
$alert = $this->get_alert( $post->ID ); |
| 466 |
$alert_type = $alert->alert_type; |
| 467 |
} |
| 468 |
$form = new Form_Generator; |
| 469 |
|
| 470 |
$field_html = $form->render_field( 'select', array( |
| 471 |
'id' => 'wp_stream_alert_type', |
| 472 |
'name' => 'wp_stream_alert_type', |
| 473 |
'value' => $alert_type, |
| 474 |
'options' => $this->get_notification_values(), |
| 475 |
'placeholder' => __( 'No Alert', 'stream' ), |
| 476 |
'title' => 'Alert Type:', |
| 477 |
) ); |
| 478 |
|
| 479 |
echo '<label>' . esc_html__( 'Alert me by', 'stream' ) . '</label>'; |
| 480 |
echo $field_html; // Xss ok. |
| 481 |
|
| 482 |
echo '<div id="wp_stream_alert_type_form">'; |
| 483 |
if ( is_object( $alert ) ) { |
| 484 |
$alert->get_alert_type_obj()->display_fields( $alert ); |
| 485 |
} else { |
| 486 |
$this->plugin->alerts->alert_types['none']->display_fields( array() ); |
| 487 |
} |
| 488 |
|
| 489 |
echo '</div>'; |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Returns settings form HTML for AJAX use |
| 494 |
* |
| 495 |
* @action wp_ajax_load_alerts_settings |
| 496 |
* |
| 497 |
* @return void |
| 498 |
*/ |
| 499 |
function load_alerts_settings() { |
| 500 |
$alert = array(); |
| 501 |
$post_id = wp_stream_filter_input( INPUT_POST, 'post_id' ); |
| 502 |
if ( ! empty( $post_id ) ) { |
| 503 |
$alert = $this->get_alert( $post_id ); |
| 504 |
if ( ! $alert ) { |
| 505 |
wp_send_json_error( array( |
| 506 |
'message' => 'Could not find alert.', |
| 507 |
) ); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
$alert_type = wp_stream_filter_input( INPUT_POST, 'alert_type' ); |
| 512 |
if ( empty( $alert_type ) ) { |
| 513 |
$alert_type = 'none'; |
| 514 |
} |
| 515 |
if ( ! array_key_exists( $alert_type, $this->alert_types ) ) { |
| 516 |
wp_send_json_error( array( |
| 517 |
'message' => 'Could not find alert type.', |
| 518 |
) ); |
| 519 |
} |
| 520 |
|
| 521 |
ob_start(); |
| 522 |
$this->alert_types[ $alert_type ]->display_fields( $alert ); |
| 523 |
$output = ob_get_contents(); |
| 524 |
ob_end_clean(); |
| 525 |
|
| 526 |
$data = array( 'html' => $output ); |
| 527 |
wp_send_json_success( $data ); |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Display Trigger Meta Box |
| 532 |
* |
| 533 |
* @param \WP_Post|array $post Post object for current alert. |
| 534 |
* |
| 535 |
* @return void |
| 536 |
*/ |
| 537 |
function display_triggers_box( $post = array() ) { |
| 538 |
if ( is_object( $post ) ) { |
| 539 |
$alert = $this->get_alert( $post->ID ); |
| 540 |
} else { |
| 541 |
$alert = array(); |
| 542 |
} |
| 543 |
$form = new Form_Generator; |
| 544 |
do_action( 'wp_stream_alert_trigger_form_display', $form, $alert ); |
| 545 |
// @TODO use human readable text. |
| 546 |
echo '<label>' . esc_html__( 'Alert me when', 'stream' ) . '</label>'; |
| 547 |
echo $form->render_fields(); // Xss ok. |
| 548 |
wp_nonce_field( 'save_alert', 'wp_stream_alerts_nonce' ); |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Display Submit Box |
| 553 |
* |
| 554 |
* @param \WP_Post $post Post object for current alert. |
| 555 |
* |
| 556 |
* @return void |
| 557 |
*/ |
| 558 |
function display_submit_box( $post ) { |
| 559 |
if ( empty( $post ) ) { |
| 560 |
return; |
| 561 |
} |
| 562 |
|
| 563 |
$post_status = $post->post_status; |
| 564 |
if ( 'auto-draft' === $post_status ) { |
| 565 |
$post_status = 'wp_stream_enabled'; |
| 566 |
} |
| 567 |
?> |
| 568 |
<div class="submitbox" id="submitpost"> |
| 569 |
<div id="minor-publishing"> |
| 570 |
<div id="misc-publishing-actions"> |
| 571 |
<div class="misc-pub-section misc-pub-post-status"> |
| 572 |
<label for="wp_stream_alert_status"><?php esc_html_e( 'Status', 'stream' ); ?></label> |
| 573 |
<select name='wp_stream_alert_status' id='wp_stream_alert_status'> |
| 574 |
<option<?php selected( $post_status, 'wp_stream_enabled' ); ?> |
| 575 |
value='wp_stream_enabled'><?php esc_html_e( 'Enabled', 'stream' ); ?></option> |
| 576 |
<option<?php selected( $post_status, 'wp_stream_disabled' ); ?> |
| 577 |
value='wp_stream_disabled'><?php esc_html_e( 'Disabled', 'stream' ); ?></option> |
| 578 |
</select> |
| 579 |
</div> |
| 580 |
</div> |
| 581 |
<div class="clear"></div> |
| 582 |
</div> |
| 583 |
|
| 584 |
<div id="major-publishing-actions"> |
| 585 |
<div id="delete-action"> |
| 586 |
<?php |
| 587 |
if ( current_user_can( 'delete_post', $post->ID ) ) { |
| 588 |
if ( ! EMPTY_TRASH_DAYS ) { |
| 589 |
$delete_text = __( 'Delete Permanently', 'stream' ); |
| 590 |
} else { |
| 591 |
$delete_text = __( 'Move to Trash', 'stream' ); |
| 592 |
} |
| 593 |
?> |
| 594 |
<a class="submitdelete deletion" |
| 595 |
href="<?php echo get_delete_post_link( $post->ID ); ?>"><?php esc_html( $delete_text ); ?></a><?php |
| 596 |
} ?> |
| 597 |
</div> |
| 598 |
<div id="publishing-action"> |
| 599 |
<span class="spinner"></span> |
| 600 |
<?php submit_button( __( 'Save' ), 'primary button-large', 'publish', false ); ?> |
| 601 |
</div> |
| 602 |
<div class="clear"></div> |
| 603 |
</div> |
| 604 |
</div> |
| 605 |
<?php |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Display Status Box |
| 610 |
* |
| 611 |
* @return void |
| 612 |
*/ |
| 613 |
function display_status_box() { |
| 614 |
?> |
| 615 |
<div id="minor-publishing"> |
| 616 |
<div id="misc-publishing-actions"> |
| 617 |
<div class="misc-pub-section misc-pub-post-status"> |
| 618 |
<label for="wp_stream_alert_status"> |
| 619 |
<span class="title"><?php esc_html_e( 'Status:', 'stream' ); ?></span> |
| 620 |
<span class="input-text-wrap"> |
| 621 |
<select name='wp_stream_alert_status' id='wp_stream_alert_status'> |
| 622 |
<option selected value='wp_stream_enabled'><?php esc_html_e( 'Enabled', 'stream' ); ?></option> |
| 623 |
<option value='wp_stream_disabled'><?php esc_html_e( 'Disabled', 'stream' ); ?></option> |
| 624 |
</select> |
| 625 |
</span> |
| 626 |
</label> |
| 627 |
</div> |
| 628 |
</div> |
| 629 |
<div class="clear"></div> |
| 630 |
</div> |
| 631 |
<?php |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* Return all notification values |
| 636 |
* |
| 637 |
* @return array |
| 638 |
*/ |
| 639 |
function get_notification_values() { |
| 640 |
$result = array(); |
| 641 |
$names = wp_list_pluck( $this->alert_types, 'name', 'slug' ); |
| 642 |
foreach ( $names as $slug => $name ) { |
| 643 |
$result[ $slug ] = $name; |
| 644 |
} |
| 645 |
|
| 646 |
return $result; |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Update actions dropdown options based on the connector selected. |
| 651 |
*/ |
| 652 |
function get_actions() { |
| 653 |
$connector_name = wp_stream_filter_input( INPUT_POST, 'connector' ); |
| 654 |
$stream_connectors = wp_stream_get_instance()->connectors; |
| 655 |
if ( ! empty( $connector_name ) ) { |
| 656 |
if ( isset( $stream_connectors->connectors[ $connector_name ] ) ) { |
| 657 |
$connector = $stream_connectors->connectors[ $connector_name ]; |
| 658 |
if ( method_exists( $connector, 'get_action_labels' ) ) { |
| 659 |
$actions = $connector->get_action_labels(); |
| 660 |
} |
| 661 |
} |
| 662 |
} else { |
| 663 |
$actions = $stream_connectors->term_labels['stream_action']; |
| 664 |
} |
| 665 |
ksort( $actions ); |
| 666 |
wp_send_json_success( $actions ); |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Save a new alert |
| 671 |
*/ |
| 672 |
function save_new_alert() { |
| 673 |
check_ajax_referer( 'save_alert', 'wp_stream_alerts_nonce' ); |
| 674 |
$trigger_author = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_author' ); |
| 675 |
$trigger_connector_and_context = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' ); |
| 676 |
if ( false !== strpos( $trigger_connector_and_context, '-' ) ) { |
| 677 |
// This is a connector with a context such as posts-post. |
| 678 |
$trigger_connector_and_context_split = explode( '-', $trigger_connector_and_context ); |
| 679 |
$trigger_connector = $trigger_connector_and_context_split[0]; |
| 680 |
$trigger_context = $trigger_connector_and_context_split[1]; |
| 681 |
} else { |
| 682 |
if ( ! empty( $trigger_connector_and_context ) ) { |
| 683 |
// This is a parent connector with no dash such as posts. |
| 684 |
$trigger_connector = $trigger_connector_and_context; |
| 685 |
$trigger_context = ''; |
| 686 |
} else { |
| 687 |
// There is no connector or context. |
| 688 |
$trigger_connector = ''; |
| 689 |
$trigger_context = ''; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
$trigger_action = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_action' ); |
| 694 |
$alert_type = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_type' ); |
| 695 |
$alert_status = wp_stream_filter_input( INPUT_POST, 'wp_stream_alert_status' ); |
| 696 |
|
| 697 |
// Insert the post into the database |
| 698 |
$item = (object) array( |
| 699 |
'alert_type' => $alert_type, |
| 700 |
'alert_meta' => array( |
| 701 |
'trigger_author' => $trigger_author, |
| 702 |
'trigger_connector' => $trigger_connector, |
| 703 |
'trigger_action' => $trigger_action, |
| 704 |
'trigger_context' => $trigger_context, |
| 705 |
), |
| 706 |
'alert_status' => $alert_status, |
| 707 |
); |
| 708 |
$alert = new Alert( $item, $this->plugin ); |
| 709 |
$title = $alert->get_title(); |
| 710 |
$post_id = wp_insert_post( array( |
| 711 |
'post_status' => $alert_status, |
| 712 |
'post_type' => 'wp_stream_alerts', |
| 713 |
'post_title' => $title, |
| 714 |
) ); |
| 715 |
if ( empty( $post_id ) ) { |
| 716 |
wp_send_json_error(); |
| 717 |
} |
| 718 |
add_post_meta( $post_id, 'alert_type', $alert_type ); |
| 719 |
|
| 720 |
$alert_meta = array( |
| 721 |
'trigger_author' => $trigger_author, |
| 722 |
'trigger_connector' => $trigger_connector, |
| 723 |
'trigger_action' => $trigger_action, |
| 724 |
'trigger_context' => $trigger_context, |
| 725 |
); |
| 726 |
$alert_meta = apply_filters( 'wp_stream_alerts_save_meta', $alert_meta, $alert_type ); |
| 727 |
add_post_meta( $post_id, 'alert_meta', $alert_meta ); |
| 728 |
wp_send_json_success( array( 'success' => true ) ); |
| 729 |
} |
| 730 |
|
| 731 |
/** |
| 732 |
* |
| 733 |
*/ |
| 734 |
function get_new_alert_triggers_notifications() { |
| 735 |
ob_start(); |
| 736 |
?> |
| 737 |
<fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-triggers"> |
| 738 |
<legend class="inline-edit-legend">Add New</legend> |
| 739 |
<?php $GLOBALS['wp_stream']->alerts->display_triggers_box(); ?> |
| 740 |
</fieldset> |
| 741 |
<fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-notifications"> |
| 742 |
<?php $GLOBALS['wp_stream']->alerts->display_notification_box(); ?> |
| 743 |
</fieldset> |
| 744 |
<fieldset class="inline-edit-col inline-edit-wp_stream_alerts inline-edit-add-new-status"> |
| 745 |
<?php $GLOBALS['wp_stream']->alerts->display_status_box(); ?> |
| 746 |
</fieldset> |
| 747 |
<?php |
| 748 |
$html = ob_get_clean(); |
| 749 |
wp_send_json_success( array( 'success' => true, 'html' => $html ) ); |
| 750 |
} |
| 751 |
/** |
| 752 |
* Add action links to Stream drop row in admin list screen |
| 753 |
* |
| 754 |
* @filter wp_stream_action_links_{connector} |
| 755 |
* |
| 756 |
* @param array $links Previous links registered |
| 757 |
* @param Record $record Stream record |
| 758 |
* |
| 759 |
* @return array Action links |
| 760 |
*/ |
| 761 |
function change_alert_action_links( $links, $record ) { |
| 762 |
$post = get_post( $record->object_id ); |
| 763 |
|
| 764 |
if ( $post && self::POST_TYPE === $post->post_type && $post->post_status === $record->get_meta( 'new_status', true ) ) { |
| 765 |
if ( 'trash' !== $post->post_status ) { |
| 766 |
$connector_posts = new \WP_Stream\Connector_Posts; |
| 767 |
$post_type_name = $connector_posts->get_post_type_name( get_post_type( $post->ID ) ); |
| 768 |
|
| 769 |
$links[ sprintf( esc_html_x( 'Edit %s', 'Post type singular name', 'stream' ), $post_type_name ) ] = admin_url( 'edit.php?post_type=wp_stream_alerts#post-' . $post->ID ); |
| 770 |
unset( $links[ esc_html__( 'View', 'stream' ) ] ); |
| 771 |
} |
| 772 |
} |
| 773 |
return $links; |
| 774 |
} |
| 775 |
} |
| 776 |
|