PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.3
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.3
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 7.8.7 All 46 releases
gamipress / libraries / ct / includes / class-ct-edit-view.php

class-ct-edit-view.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 8.0.3, at libraries/ct/includes/class-ct-edit-view.php

571 lines 22.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Edit View class
4 *
5 * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]>
6 *
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 defined( 'ABSPATH' ) || exit;
11
12 if ( ! class_exists( 'CT_Edit_View' ) ) :
13
14 class CT_Edit_View extends CT_View {
15
16 protected $object_id = 0;
17 protected $object = null;
18 protected $editing = false;
19 protected $message = false;
20 protected $columns = 2;
21
22 public function __construct( $name, $args ) {
23
24 parent::__construct( $name, $args );
25
26 $this->columns = isset( $args['columns'] ) ? $args['columns'] : 2;
27
28 }
29
30 public function init() {
31
32 global $ct_registered_tables, $ct_table;
33
34 if( ! isset( $ct_registered_tables[$this->name] ) ) {
35 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
36 }
37
38 // Setup global $ct_table
39 $ct_table = $ct_registered_tables[$this->name];
40
41 // If not CT object, die
42 if ( ! $ct_table )
43 wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) );
44
45 // If not CT object allow ui, die
46 if ( ! $ct_table->show_ui ) {
47 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) );
48 }
49
50 if( isset( $_POST['ct-save'] ) ) {
51 // Saving
52 $this->save();
53 }
54
55 $primary_key = $ct_table->db->primary_key;
56
57 if( isset( $_GET[$primary_key] ) ) {
58 // Editing object
59 $this->object_id = (int) $_GET[$primary_key];
60 $this->object = $ct_table->db->get( $this->object_id );
61 $this->editing = true;
62
63 // If not id, return to list
64 if ( empty( $this->object_id ) ) {
65 wp_redirect( ct_get_list_link( $ct_table->name ) );
66 exit();
67 }
68
69 // If not object, die
70 if ( ! $this->object ) {
71 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_deleted' ) ) );
72 }
73
74 // If not current user can edit, die
75 if ( ! current_user_can( $ct_table->cap->edit_item, $this->object_id ) ) {
76 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_not_allowed' ) ) );
77 }
78
79 } else {
80 // See filter "ct_{$ct_table->name}_default_data"
81 $this->object_id = ct_insert_object( array() );
82
83 // If not id, return to list
84 if ( empty( $this->object_id ) ) {
85 wp_redirect( ct_get_list_link( $ct_table->name ) );
86 exit();
87 }
88
89 $this->object = ct_get_object( $this->object_id );
90
91 // If not object, die
92 if ( ! $this->object )
93 wp_die( esc_html__( 'Unable to create the draft item.', 'ct' ) );
94
95 // If not current user can create, die
96 if ( ! current_user_can( $ct_table->cap->create_items, $this->object_id )
97 && ! current_user_can( $ct_table->cap->edit_item, $this->object_id ) ) {
98 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) );
99 }
100
101 // Redirect to edit screen to prevent add a draft item multiples times
102 wp_redirect( ct_get_edit_link( $ct_table->name, $this->object_id ) );
103 }
104
105 }
106
107 /**
108 * Screen settings text displayed in the Screen Options tab.
109 *
110 * @param string $screen_settings Screen settings.
111 * @param WP_Screen $screen WP_Screen object.
112 */
113 public function screen_settings( $screen_settings, $screen ) {
114
115 $this->render_meta_boxes_preferences();
116 $this->render_screen_layout();
117
118 }
119
120 /**
121 * Render the meta boxes preferences.
122 *
123 * @since 1.0.0
124 *
125 * @global array $wp_meta_boxes
126 */
127 public function render_meta_boxes_preferences() {
128
129 global $wp_meta_boxes, $ct_table;
130
131 // TODO: Forced to place it here until fix issue with load-{pagehook}
132
133 /** This action is documented in wp-admin/edit-form-advanced.php */
134 do_action( 'add_meta_boxes', $ct_table->name, $this->object );
135
136 /** This action is documented in wp-admin/edit-form-advanced.php */
137 do_action( "add_meta_boxes_{$ct_table->name}", $this->object );
138
139 /** This action is documented in wp-admin/edit-form-advanced.php */
140 do_action( 'do_meta_boxes', $ct_table->name, 'normal', $this->object );
141 /** This action is documented in wp-admin/edit-form-advanced.php */
142 do_action( 'do_meta_boxes', $ct_table->name, 'advanced', $this->object );
143 /** This action is documented in wp-admin/edit-form-advanced.php */
144 do_action( 'do_meta_boxes', $ct_table->name, 'side', $this->object );
145
146 if ( ! isset( $wp_meta_boxes[ $ct_table->name ] ) ) {
147 return;
148 }
149 ?>
150
151 <fieldset class="metabox-prefs">
152 <legend><?php esc_html_e( 'Screen elements' ); ?></legend>
153 <p>
154 <?php esc_html_e( 'Some screen elements can be shown or hidden by using the checkboxes.' ); ?>
155 <?php esc_html_e( 'Expand or collapse the elements by clicking on their headings, and arrange them by dragging their headings or by clicking on the up and down arrows.' ); ?>
156 </p>
157 <?php meta_box_prefs( $ct_table->name ); ?>
158 </fieldset>
159
160 <?php
161 }
162
163 /**
164 * Render the option for number of columns on the page
165 *
166 * @since 1.0.0
167 */
168 public function render_screen_layout() {
169
170 if ( $this->columns <= 1 ) {
171 return;
172 }
173
174 $screen_layout_columns = get_current_screen()->get_columns();
175
176 if( ! $screen_layout_columns ) {
177 $screen_layout_columns = $this->columns;
178 }
179
180 $num = $this->columns;
181
182 ?>
183 <fieldset class='columns-prefs'>
184 <legend class="screen-layout"><?php esc_html_e( 'Layout' ); ?></legend><?php
185 for ( $i = 1; $i <= $num; ++$i ):
186 ?>
187 <label class="columns-prefs-<?php echo esc_attr ( $i ); ?>">
188 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
189 <?php checked( $screen_layout_columns, $i ); ?> />
190 <?php
191 // translators: %s: Column name
192 echo esc_html( sprintf( _n( '%s column', '%s columns', $i ), number_format_i18n( $i ) ) );
193 ?>
194 </label>
195 <?php
196 endfor; ?>
197 </fieldset>
198 <?php
199 }
200
201 public function submit_meta_box( $object ) {
202
203 global $ct_table;
204
205 /**
206 * Fires at top of the submit meta box.
207 *
208 * @since 1.0.0
209 *
210 * @param object $object Object.
211 * @param CT_Table $ct_table CT Table object.
212 * @param bool $editing True if edit screen, false if is adding a new one.
213 * @param CT_Edit_View $view Edit view object.
214 */
215 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_top", $object, $ct_table, $this->editing, $this );
216
217 $submit_label = __( 'Add' );
218
219 if( $this->editing ) {
220 $submit_label = __( 'Update' );
221 }
222
223 /**
224 * Filter to override the submit button label.
225 *
226 * @since 1.0.0
227 *
228 * @param string $submit_label The submit label.
229 * @param object $object Object.
230 * @param CT_Table $ct_table CT Table object.
231 * @param bool $editing True if edit screen, false if is adding a new one.
232 * @param CT_Edit_View $view Edit view object.
233 */
234 $submit_label = apply_filters( "ct_{$ct_table->name}_edit_screen_submit_label", $submit_label, $object, $ct_table, $this->editing, $this );
235
236 $primary_key = $ct_table->db->primary_key;
237 $object_id = $object->$primary_key;
238
239 ?>
240
241 <div class="submitbox" id="submitpost">
242
243 <?php
244 /**
245 * Fires at top of submit meta box submit post element.
246 *
247 * @since 1.0.0
248 *
249 * @param object $object Object.
250 * @param CT_Table $ct_table CT Table object.
251 * @param bool $editing True if edit screen, false if is adding a new one.
252 * @param CT_Edit_View $view Edit view object.
253 */
254 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_submit_post_top", $object, $ct_table, $this->editing, $this ); ?>
255
256 <div id="minor-publishing">
257
258 <?php ob_start();
259 /**
260 * Fires inside minor publishing actions from submit meta box.
261 *
262 * @since 1.0.0
263 *
264 * @param object $object Object.
265 * @param CT_Table $ct_table CT Table object.
266 * @param bool $editing True if edit screen, false if is adding a new one.
267 * @param CT_Edit_View $view Edit view object.
268 */
269 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_minor_publishing_actions", $object, $ct_table, $this->editing, $this );
270 $minor_publishing_actions = ob_get_clean(); ?>
271
272 <?php // Since minor-publishing-actions has a margin, check if minor publishing actions has any content to render it or not
273 if( ! empty( $minor_publishing_actions ) ) : ?>
274 <div id="minor-publishing-actions"><?php echo $minor_publishing_actions; ?></div>
275 <?php endif; ?>
276
277 <?php ob_start();
278 /**
279 * Fires inside misc publishing actions from submit meta box.
280 *
281 * @since 1.0.0
282 *
283 * @param object $object Object.
284 * @param CT_Table $ct_table CT Table object.
285 * @param bool $editing True if edit screen, false if is adding a new one.
286 * @param CT_Edit_View $view Edit view object.
287 */
288 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_misc_publishing_actions", $object, $ct_table, $this->editing, $this );
289 $misc_publishing_actions = ob_get_clean(); ?>
290
291 <?php // Since misc-publishing-actions has a margin, check if misc publishing actions has any content to render it or not
292 if( ! empty( $misc_publishing_actions ) ) : ?>
293 <div id="misc-publishing-actions"><?php echo $misc_publishing_actions; ?></div>
294 <?php endif; ?>
295
296 <div class="clear"></div>
297
298 </div>
299
300 <div id="major-publishing-actions">
301
302 <?php
303 if ( current_user_can( $ct_table->cap->delete_item, $object_id ) ) {
304
305 printf(
306 '<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>',
307 esc_attr( ct_get_delete_link( $ct_table->name, $object_id ) ),
308 "return confirm('" .
309 esc_attr( ct_get_table_label( $ct_table->name, 'delete_item_confirm' ) ) .
310 "');",
311 esc_attr( __( 'Delete permanently' ) ),
312 esc_html__( 'Delete Permanently' )
313 );
314
315 } ?>
316
317 <div id="publishing-action">
318 <span class="spinner"></span>
319 <?php submit_button( $submit_label, 'primary large', 'ct-save', false ); ?>
320 <input type="button" name="" id="ct-save-disabled" class="button button-primary" value="<?php echo esc_attr( $submit_label ); ?>" disabled="disabled" style="display: none;">
321
322 </div>
323
324 <div class="clear"></div>
325
326 </div>
327
328 <?php
329 /**
330 * Fires at bottom of submit meta box submit post element.
331 *
332 * @since 1.0.0
333 *
334 * @param object $object Object.
335 * @param CT_Table $ct_table CT Table object.
336 * @param bool $editing True if edit screen, false if is adding a new one.
337 * @param CT_Edit_View $view Edit view object.
338 */
339 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_submit_post_bottom", $object, $ct_table, $this->editing, $this ); ?>
340
341 </div>
342
343 <?php
344
345 /**
346 * Fires at bottom the submit meta box.
347 *
348 * @since 1.0.0
349 *
350 * @param object $object Object.
351 * @param CT_Table $ct_table CT Table object.
352 * @param bool $editing True if edit screen, false if is adding a new one.
353 * @param CT_Edit_View $view Edit view object.
354 */
355 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_bottom", $object, $ct_table, $this->editing, $this );
356 }
357
358 public function save() {
359
360 global $ct_registered_tables, $ct_table;
361
362 // If not CT object, die
363 if ( ! $ct_table )
364 wp_die( esc_html__( 'Invalid object type.' ) );
365
366 // If not CT object allow ui, die
367 if ( ! $ct_table->show_ui ) {
368 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) );
369 }
370
371 $primary_key = $ct_table->db->primary_key;
372
373 if( ! isset( $_POST[$primary_key] ) ) {
374 wp_die( esc_html__( 'Invalid object type.' ) );
375 }
376
377 $object_id = $_POST[$primary_key];
378
379 // Nonce check
380 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'ct_edit_' . $object_id ) ) {
381 wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_not_allowed' ) ) );
382 }
383
384 $object_data = map_deep( $_POST, 'wp_kses_post' );
385
386 unset( $object_data['ct-save'] );
387
388 $success = ct_update_object( $object_data );
389
390 $location = add_query_arg( array( $primary_key => $object_id ), $this->get_link() );
391
392 if( $success ) {
393 $location = add_query_arg( array( 'message' => 1 ), $location );
394 } else {
395 $location = add_query_arg( array( 'message' => 0 ), $location );
396 }
397
398 wp_redirect( $location );
399 exit;
400
401 }
402
403 public function pre_render() {
404
405 global $ct_registered_tables, $ct_table;
406
407 $messages = array(
408 0 => ct_get_table_label( $ct_table->name, 'update_error' ),
409 1 => ct_get_table_label( $ct_table->name, 'update_success' ),
410 );
411
412 /**
413 * Filters the table updated messages (string keys allowed!).
414 *
415 * @since 1.0.0
416 *
417 * @param array $messages Post updated messages. For defaults @see $messages declarations above.
418 */
419 $messages = apply_filters( 'ct_table_updated_messages', $messages );
420
421 // Setup screen message
422 if ( isset($_GET['message']) ) {
423 $message_key = sanitize_key( $_GET['message'] );
424
425 if ( isset($messages[$message_key]) )
426 $this->message = sprintf( $messages[$message_key], $ct_table->labels->singular_name );
427
428 }
429
430 wp_enqueue_script( 'post' );
431
432 if ( wp_is_mobile() ) {
433 wp_enqueue_script( 'jquery-touch-punch' );
434 }
435
436 // Register submitdiv metabox
437 add_meta_box( 'submitdiv', __( 'Save Changes' ), array( $this, 'submit_meta_box' ), $ct_table->name, 'side', 'core' );
438
439 /**
440 * Fires after all built-in meta boxes have been added.
441 *
442 * @since 1.0.0
443 *
444 * @param string $post_type Post type.
445 * @param WP_Post $post Post object.
446 */
447 do_action( 'add_meta_boxes', $ct_table->name, $this->object );
448
449 /**
450 * Fires after all built-in meta boxes have been added, contextually for the given post type.
451 *
452 * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
453 *
454 * @since 1.0.0
455 *
456 * @param WP_Post $post Post object.
457 */
458 do_action( "add_meta_boxes_{$ct_table->name}", $this->object );
459
460 /**
461 * Fires after meta boxes have been added.
462 *
463 * Fires once for each of the default meta box contexts: normal, advanced, and side.
464 *
465 * @since 1.0.0
466 *
467 * @param string $post_type Type of the object.
468 * @param string $context string Meta box context.
469 * @param WP_Post $object The object.
470 */
471 do_action( 'do_meta_boxes', $ct_table->name, 'normal', $this->object );
472 /** This action is documented in wp-admin/edit-form-advanced.php */
473 do_action( 'do_meta_boxes', $ct_table->name, 'advanced', $this->object );
474 /** This action is documented in wp-admin/edit-form-advanced.php */
475 do_action( 'do_meta_boxes', $ct_table->name, 'side', $this->object );
476
477 // TODO: Need to add it manually through screen_settings() function
478 //add_screen_option( 'layout_columns', array( 'max' => $this->columns, 'default' => $this->columns ) );
479
480 }
481
482 public function render() {
483
484 global $ct_registered_tables, $ct_table;
485
486 $this->pre_render();
487
488 if( $this->editing ) {
489 $title = $ct_table->labels->edit_item;
490 $new_url = ( $ct_table->views->add ? $ct_table->views->add->get_link() : false );
491 } else {
492 $title = $ct_table->labels->add_new_item;
493 }
494
495 ?>
496
497 <div class="wrap">
498
499 <h1 class="wp-heading-inline"><?php echo $title ; ?></h1>
500
501 <?php if ( isset( $new_url ) && $new_url && current_user_can( $ct_table->cap->create_items ) ) :
502 echo ' <a href="' . esc_url( $new_url ) . '" class="page-title-action">' . $ct_table->labels->add_new_item . '</a>';
503 endif; ?>
504
505 <hr class="wp-header-end">
506
507 <?php if ( $this->message ) : ?>
508 <div id="message" class="updated notice notice-success is-dismissible"><p><?php echo $this->message; ?></p></div>
509 <?php endif; ?>
510
511 <form name="ct_edit_form" action="" method="post" id="ct_edit_form">
512
513 <input type="hidden" id="object_id" name="<?php echo esc_attr( $ct_table->db->primary_key ); ?>" value="<?php echo esc_attr( $this->object_id) ; ?>">
514 <?php wp_nonce_field( 'ct_edit_' . $this->object_id ); ?>
515
516 <?php
517 /**
518 * Fires at the beginning of the edit form.
519 *
520 * At this point, the required hidden fields and nonces have already been output.
521 *
522 * @since 1.0.0
523 *
524 * @param stdClass $object Object.
525 */
526 do_action( 'ct_edit_form_top', $this->object ); ?>
527
528 <div id="poststuff">
529
530 <div id="post-body" class="metabox-holder columns-<?php echo get_current_screen()->get_columns() === 1 || $this->columns === 1 ? '1' : '2'; ?>">
531
532 <div id="postbox-container-1" class="postbox-container">
533
534 <?php do_meta_boxes( $ct_table->name, 'side', $this->object ); ?>
535
536 </div>
537
538 <div id="postbox-container-2" class="postbox-container">
539
540 <?php do_meta_boxes( $ct_table->name, 'normal', $this->object ); ?>
541
542 <?php do_meta_boxes( $ct_table->name, 'advanced', $this->object ); ?>
543
544 </div>
545
546 </div><!-- /post-body -->
547
548 <br class="clear" />
549
550 </div><!-- /poststuff -->
551
552 <?php
553 /**
554 * Fires at the end of the edit form.
555 *
556 * @since 1.0.0
557 *
558 * @param stdClass $object Object.
559 */
560 do_action( 'ct_edit_form_bottom', $this->object ); ?>
561
562 </form>
563
564 </div>
565
566 <?php
567 }
568
569 }
570
571 endif;