PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.9
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.9
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
gamipress / includes / admin / meta-boxes / log-extra-data-ui.php

log-extra-data-ui.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.9, at includes/admin/meta-boxes/log-extra-data-ui.php

350 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Log Extra Data UI
4 *
5 * @package GamiPress\Admin\Log_Extra_Data_UI
6 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Add log data and extra data meta box
14 *
15 * @since 1.0.0
16 * @return void
17 */
18 function gamipress_add_log_extra_data_ui_meta_box() {
19
20 add_meta_box( 'gamipress_log_details_ui', __( 'Log Details', 'gamipress' ), 'gamipress_log_details_ui_meta_box', 'gamipress_logs', 'side', 'default' );
21 add_meta_box( 'gamipress_log_extra_data_ui', __( 'Extra Data', 'gamipress' ), 'gamipress_log_extra_data_ui_meta_box', 'gamipress_logs', 'normal', 'default' );
22
23 }
24 add_action( 'add_meta_boxes', 'gamipress_add_log_extra_data_ui_meta_box' );
25
26 /**
27 * Renders the log data meta box
28 *
29 * @since 1.2.8
30 *
31 * @param stdClass $object The current object
32 */
33 function gamipress_log_details_ui_meta_box( $object = null ) {
34 ?>
35 <div id="log-data-ui">
36 <?php gamipress_log_details_ui_html( $object, $object->type ); ?>
37 </div>
38 <?php
39 }
40
41 /**
42 * Renders the log data meta box HTML
43 *
44 * @since 1.2.8
45 *
46 * @param stdClass $object The current object
47 * @param string $type Type to render form
48 */
49 function gamipress_log_details_ui_html( $object, $type ) {
50
51 global $ct_table;
52
53 // Setup vars
54 $log_types = gamipress_get_log_types();
55 $primary_key = $ct_table->db->primary_key;
56 $object_id = $object->$primary_key;
57
58 ?>
59 <div class="submitbox" id="submitpost" style="margin: -6px -12px -12px;">
60
61 <div id="minor-publishing">
62
63 <div id="misc-publishing-actions">
64
65 <div class="misc-pub-section misc-pub-post-status">
66 <?php echo __( 'Type:', 'gamipress' ); ?> <span id="post-status-display"><?php echo isset( $log_types[$object->type] ) ? $log_types[$object->type] : $object->type ; ?></span>
67 </div>
68
69 <div class="misc-pub-section misc-pub-visibility" id="visibility">
70 <?php echo __( 'Visibility:', 'gamipress' ); ?> <span id="post-visibility-display"><?php echo $object->access === 'public' ? __( 'Public', 'gamipress' ) : __( 'Private', 'gamipress' ); ?></span>
71 </div>
72
73 <div class="misc-pub-section curtime misc-pub-curtime">
74 <span id="timestamp"><?php echo __( 'Date:', 'gamipress' ); ?> <b><abbr title="<?php echo date( 'Y/m/d g:i:s a', strtotime( $object->date ) ); ?>"><?php echo date( 'Y/m/d', strtotime( $object->date ) ); ?></abbr></b></span>
75 </div>
76
77 </div>
78
79 </div>
80
81 <div id="major-publishing-actions">
82
83 <div id="delete-action">
84 <?php
85 printf(
86 '<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>',
87 ct_get_delete_link( $ct_table->name, $object_id ),
88 "return confirm('" .
89 esc_attr( __( "Are you sure you want to delete this item?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the delete." ) ) .
90 "');",
91 esc_attr( __( 'Delete permanently' ) ),
92 __( 'Delete Permanently' )
93 );
94 ?>
95 </div>
96
97 <div id="publishing-action">
98 <span class="spinner"></span>
99 <?php submit_button( __( 'Save Changes' ), 'primary large', 'ct-save', false ); ?>
100 </div>
101
102 <div class="clear"></div>
103
104 </div>
105
106 </div>
107 <?php
108 }
109
110 /**
111 * Renders the log extra data
112 *
113 * @since 1.0.0
114 * @param stdClass $object The current object
115 */
116 function gamipress_log_extra_data_ui_meta_box( $object = null ) {
117 ?>
118 <div id="log-extra-data-ui">
119 <?php gamipress_log_extra_data_ui_html( $object, $object->log_id, $object->type ); ?>
120 </div>
121 <?php
122 }
123
124 /**
125 * Renders the HTML for meta box based on the log type given
126 *
127 * @since 1.0.0
128 *
129 * @param stdClass $object The current object
130 * @param integer $object_id The current object ID
131 * @param string $type Type to render form
132 */
133 function gamipress_log_extra_data_ui_html( $object, $object_id, $type ) {
134
135 // Start with an underscore to hide fields from custom fields list
136 $prefix = '_gamipress_';
137 $fields = array();
138
139 if( $type === 'event_trigger' ) {
140
141 $fields = array(
142 array(
143 'name' => __( 'Trigger', 'gamipress' ),
144 'tooltip' => __( 'The event user has triggered.', 'gamipress' ),
145 'label_cb' => 'cmb_tooltip_label_cb',
146 'id' => ( ( ! is_gamipress_upgraded_to( '1.4.7' ) ) ? $prefix : '' ) . 'trigger_type',
147 'type' => 'advanced_select',
148 'options' => gamipress_get_activity_triggers(),
149 ),
150 array(
151 'name' => __( 'Count', 'gamipress' ),
152 'tooltip' => __( 'Number of times user triggered this event until this log.', 'gamipress' ),
153 'label_cb' => 'cmb_tooltip_label_cb',
154 'id' => $prefix . 'count',
155 'type' => 'text',
156 ),
157 );
158
159 if( ! is_gamipress_upgraded_to( '1.4.7' ) ) {
160 $trigger = ct_get_object_meta( $object_id, $prefix . 'trigger_type', true );
161 } else {
162 $trigger = $object->trigger_type;
163 }
164
165 // If is a specific activity trigger, then add the achievement_post field
166 if( in_array( $trigger, array_keys( gamipress_get_specific_activity_triggers() ) ) ) {
167
168 $achievement_post_id = ct_get_object_meta( $object_id, $prefix . 'achievement_post', true );
169 $achievement_post_site_id = ct_get_object_meta( $object_id, $prefix . 'achievement_post_site_id', true );
170
171 $fields[] = array(
172 'name' => __( 'Assigned Post', 'gamipress' ),
173 'tooltip' => __( 'Attached post to this log.', 'gamipress' ),
174 'label_cb' => 'cmb_tooltip_label_cb',
175 'id' => $prefix . 'achievement_post',
176 'type' => 'select',
177 'options' => array(
178 $achievement_post_id => gamipress_get_specific_activity_trigger_post_title( $achievement_post_id, $trigger, $achievement_post_site_id ),
179 ),
180 );
181 }
182 } else if( $type === 'achievement_earn' || $type === 'achievement_award' ) {
183 $achievement_id = ct_get_object_meta( $object_id, $prefix . 'achievement_id', true );
184
185 $fields = array(
186 array(
187 'name' => __( 'Achievement', 'gamipress' ),
188 'tooltip' => __( 'Achievement user has earned.', 'gamipress' ),
189 'label_cb' => 'cmb_tooltip_label_cb',
190 'id' => $prefix . 'achievement_id',
191 'type' => 'select',
192 'options' => array(
193 $achievement_id => gamipress_get_post_field( 'post_title', $achievement_id ),
194 ),
195 ),
196 );
197 } else if( in_array( $type, array( 'points_award', 'points_earn', 'points_revoke', 'points_deduct', 'points_expend' ) ) ) {
198 // Grab our points types as an array
199 $points_types_options = array(
200 '' => 'Default'
201 );
202
203 foreach( gamipress_get_points_types() as $slug => $data ) {
204 $points_types_options[$slug] = $data['plural_name'];
205 }
206
207 $fields = array(
208 array(
209 'name' => __( 'Points', 'gamipress' ),
210 'tooltip' => ( $type === 'points_award' || $type === 'points_earn' ? __( 'Points user has earned.', 'gamipress' ) : __( 'Points deducted to user.', 'gamipress' ) ),
211 'label_cb' => 'cmb_tooltip_label_cb',
212 'id' => $prefix . 'points',
213 'type' => 'text_small',
214 ),
215 array(
216 'name' => __( 'Points Type', 'gamipress' ),
217 'tooltip' => ( $type === 'points_award' || $type === 'points_earn' ? __( 'Points type user has earned.', 'gamipress' ) : __( 'Points type deducted to user.', 'gamipress' ) ),
218 'label_cb' => 'cmb_tooltip_label_cb',
219 'id' => $prefix . 'points_type',
220 'type' => 'select',
221 'options' => $points_types_options
222 ),
223 array(
224 'name' => __( 'Total Points', 'gamipress' ),
225 'tooltip' => __( 'Total points user has earned until this log.', 'gamipress' ),
226 'label_cb' => 'cmb_tooltip_label_cb',
227 'id' => $prefix . 'total_points',
228 'type' => 'text_small',
229 ),
230 );
231
232 } else if( $type === 'rank_earn' || $type === 'rank_award' ) {
233 $rank_id = ct_get_object_meta( $object_id, $prefix . 'rank_id', true );
234
235 $fields = array(
236 array(
237 'name' => __( 'Rank', 'gamipress' ),
238 'tooltip' => __( 'Rank user has earned.', 'gamipress' ),
239 'label_cb' => 'cmb_tooltip_label_cb',
240 'id' => $prefix . 'rank_id',
241 'type' => 'select',
242 'options' => array(
243 $rank_id => gamipress_get_post_field( 'post_title', $rank_id ),
244 ),
245 ),
246 );
247 }
248
249 if( in_array( $type, array( 'achievement_award', 'points_award', 'points_revoke', 'rank_award' ) ) ) {
250 $admin_id = ct_get_object_meta( $object_id, $prefix . 'admin_id', true );
251 $admin = get_userdata( $admin_id );
252
253 $fields[] = array(
254 'name' => __( 'Administrator', 'gamipress' ),
255 'tooltip' => ( $type === 'points_revoke' ? __( 'User has made the revoke.', 'gamipress' ) : __( 'User has made the award.', 'gamipress' ) ),
256 'label_cb' => 'cmb_tooltip_label_cb',
257 'id' => $prefix . 'admin_id',
258 'type' => 'select',
259 'options' => array(
260 $admin_id => $admin->user_login,
261 ),
262 );
263 }
264
265 /**
266 * Log extra data fields
267 *
268 * @since 1.0.0
269 * @updated 1.6.3 Added $object parameter
270 *
271 * @param array $fields Log extra fields.
272 * @param int $log_id The log ID.
273 * @param string $type The log type. See gamipress_get_log_types().
274 * @param stdClass $object The log object.
275 *
276 * @return array
277 */
278 $fields = apply_filters( 'gamipress_log_extra_data_fields', $fields, $object_id, $type, $object );
279
280 if( ! empty( $fields ) ) {
281
282 // Create a new box to render the form
283 $cmb2 = new CMB2( array(
284 'id' => 'log_extra_data_ui_box',
285 'classes' => 'gamipress-form gamipress-box-form',
286 'hookup' => false,
287 'show_on' => array(
288 'key' => 'gamipress_logs',
289 'value' => $object_id
290 ),
291 'fields' => $fields
292 ) );
293
294 $cmb2->object_id( $object_id );
295
296 $cmb2->show_form();
297
298 } else {
299
300 _e( 'No extra data registered', 'gamipress' );
301
302 }
303
304 }
305
306 /**
307 * AJAX Handler for retrieve the HTML with
308 *
309 * @since 1.0.0
310 * @return void
311 */
312 function gamipress_get_log_extra_data_ui_ajax_handler() {
313 // Security check, forces to die if not security passed
314 check_ajax_referer( 'gamipress_admin', 'nonce' );
315
316 ct_setup_table( 'gamipress_logs' );
317
318 $ct_object = ct_get_object( absint( $_REQUEST['object_id'] ) );
319
320 gamipress_log_extra_data_ui_html( $ct_object, $ct_object->log_id, sanitize_text_field( $_REQUEST['type'] ) );
321 die;
322 }
323 add_action( 'wp_ajax_gamipress_get_log_extra_data_ui', 'gamipress_get_log_extra_data_ui_ajax_handler' );
324
325 /**
326 * Helper function to recursive search an array key
327 *
328 * @since 1.0.0
329 *
330 * @param $needle_key
331 * @param $array
332 *
333 * @return bool|mixed
334 */
335 function gamipress_array_search_key( $needle_key, $array ) {
336
337 foreach( $array as $key => $value ) {
338
339 if( $key == $needle_key )
340 return $value;
341
342 if( is_array( $value ) ) {
343 if( ( $result = gamipress_array_search_key( $needle_key, $value ) ) !== false )
344 return $result;
345 }
346 }
347
348 return false;
349
350 }