| 1 |
<?php |
| 2 |
/** |
| 3 |
* Trigger for an Author. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
namespace WP_Stream; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Alert_Trigger_Author |
| 11 |
* |
| 12 |
* @package WP_Stream |
| 13 |
*/ |
| 14 |
class Alert_Trigger_Author extends Alert_Trigger { |
| 15 |
|
| 16 |
/** |
| 17 |
* Unique identifier |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
public $slug = 'author'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Field key used in database |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $field_key = 'wp_stream_trigger_author'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Checks if a record matches the criteria from the trigger. |
| 32 |
* |
| 33 |
* @see Alert_Trigger::check_record(). |
| 34 |
* @param bool $success Status of previous checks. |
| 35 |
* @param int $record_id Record ID. |
| 36 |
* @param array $recordarr Record data. |
| 37 |
* @param Alert $alert The Alert being worked on. |
| 38 |
* @return bool False on failure, otherwise should return original value of $success. |
| 39 |
*/ |
| 40 |
public function check_record( $success, $record_id, $recordarr, $alert ) { |
| 41 |
if ( ! empty( $alert->alert_meta['trigger_author'] ) && intval( $alert->alert_meta['trigger_author'] ) !== intval( $recordarr['user_id'] ) ) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
return $success; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Adds fields to the trigger form. |
| 49 |
* |
| 50 |
* @see Alert_Trigger::add_fields(). |
| 51 |
* @param Form_Generator $form The Form Object to add to. |
| 52 |
* @param Alert $alert The Alert being worked on. |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function add_fields( $form, $alert = array() ) { |
| 56 |
$value = ''; |
| 57 |
if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_author'] ) ) { |
| 58 |
$value = $alert->alert_meta['trigger_author']; |
| 59 |
} |
| 60 |
|
| 61 |
$args = array( |
| 62 |
'name' => esc_attr( $this->field_key ), |
| 63 |
'value' => esc_attr( $value ), |
| 64 |
'options' => $this->get_values(), |
| 65 |
'classes' => 'wp_stream_ajax_forward', |
| 66 |
'data' => array( |
| 67 |
'placeholder' => __( 'Any Author', 'stream' ), |
| 68 |
), |
| 69 |
); |
| 70 |
$form->add_field( 'select2', $args ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Generate array of possible action values |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function get_values() { |
| 79 |
$all_records = array(); |
| 80 |
|
| 81 |
$user_count = count_users(); |
| 82 |
$total_users = $user_count['total_users']; |
| 83 |
|
| 84 |
if ( $total_users > $this->plugin->admin->preload_users_max ) { |
| 85 |
return array(); |
| 86 |
} |
| 87 |
|
| 88 |
$users = array_map( |
| 89 |
function( $user_id ) { |
| 90 |
return new Author( $user_id ); |
| 91 |
}, |
| 92 |
get_users( array( 'fields' => 'ID' ) ) |
| 93 |
); |
| 94 |
|
| 95 |
if ( is_multisite() && is_super_admin() ) { |
| 96 |
$super_admins = array_map( |
| 97 |
function( $login ) { |
| 98 |
$user = get_user_by( 'login', $login ); |
| 99 |
return new Author( $user->ID ); |
| 100 |
}, |
| 101 |
get_super_admins() |
| 102 |
); |
| 103 |
$users = array_unique( array_merge( $users, $super_admins ) ); |
| 104 |
} |
| 105 |
|
| 106 |
$users[] = new Author( 0, array( 'is_wp_cli' => true ) ); |
| 107 |
|
| 108 |
foreach ( $users as $user ) { |
| 109 |
$all_records[] = array( |
| 110 |
'id' => $user->id, |
| 111 |
'value' => $user->id, |
| 112 |
'text' => $user->get_display_name(), |
| 113 |
); |
| 114 |
} |
| 115 |
return $all_records; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Validate and save Alert object |
| 120 |
* |
| 121 |
* @see Alert_Trigger::save_fields(). |
| 122 |
* @param Alert $alert The Alert being worked on. |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function save_fields( $alert ) { |
| 126 |
$input = wp_stream_filter_input( INPUT_POST, $this->field_key ); |
| 127 |
if ( array_key_exists( $input, $this->get_values( $alert ) ) ) { |
| 128 |
$alert->alert_meta['trigger_author'] = $input; |
| 129 |
} else { |
| 130 |
$alert->alert_meta['trigger_author'] = ''; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns the trigger's value for the given alert. |
| 136 |
* |
| 137 |
* @see Alert_Trigger::get_display_value(). |
| 138 |
* @param string $context The location this data will be displayed in. |
| 139 |
* @param Alert $alert Alert being processed. |
| 140 |
* @return string |
| 141 |
*/ |
| 142 |
function get_display_value( $context = 'normal', $alert ) { |
| 143 |
$author = ( ! empty( $alert->alert_meta['trigger_author'] ) ) ? $alert->alert_meta['trigger_author'] : null; |
| 144 |
if ( empty( $author ) ) { |
| 145 |
$author = __( 'Any User', 'stream' ); |
| 146 |
} elseif ( is_numeric( $author ) ) { |
| 147 |
$author_data = get_userdata( $author ); |
| 148 |
if ( $author_data ) { |
| 149 |
$author = $author_data->display_name; |
| 150 |
} else { |
| 151 |
$author = __( 'Unknown User', 'stream' ); |
| 152 |
} |
| 153 |
} |
| 154 |
return ucfirst( $author ); |
| 155 |
} |
| 156 |
} |
| 157 |
|