PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
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 7.9.8, at libraries/ct/includes/class-ct-edit-view.php

568 lines 22.0 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( 'Boxes' ); ?></legend>
153 <?php meta_box_prefs( $ct_table->name ); ?>
154 </fieldset>
155
156 <?php
157 }
158
159 /**
160 * Render the option for number of columns on the page
161 *
162 * @since 1.0.0
163 */
164 public function render_screen_layout() {
165
166 if ( $this->columns <= 1 ) {
167 return;
168 }
169
170 $screen_layout_columns = get_current_screen()->get_columns();
171
172 if( ! $screen_layout_columns ) {
173 $screen_layout_columns = $this->columns;
174 }
175
176 $num = $this->columns;
177
178 ?>
179 <fieldset class='columns-prefs'>
180 <legend class="screen-layout"><?php esc_html_e( 'Layout' ); ?></legend><?php
181 for ( $i = 1; $i <= $num; ++$i ):
182 ?>
183 <label class="columns-prefs-<?php echo esc_attr ( $i ); ?>">
184 <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
185 <?php checked( $screen_layout_columns, $i ); ?> />
186 <?php printf( _n( '%s column', '%s columns', $i ), number_format_i18n( $i ) ); ?>
187 </label>
188 <?php
189 endfor; ?>
190 </fieldset>
191 <?php
192 }
193
194 public function submit_meta_box( $object ) {
195
196 global $ct_table;
197
198 /**
199 * Fires at top of the submit meta box.
200 *
201 * @since 1.0.0
202 *
203 * @param object $object Object.
204 * @param CT_Table $ct_table CT Table object.
205 * @param bool $editing True if edit screen, false if is adding a new one.
206 * @param CT_Edit_View $view Edit view object.
207 */
208 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_top", $object, $ct_table, $this->editing, $this );
209
210 $submit_label = __( 'Add' );
211
212 if( $this->editing ) {
213 $submit_label = __( 'Update' );
214 }
215
216 /**
217 * Filter to override the submit button label.
218 *
219 * @since 1.0.0
220 *
221 * @param string $submit_label The submit label.
222 * @param object $object Object.
223 * @param CT_Table $ct_table CT Table object.
224 * @param bool $editing True if edit screen, false if is adding a new one.
225 * @param CT_Edit_View $view Edit view object.
226 */
227 $submit_label = apply_filters( "ct_{$ct_table->name}_edit_screen_submit_label", $submit_label, $object, $ct_table, $this->editing, $this );
228
229 $primary_key = $ct_table->db->primary_key;
230 $object_id = $object->$primary_key;
231
232 ?>
233
234 <div class="submitbox" id="submitpost">
235
236 <?php
237 /**
238 * Fires at top of submit meta box submit post element.
239 *
240 * @since 1.0.0
241 *
242 * @param object $object Object.
243 * @param CT_Table $ct_table CT Table object.
244 * @param bool $editing True if edit screen, false if is adding a new one.
245 * @param CT_Edit_View $view Edit view object.
246 */
247 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_submit_post_top", $object, $ct_table, $this->editing, $this ); ?>
248
249 <div id="minor-publishing">
250
251 <?php ob_start();
252 /**
253 * Fires inside minor publishing actions from submit meta box.
254 *
255 * @since 1.0.0
256 *
257 * @param object $object Object.
258 * @param CT_Table $ct_table CT Table object.
259 * @param bool $editing True if edit screen, false if is adding a new one.
260 * @param CT_Edit_View $view Edit view object.
261 */
262 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_minor_publishing_actions", $object, $ct_table, $this->editing, $this );
263 $minor_publishing_actions = ob_get_clean(); ?>
264
265 <?php // Since minor-publishing-actions has a margin, check if minor publishing actions has any content to render it or not
266 if( ! empty( $minor_publishing_actions ) ) : ?>
267 <div id="minor-publishing-actions"><?php echo $minor_publishing_actions; ?></div>
268 <?php endif; ?>
269
270 <?php ob_start();
271 /**
272 * Fires inside misc publishing actions from submit meta box.
273 *
274 * @since 1.0.0
275 *
276 * @param object $object Object.
277 * @param CT_Table $ct_table CT Table object.
278 * @param bool $editing True if edit screen, false if is adding a new one.
279 * @param CT_Edit_View $view Edit view object.
280 */
281 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_misc_publishing_actions", $object, $ct_table, $this->editing, $this );
282 $misc_publishing_actions = ob_get_clean(); ?>
283
284 <?php // Since misc-publishing-actions has a margin, check if misc publishing actions has any content to render it or not
285 if( ! empty( $misc_publishing_actions ) ) : ?>
286 <div id="misc-publishing-actions"><?php echo $misc_publishing_actions; ?></div>
287 <?php endif; ?>
288
289 <div class="clear"></div>
290
291 </div>
292
293 <div id="major-publishing-actions">
294
295 <?php
296 if ( current_user_can( $ct_table->cap->delete_item, $object_id ) ) {
297
298 printf(
299 '<a href="%s" class="submitdelete deletion" onclick="%s" aria-label="%s">%s</a>',
300 ct_get_delete_link( $ct_table->name, $object_id ),
301 "return confirm('" .
302 esc_attr( ct_get_table_label( $ct_table->name, 'delete_item_confirm' ) ) .
303 "');",
304 esc_attr( __( 'Delete permanently' ) ),
305 __( 'Delete Permanently' )
306 );
307
308 } ?>
309
310 <div id="publishing-action">
311 <span class="spinner"></span>
312 <?php submit_button( $submit_label, 'primary large', 'ct-save', false ); ?>
313 <input type="button" name="" id="ct-save-disabled" class="button button-primary" value="<?php echo esc_attr( $submit_label ); ?>" disabled="disabled" style="display: none;">
314
315 </div>
316
317 <div class="clear"></div>
318
319 </div>
320
321 <?php
322 /**
323 * Fires at bottom of submit meta box submit post element.
324 *
325 * @since 1.0.0
326 *
327 * @param object $object Object.
328 * @param CT_Table $ct_table CT Table object.
329 * @param bool $editing True if edit screen, false if is adding a new one.
330 * @param CT_Edit_View $view Edit view object.
331 */
332 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_submit_post_bottom", $object, $ct_table, $this->editing, $this ); ?>
333
334 </div>
335
336 <?php
337
338 /**
339 * Fires at bottom the submit meta box.
340 *
341 * @since 1.0.0
342 *
343 * @param object $object Object.
344 * @param CT_Table $ct_table CT Table object.
345 * @param bool $editing True if edit screen, false if is adding a new one.
346 * @param CT_Edit_View $view Edit view object.
347 */
348 do_action( "ct_{$ct_table->name}_edit_screen_submit_meta_box_bottom", $object, $ct_table, $this->editing, $this );
349 }
350
351 public function save() {
352
353 global $ct_registered_tables, $ct_table;
354
355 // If not CT object, die
356 if ( ! $ct_table )
357 wp_die( esc_html__( 'Invalid item type.' ) );
358
359 // If not CT object allow ui, die
360 if ( ! $ct_table->show_ui ) {
361 wp_die( esc_html__( 'Sorry, you are not allowed to edit items of this type.' ) );
362 }
363
364 $primary_key = $ct_table->db->primary_key;
365
366 if( ! isset( $_POST[$primary_key] ) ) {
367 wp_die( esc_html__( 'Invalid item type.' ) );
368 }
369
370 $object_id = $_POST[$primary_key];
371
372 // Nonce check
373 if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
374 wp_die( esc_html__( 'Sorry, you are not allowed to edit this item.' ) );
375 }
376
377 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'ct_edit_' . $object_id ) ) {
378 wp_die( esc_html__( 'Sorry, you are not allowed to edit this item.' ) );
379 }
380
381 $object_data = map_deep( $_POST, 'wp_kses_post' );
382
383 unset( $object_data['ct-save'] );
384
385 $success = ct_update_object( $object_data );
386
387 $location = add_query_arg( array( $primary_key => $object_id ), $this->get_link() );
388
389 if( $success ) {
390 $location = add_query_arg( array( 'message' => 1 ), $location );
391 } else {
392 $location = add_query_arg( array( 'message' => 0 ), $location );
393 }
394
395 wp_redirect( $location );
396 exit;
397
398 }
399
400 public function pre_render() {
401
402 global $ct_registered_tables, $ct_table;
403
404 $messages = array(
405 0 => __( '%s could not be updated.' ),
406 1 => __( '%s updated successfully.' ),
407 );
408
409 /**
410 * Filters the table updated messages (string keys allowed!).
411 *
412 * @since 1.0.0
413 *
414 * @param array $messages Post updated messages. For defaults @see $messages declarations above.
415 */
416 $messages = apply_filters( 'ct_table_updated_messages', $messages );
417
418 // Setup screen message
419 if ( isset($_GET['message']) ) {
420 $message_key = sanitize_key( $_GET['message'] );
421
422 if ( isset($messages[$message_key]) )
423 $this->message = sprintf( $messages[$message_key], $ct_table->labels->singular_name );
424
425 }
426
427 wp_enqueue_script( 'post' );
428
429 if ( wp_is_mobile() ) {
430 wp_enqueue_script( 'jquery-touch-punch' );
431 }
432
433 // Register submitdiv metabox
434 add_meta_box( 'submitdiv', __( 'Save Changes' ), array( $this, 'submit_meta_box' ), $ct_table->name, 'side', 'core' );
435
436 /**
437 * Fires after all built-in meta boxes have been added.
438 *
439 * @since 1.0.0
440 *
441 * @param string $post_type Post type.
442 * @param WP_Post $post Post object.
443 */
444 do_action( 'add_meta_boxes', $ct_table->name, $this->object );
445
446 /**
447 * Fires after all built-in meta boxes have been added, contextually for the given post type.
448 *
449 * The dynamic portion of the hook, `$post_type`, refers to the post type of the post.
450 *
451 * @since 1.0.0
452 *
453 * @param WP_Post $post Post object.
454 */
455 do_action( "add_meta_boxes_{$ct_table->name}", $this->object );
456
457 /**
458 * Fires after meta boxes have been added.
459 *
460 * Fires once for each of the default meta box contexts: normal, advanced, and side.
461 *
462 * @since 1.0.0
463 *
464 * @param string $post_type Type of the object.
465 * @param string $context string Meta box context.
466 * @param WP_Post $object The object.
467 */
468 do_action( 'do_meta_boxes', $ct_table->name, 'normal', $this->object );
469 /** This action is documented in wp-admin/edit-form-advanced.php */
470 do_action( 'do_meta_boxes', $ct_table->name, 'advanced', $this->object );
471 /** This action is documented in wp-admin/edit-form-advanced.php */
472 do_action( 'do_meta_boxes', $ct_table->name, 'side', $this->object );
473
474 // TODO: Need to add it manually through screen_settings() function
475 //add_screen_option( 'layout_columns', array( 'max' => $this->columns, 'default' => $this->columns ) );
476
477 }
478
479 public function render() {
480
481 global $ct_registered_tables, $ct_table;
482
483 $this->pre_render();
484
485 if( $this->editing ) {
486 $title = $ct_table->labels->edit_item;
487 $new_url = ( $ct_table->views->add ? $ct_table->views->add->get_link() : false );
488 } else {
489 $title = $ct_table->labels->add_new_item;
490 }
491
492 ?>
493
494 <div class="wrap">
495
496 <h1 class="wp-heading-inline"><?php echo $title ; ?></h1>
497
498 <?php if ( isset( $new_url ) && $new_url && current_user_can( $ct_table->cap->create_items ) ) :
499 echo ' <a href="' . esc_url( $new_url ) . '" class="page-title-action">' . $ct_table->labels->add_new_item . '</a>';
500 endif; ?>
501
502 <hr class="wp-header-end">
503
504 <?php if ( $this->message ) : ?>
505 <div id="message" class="updated notice notice-success is-dismissible"><p><?php echo $this->message; ?></p></div>
506 <?php endif; ?>
507
508 <form name="ct_edit_form" action="" method="post" id="ct_edit_form">
509
510 <input type="hidden" id="object_id" name="<?php echo esc_attr( $ct_table->db->primary_key ); ?>" value="<?php echo esc_attr( $this->object_id) ; ?>">
511 <?php wp_nonce_field( 'ct_edit_' . $this->object_id ); ?>
512
513 <?php
514 /**
515 * Fires at the beginning of the edit form.
516 *
517 * At this point, the required hidden fields and nonces have already been output.
518 *
519 * @since 1.0.0
520 *
521 * @param stdClass $object Object.
522 */
523 do_action( 'ct_edit_form_top', $this->object ); ?>
524
525 <div id="poststuff">
526
527 <div id="post-body" class="metabox-holder columns-<?php echo get_current_screen()->get_columns() === 1 || $this->columns === 1 ? '1' : '2'; ?>">
528
529 <div id="postbox-container-1" class="postbox-container">
530
531 <?php do_meta_boxes( $ct_table->name, 'side', $this->object ); ?>
532
533 </div>
534
535 <div id="postbox-container-2" class="postbox-container">
536
537 <?php do_meta_boxes( $ct_table->name, 'normal', $this->object ); ?>
538
539 <?php do_meta_boxes( $ct_table->name, 'advanced', $this->object ); ?>
540
541 </div>
542
543 </div><!-- /post-body -->
544
545 <br class="clear" />
546
547 </div><!-- /poststuff -->
548
549 <?php
550 /**
551 * Fires at the end of the edit form.
552 *
553 * @since 1.0.0
554 *
555 * @param stdClass $object Object.
556 */
557 do_action( 'ct_edit_form_bottom', $this->object ); ?>
558
559 </form>
560
561 </div>
562
563 <?php
564 }
565
566 }
567
568 endif;