PluginProbe
Stream – Activity Log & Audit Trail / 3.2.2
Stream – Activity Log & Audit Trail v3.2.2
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / alerts / class-alert-trigger-context.php

class-alert-trigger-context.php in Stream – Activity Log & Audit Trail 3.2.2, at alerts/class-alert-trigger-context.php

189 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Trigger on Context.
4 *
5 * @package WP_Stream
6 */
7 namespace WP_Stream;
8
9 /**
10 * Class Alert_Trigger_Context
11 *
12 * @package WP_Stream
13 */
14 class Alert_Trigger_Context extends Alert_Trigger {
15
16 /**
17 * Unique identifier
18 *
19 * @var string
20 */
21 public $slug = 'context';
22
23 /**
24 * Field key used in database
25 *
26 * @var string
27 */
28 public $field_key = 'wp_stream_trigger_context';
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_connector'] ) && $recordarr['connector'] !== $alert->alert_meta['trigger_connector'] ) {
42 return false;
43 }
44 if ( ! empty( $alert->alert_meta['trigger_context'] ) && $recordarr['context'] !== $alert->alert_meta['trigger_context'] ) {
45 return false;
46 }
47 return $success;
48 }
49
50 /**
51 * Adds fields to the trigger form.
52 *
53 * @see Alert_Trigger::add_fields().
54 * @param Form_Generator $form The Form Object to add to.
55 * @param Alert $alert The Alert being worked on.
56 * @return void
57 */
58 public function add_fields( $form, $alert = array() ) {
59 $connector = '';
60 if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_connector'] ) ) {
61 $connector = $alert->alert_meta['trigger_connector'];
62 }
63
64 $context = '';
65 if ( is_object( $alert ) && ! empty( $alert->alert_meta['trigger_context'] ) ) {
66 $context = $alert->alert_meta['trigger_context'];
67 }
68
69 // Context dropdown menu.
70 $context_values = array();
71
72 $form->add_field( 'select2', array(
73 'name' => 'wp_stream_trigger_connector_or_context',
74 'options' => $this->get_values(),
75 'classes' => 'wp_stream_ajax_forward connector_or_context',
76 'data' => array(
77 'placeholder' => __( 'Any Context', 'stream' ),
78 ),
79 ) );
80
81 $form->add_field( 'hidden', array(
82 'name' => 'wp_stream_trigger_connector',
83 'value' => $connector,
84 'classes' => 'connector wp_stream_ajax_forward',
85 ) );
86
87 $form->add_field( 'hidden', array(
88 'name' => 'wp_stream_trigger_context',
89 'value' => $context,
90 'classes' => 'context wp_stream_ajax_forward',
91 ) );
92 }
93
94 /**
95 * Validate and save Alert object
96 *
97 * @see Alert_Trigger::save_fields().
98 * @param Alert $alert The Alert being worked on.
99 * @return void
100 */
101 public function save_fields( $alert ) {
102 $alert->alert_meta['trigger_connector'] = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_connector' );
103 $alert->alert_meta['trigger_context'] = wp_stream_filter_input( INPUT_POST, 'wp_stream_trigger_context' );
104 }
105
106 /**
107 * Generate array of possible action values
108 *
109 * @return array
110 */
111 public function get_values() {
112 foreach ( $this->get_terms_labels( 'context' ) as $context_id => $context_data ) {
113 if ( is_array( $context_data ) ) {
114 $child_values = array();
115 if ( isset( $context_data['children'] ) ) {
116 $child_values = array();
117 foreach ( $context_data['children'] as $child_id => $child_value ) {
118 $child_values[] = array( 'value' => $context_id . '-' . $child_id, 'id' => $context_id . '-' . $child_id, 'text' => $child_value, 'parent' => $context_id );
119 }
120 }
121 if ( isset( $context_data['label'] ) ) {
122 $context_values[] = array( 'value' => $context_id, 'id' => $context_id, 'text' => $context_data['label'], 'children' => $child_values );
123 }
124 } else {
125 $context_values[] = array( 'value' => $context_id, 'id' => $context_id, 'text' => $context_data );
126 }
127 }
128 return $context_values;
129 }
130
131 /**
132 * Function will return all terms labels of given column
133 *
134 * @todo refactor Settings::get_terms_labels into general utility
135 * @param string $column string Name of the column.
136 * @return array
137 */
138 public function get_terms_labels( $column ) {
139 $return_labels = array();
140
141 if ( isset( $this->plugin->connectors->term_labels[ 'stream_' . $column ] ) ) {
142 if ( 'context' === $column && isset( $this->plugin->connectors->term_labels['stream_connector'] ) ) {
143 $connectors = $this->plugin->connectors->term_labels['stream_connector'];
144 $contexts = $this->plugin->connectors->term_labels['stream_context'];
145
146 foreach ( $connectors as $connector => $connector_label ) {
147 $return_labels[ $connector ]['label'] = $connector_label;
148 foreach ( $contexts as $context => $context_label ) {
149 if ( isset( $this->plugin->connectors->contexts[ $connector ] ) && array_key_exists( $context, $this->plugin->connectors->contexts[ $connector ] ) ) {
150 $return_labels[ $connector ]['children'][ $context ] = $context_label;
151 }
152 }
153 }
154 } else {
155 $return_labels = $this->plugin->connectors->term_labels[ 'stream_' . $column ];
156 }
157
158 ksort( $return_labels );
159 }
160 return $return_labels;
161 }
162
163 /**
164 * Returns the trigger's value for the given alert.
165 *
166 * @see Alert_Trigger::get_display_value().
167 * @param string $context The location this data will be displayed in.
168 * @param Alert $alert Alert being processed.
169 * @return string
170 */
171 function get_display_value( $context = 'normal', $alert ) {
172 $context = ( ! empty( $alert->alert_meta['trigger_context'] ) ) ? $alert->alert_meta['trigger_context'] : null;
173 $connector = ( ! empty( $alert->alert_meta['trigger_connector'] ) ) ? $alert->alert_meta['trigger_connector'] : null;
174 if ( empty( $context ) && empty( $connector ) ) {
175 $context = __( 'Any Context', 'stream' );
176 } else {
177 $term_labels = $this->get_terms_labels( 'context' );
178 if ( ! empty( $term_labels[ $connector ]['children'][ $context ] ) ) {
179 $context = $term_labels[ $connector ]['children'][ $context ];
180 } else {
181 if ( ! empty( $term_labels[ $connector ]['label'] ) ) {
182 $context = $term_labels[ $connector ]['label'];
183 }
184 }
185 }
186 return ucfirst( $context );
187 }
188 }
189