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