mode = $args['mode']; } else { wp_die( esc_attr__( 'ERROR: Wrong arguments [missing mode]', 'wp-data-access' ) ); } if ( isset( $args['child_request'] ) ) { $this->child_request = $args['child_request']; } else { wp_die( esc_attr__( 'ERROR: Wrong arguments [missing child_request]', 'wp-data-access' ) ); } $action = null; if ( isset( $_REQUEST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- already verified // Possible values: "new", "edit" and "view". $action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- already verified $this->action_posted = $action; } elseif ( isset( $args['action'] ) ) { $action = $args['action']; $this->action_posted = $action; } $this->relations = $relationship; $args['back_to_list_text'] = __( 'Parent List', 'wp-data-access' ); parent::__construct( $schema_name, $table_name, $wpda_list_columns, $args ); if ( 'new' !== $action || $this->child_request ) { if ( isset( $this->relations['parent']['key'] ) && is_array( $this->relations['parent']['key'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- already verified foreach ( $this->relations['parent']['key'] as $key ) { if ( isset( $_REQUEST[ 'WPDA_PARENT_KEY*' . $key ] ) ) { array_push( $this->parent_key, $key ); // phpcs:ignore -- 8.1 proof $this->parent_key_value[ $key ] = sanitize_text_field( wp_unslash( $_REQUEST[ 'WPDA_PARENT_KEY*' . $key ] ) ); // input var okay. } elseif ( isset( $_REQUEST[ $key ] ) ) { array_push( $this->parent_key, $key ); // phpcs:ignore -- 8.1 proof $this->parent_key_value[ $key ] = sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) ); // input var okay. } if ( is_array( $this->relations['children'] ) ) { $this->children = $this->relations['children']; } } // phpcs:enable WordPress.Security.NonceVerification.Recommended } } $this->set_child_action_member(); if ( $this->child_request ) { if ( 'list' === $this->action || '-1' === $this->action ) { $this->action = 'edit'; } else { $this->action = 'view'; } } if ( 'view' === $this->mode ) { $this->action = 'view'; } foreach ( $this->children as $child ) { if ( null === $this->current_tab ) { $this->current_tab = $child['table_name']; } $this->tabs[ $child['table_name'] ] = $child['tab_label']; if ( isset( $child['relation_nm'] ) ) { $this->relations[ $child['table_name'] ]['relation_nm'] = $child['relation_nm']; } elseif ( isset( $child['relation_1n'] ) ) { $this->relations[ $child['table_name'] ]['relation_1n'] = $child['relation_1n']; } } if ( isset( $_REQUEST['child_tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified in parent class if ( isset( $this->tabs[ $_REQUEST['child_tab'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified in parent class $this->current_tab = sanitize_text_field( wp_unslash( $_REQUEST['child_tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- verified in parent class } } if ( isset( $args['edit_form_class'] ) ) { $this->edit_form_class = $args['edit_form_class']; } if ( isset( $args['list_form_class'] ) ) { $this->list_table_class = $args['list_form_class']; } } /** * Overwrite method to add show less/more functionality * * @param bool $set_back_form_values */ protected function prepare_items( $set_back_form_values = false ) { parent::prepare_items( $set_back_form_values ); foreach ( $this->wpda_list_columns->get_table_columns() as $columns ) { if ( isset( $columns['less'] ) && ! $columns['less'] ) { foreach ( $this->form_items as $item ) { if ( $item->get_item_name() === $columns['column_name'] ) { $item->set_item_class( 'row-show-less-more' ); } } } } } /** * Determine and set action to be performed on child */ protected function set_child_action_member() { if ( $this->child_request ) { if ( '-1' === $this->action ) { $this->child_action = $this->action2; } else { $this->child_action = $this->action; } } else { $this->child_action = 'list'; } } /** * Overwrite method show * * @param bool $allow_save * @param string $add_param * * @see WPDP_Simple_Form::show() */ public function show( $allow_save = true, $add_param = '' ) { if ( $this->child_request ) { parent::show( false, $add_param ); } else { parent::show( $allow_save, $add_param ); } if ( 'new' === $this->action_posted && 'edit' === parent::get_form_action() ) { // Record successfully inserted: reload page to show tabs ?> add_tabs(); foreach ( $this->children as $child ) { if ( $child['table_name'] === $this->current_tab ) { if ( 'edit' === $this->child_action || 'new' === $this->child_action || 'view' === $this->child_action ) { $this->show_child_form( $child['table_name'], $child ); } else { $this->show_child_list_table( $child['table_name'], $child ); } } } } /** * Show child form * * @param string $child_table_name Database table name * @param array $child Child info */ protected function show_child_form( $child_table_name, $child ) { if ( 'edit' === $this->mode && ( 'new' === $this->child_action || 'edit' === $this->child_action ) && ( ! ( isset( $_POST['postaction'] ) && 'childlist' === $_POST['postaction'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in parent class ) { $this->button_add_new( $child, $child_table_name ); echo '
'; } $wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $child_table_name, 'tableform', $this->setname ); $wpda_child_form = new $this->edit_form_class( $this->schema_name, $child_table_name, $wpda_list_columns, array( 'show_title' => false, 'show_back_button' => true, 'mode' => $this->mode, 'parent' => array( 'parent_key' => $this->parent_key, 'parent_key_value' => $this->parent_key_value, ), 'child' => $child, 'back_to_list_text' => __( 'Child List', 'wp-data-access' ), ) ); $wpda_child_form->prepare_form(); if ( isset( $_POST['postaction'] ) && 'childlist' === $_POST['postaction'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified in parent class $this->show_child_list_table( $child_table_name, $child ); } else { $wpda_child_form->show(); } } /** * Show child list table * * @param string $child_table_name Database table name * @param array $child Child info */ protected function show_child_list_table( $child_table_name, $child ) { $is_list_table_selection = isset( $_REQUEST['list_table_selection'] ); // phpcs:ignore -- verified in parent class if ( 'edit' === $this->mode ) { $this->button_add_new( $child, $child_table_name ); } if ( $is_list_table_selection || 'add' === $this->child_action || 'bulk-add' === $this->child_action ) { $list_table_class = 'WPDataProjects\\Parent_Child\\WPDP_Child_List_Table_Selection'; } else { $list_table_class = $this->list_table_class; } $wpda_list_columns = WPDP_List_Columns_Cache::get_list_columns( $this->schema_name, $child_table_name, 'listtable', $this->setname ); $wpda_list_table = new $list_table_class( array( 'wpdaschema_name' => $this->schema_name, 'table_name' => $child_table_name, 'wpda_list_columns' => $wpda_list_columns, 'mode' => $this->mode, 'title' => '', 'subtitle' => '', 'allow_import' => 'off', 'parent' => array( 'parent_key' => $this->parent_key, 'parent_key_value' => $this->parent_key_value, ), 'child' => $child, ) ); $wpda_list_table->show(); } /** * Add a tab for every child */ protected function add_tabs() { if ( count( $this->children ) > 0 ) { // phpcs:ignore -- 8.1 proof ?> relations['parent']['key'] ) && is_array( $this->relations['parent']['key'] ) ) { foreach ( $this->relations['parent']['key'] as $key ) { if ( isset( $_REQUEST[ 'WPDA_PARENT_KEY*' . $key ] ) ) { // phpcs:ignore -- verified in parent class $this->form_items_new_values[ $key ] = sanitize_text_field( wp_unslash( $_REQUEST[ 'WPDA_PARENT_KEY*' . $key ] ) ); // phpcs:ignore -- verified in parent class } } } } /** * Add button new to page top * * @param array $child Child info * @param string $child_table_name Database table name */ protected function button_add_new( $child, $child_table_name ) { // Check if table has primary key. If not, disable adding a new record. $check_pk = WPDA_List_Columns_Cache::get_list_columns( $this->schema_name, $child_table_name ); $title = 'Add new row'; if ( is_admin() ) { $url = "?page={$this->page}"; } else { $url = ''; } if ( ! empty( $check_pk->get_table_primary_key() ) ) { ?>
add_parent_keys( 'WPDA_PARENT_KEY*' ); ?> page_number_item; // phpcs:ignore WordPress.Security.EscapeOutput ?>
add_parent_keys( 'WPDA_PARENT_KEY*' ); ?> page_number_item; // phpcs:ignore WordPress.Security.EscapeOutput ?>
parent_key as $parent_key ) { ?> action_posted || $this->child_request ) { ?>