, Ruben Garcia * * @since 1.0.0 */ // Exit if accessed directly defined( 'ABSPATH' ) || exit; if ( ! class_exists( 'CT_Edit_View' ) ) : class CT_Edit_View extends CT_View { protected $object_id = 0; protected $object = null; protected $editing = false; protected $message = false; protected $columns = 2; public function __construct( $name, $args ) { parent::__construct( $name, $args ); $this->columns = isset( $args['columns'] ) ? $args['columns'] : 2; } public function init() { global $ct_registered_tables, $ct_table; if( ! isset( $ct_registered_tables[$this->name] ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) ); } // Setup global $ct_table $ct_table = $ct_registered_tables[$this->name]; // If not CT object, die if ( ! $ct_table ) wp_die( esc_html__( 'Sorry, you are not allowed to perform this action.' ) ); // If not CT object allow ui, die if ( ! $ct_table->show_ui ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) ); } if( isset( $_POST['ct-save'] ) ) { // Saving $this->save(); } $primary_key = $ct_table->db->primary_key; if( isset( $_GET[$primary_key] ) ) { // Editing object $this->object_id = (int) $_GET[$primary_key]; $this->object = $ct_table->db->get( $this->object_id ); $this->editing = true; // If not id, return to list if ( empty( $this->object_id ) ) { wp_redirect( ct_get_list_link( $ct_table->name ) ); exit(); } // If not object, die if ( ! $this->object ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_deleted' ) ) ); } // If not current user can edit, die if ( ! current_user_can( $ct_table->cap->edit_item, $this->object_id ) ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_not_allowed' ) ) ); } } else { // See filter "ct_{$ct_table->name}_default_data" $this->object_id = ct_insert_object( array() ); // If not id, return to list if ( empty( $this->object_id ) ) { wp_redirect( ct_get_list_link( $ct_table->name ) ); exit(); } $this->object = ct_get_object( $this->object_id ); // If not object, die if ( ! $this->object ) wp_die( esc_html__( 'Unable to create the draft item.', 'ct' ) ); // If not current user can create, die if ( ! current_user_can( $ct_table->cap->create_items, $this->object_id ) && ! current_user_can( $ct_table->cap->edit_item, $this->object_id ) ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) ); } // Redirect to edit screen to prevent add a draft item multiples times wp_redirect( ct_get_edit_link( $ct_table->name, $this->object_id ) ); } } /** * Screen settings text displayed in the Screen Options tab. * * @param string $screen_settings Screen settings. * @param WP_Screen $screen WP_Screen object. */ public function screen_settings( $screen_settings, $screen ) { $this->render_meta_boxes_preferences(); $this->render_screen_layout(); } /** * Render the meta boxes preferences. * * @since 1.0.0 * * @global array $wp_meta_boxes */ public function render_meta_boxes_preferences() { global $wp_meta_boxes, $ct_table; // TODO: Forced to place it here until fix issue with load-{pagehook} /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'add_meta_boxes', $ct_table->name, $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( "add_meta_boxes_{$ct_table->name}", $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'do_meta_boxes', $ct_table->name, 'normal', $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'do_meta_boxes', $ct_table->name, 'advanced', $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'do_meta_boxes', $ct_table->name, 'side', $this->object ); if ( ! isset( $wp_meta_boxes[ $ct_table->name ] ) ) { return; } ?>

name ); ?>
columns <= 1 ) { return; } $screen_layout_columns = get_current_screen()->get_columns(); if( ! $screen_layout_columns ) { $screen_layout_columns = $this->columns; } $num = $this->columns; ?>
name}_edit_screen_submit_meta_box_top", $object, $ct_table, $this->editing, $this ); $submit_label = __( 'Add' ); if( $this->editing ) { $submit_label = __( 'Update' ); } /** * Filter to override the submit button label. * * @since 1.0.0 * * @param string $submit_label The submit label. * @param object $object Object. * @param CT_Table $ct_table CT Table object. * @param bool $editing True if edit screen, false if is adding a new one. * @param CT_Edit_View $view Edit view object. */ $submit_label = apply_filters( "ct_{$ct_table->name}_edit_screen_submit_label", $submit_label, $object, $ct_table, $this->editing, $this ); $primary_key = $ct_table->db->primary_key; $object_id = $object->$primary_key; ?>
name}_edit_screen_submit_meta_box_submit_post_top", $object, $ct_table, $this->editing, $this ); ?>
name}_edit_screen_submit_meta_box_minor_publishing_actions", $object, $ct_table, $this->editing, $this ); $minor_publishing_actions = ob_get_clean(); ?>
name}_edit_screen_submit_meta_box_misc_publishing_actions", $object, $ct_table, $this->editing, $this ); $misc_publishing_actions = ob_get_clean(); ?>
cap->delete_item, $object_id ) ) { printf( '%s', esc_attr( ct_get_delete_link( $ct_table->name, $object_id ) ), "return confirm('" . esc_attr( ct_get_table_label( $ct_table->name, 'delete_item_confirm' ) ) . "');", esc_attr( __( 'Delete permanently' ) ), esc_html__( 'Delete Permanently' ) ); } ?>
name}_edit_screen_submit_meta_box_submit_post_bottom", $object, $ct_table, $this->editing, $this ); ?>
name}_edit_screen_submit_meta_box_bottom", $object, $ct_table, $this->editing, $this ); } public function save() { global $ct_registered_tables, $ct_table; // If not CT object, die if ( ! $ct_table ) wp_die( esc_html__( 'Invalid object type.' ) ); // If not CT object allow ui, die if ( ! $ct_table->show_ui ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_items_not_allowed' ) ) ); } $primary_key = $ct_table->db->primary_key; if( ! isset( $_POST[$primary_key] ) ) { wp_die( esc_html__( 'Invalid object type.' ) ); } $object_id = $_POST[$primary_key]; // Nonce check if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_REQUEST['_wpnonce'] ) ), 'ct_edit_' . $object_id ) ) { wp_die( esc_html( ct_get_table_label( $ct_table->name, 'edit_item_not_allowed' ) ) ); } $object_data = map_deep( $_POST, 'wp_kses_post' ); unset( $object_data['ct-save'] ); $success = ct_update_object( $object_data ); $location = add_query_arg( array( $primary_key => $object_id ), $this->get_link() ); if( $success ) { $location = add_query_arg( array( 'message' => 1 ), $location ); } else { $location = add_query_arg( array( 'message' => 0 ), $location ); } wp_redirect( $location ); exit; } public function pre_render() { global $ct_registered_tables, $ct_table; $messages = array( 0 => ct_get_table_label( $ct_table->name, 'update_error' ), 1 => ct_get_table_label( $ct_table->name, 'update_success' ), ); /** * Filters the table updated messages (string keys allowed!). * * @since 1.0.0 * * @param array $messages Post updated messages. For defaults @see $messages declarations above. */ $messages = apply_filters( 'ct_table_updated_messages', $messages ); // Setup screen message if ( isset($_GET['message']) ) { $message_key = sanitize_key( $_GET['message'] ); if ( isset($messages[$message_key]) ) $this->message = sprintf( $messages[$message_key], $ct_table->labels->singular_name ); } wp_enqueue_script( 'post' ); if ( wp_is_mobile() ) { wp_enqueue_script( 'jquery-touch-punch' ); } // Register submitdiv metabox add_meta_box( 'submitdiv', __( 'Save Changes' ), array( $this, 'submit_meta_box' ), $ct_table->name, 'side', 'core' ); /** * Fires after all built-in meta boxes have been added. * * @since 1.0.0 * * @param string $post_type Post type. * @param WP_Post $post Post object. */ do_action( 'add_meta_boxes', $ct_table->name, $this->object ); /** * Fires after all built-in meta boxes have been added, contextually for the given post type. * * The dynamic portion of the hook, `$post_type`, refers to the post type of the post. * * @since 1.0.0 * * @param WP_Post $post Post object. */ do_action( "add_meta_boxes_{$ct_table->name}", $this->object ); /** * Fires after meta boxes have been added. * * Fires once for each of the default meta box contexts: normal, advanced, and side. * * @since 1.0.0 * * @param string $post_type Type of the object. * @param string $context string Meta box context. * @param WP_Post $object The object. */ do_action( 'do_meta_boxes', $ct_table->name, 'normal', $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'do_meta_boxes', $ct_table->name, 'advanced', $this->object ); /** This action is documented in wp-admin/edit-form-advanced.php */ do_action( 'do_meta_boxes', $ct_table->name, 'side', $this->object ); // TODO: Need to add it manually through screen_settings() function //add_screen_option( 'layout_columns', array( 'max' => $this->columns, 'default' => $this->columns ) ); } public function render() { global $ct_registered_tables, $ct_table; $this->pre_render(); if( $this->editing ) { $title = $ct_table->labels->edit_item; $new_url = ( $ct_table->views->add ? $ct_table->views->add->get_link() : false ); } else { $title = $ct_table->labels->add_new_item; } ?>

cap->create_items ) ) : echo ' ' . $ct_table->labels->add_new_item . ''; endif; ?>
message ) : ?>

message; ?>

object_id ); ?> object ); ?>
name, 'side', $this->object ); ?>
name, 'normal', $this->object ); ?> name, 'advanced', $this->object ); ?>

object ); ?>