__( 'Forms', 'email-subscribers' ), // singular name of the listed records 'plural' => __( 'Forms', 'email-subscribers' ), // plural name of the listed records 'ajax' => false, // does this table support ajax?, 'screen' => 'es_forms', ) ); $this->db = new ES_DB_Forms(); $this->init(); } public function init() { add_action( 'ig_es_additional_form_options', array( $this, 'show_additional_form_setting' ) ); } /** * Add Screen Option * * @since 4.2.1 */ public static function screen_options() { $action = ig_es_get_request_data( 'action' ); if ( empty( $action ) ) { $option = 'per_page'; $args = array( 'label' => __( 'Number of forms per page', 'email-subscribers' ), 'default' => 20, 'option' => self::$option_per_page, ); add_screen_option( $option, $args ); } } /** * Render Forms list view * * @since 4.0 */ public function render() { $action = ig_es_get_request_data( 'action' ); ?>
es_new_form_callback(); } elseif ( 'edit' === $action ) { $form = ig_es_get_request_data( 'form' ); echo wp_kses_post( $this->edit_form( absint( $form ) ) ); } else { ?>


prepare_items(); ?>
display(); ?>

$status, 'message' => $message, ); return $response; } public function es_new_form_callback() { $submitted = ig_es_get_request_data( 'submitted' ); if ( 'submitted' === $submitted ) { $nonce = ig_es_get_request_data( '_wpnonce' ); $form_data = ig_es_get_request_data( 'form_data', array(), false ); $lists = ig_es_get_request_data( 'lists' ); $form_data['lists'] = $lists; $editor_type = ! empty( $form_data['settings']['editor_type'] ) ? $form_data['settings']['editor_type'] : ''; $validate_data = array( 'nonce' => $nonce, 'name' => ! empty( $form_data['name'] ) ? sanitize_text_field( $form_data['name'] ) : '', 'lists' => ! empty( $form_data['lists'] ) ? $form_data['lists'] : array(), 'editor_type' => $editor_type, ); $response = $this->validate_data( $validate_data ); if ( 'error' === $response['status'] ) { $message = $response['message']; ES_Common::show_message( $message, 'error' ); $this->prepare_list_form( null, $form_data ); return; } $this->save_form( null, $form_data ); $form_url = admin_url( 'admin.php?page=es_forms&action=form_created' ); wp_safe_redirect( $form_url ); exit(); } $this->prepare_list_form(); } public function edit_form( $id ) { global $wpdb; if ( $id ) { $form_data = array(); $data = ES()->forms_db->get_by_conditions( $wpdb->prepare( ' id = %d', $id ) ); if ( count( $data ) > 0 ) { $submitted = ig_es_get_request_data( 'submitted' ); if ( 'submitted' === $submitted ) { $nonce = ig_es_get_request_data( '_wpnonce' ); $form_data = ig_es_get_request_data( 'form_data', array(), false ); $lists = ig_es_get_request_data( 'lists' ); $form_data['captcha'] = ! empty( $form_data['captcha'] ) ? $form_data['captcha'] : 'no'; $form_data['lists'] = $lists; $editor_type = ! empty( $form_data['settings']['editor_type'] ) ? $form_data['settings']['editor_type'] : ''; $validate_data = array( 'nonce' => $nonce, 'name' => $form_data['name'], 'lists' => $form_data['lists'], 'editor_type' => $editor_type, ); $response = $this->validate_data( $validate_data ); if ( 'error' === $response['status'] ) { $message = $response['message']; ES_Common::show_message( $message, 'error' ); $this->prepare_list_form( $id, $form_data ); return; } $this->save_form( $id, $form_data ); $form_url = admin_url( 'admin.php?page=es_forms&action=form_updated' ); wp_safe_redirect( $form_url ); exit(); } else { $data = $data[0]; $id = $data['id']; $form_data = self::get_form_data_from_body( $data ); } } else { $message = __( 'Sorry, form not found', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); } $this->prepare_list_form( $id, $form_data ); } } public function prepare_list_form( $id = 0, $data = array() ) { $is_new = empty( $id ) ? 1 : 0; $editor_type = ''; if ( $is_new ) { $editor_type = IG_ES_DRAG_AND_DROP_EDITOR; $data['settings']['editor_type'] = $editor_type; } else { $editor_type = ! empty( $data['settings']['editor_type'] ) ? $data['settings']['editor_type'] : ''; } $is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type; if ( $is_dnd_editor ) { do_action( 'ig_es_render_dnd_form', $id, $data ); } else { do_action( 'ig_es_render_classic_form', $id, $data ); } } /** * Show additional form setting for es form * * @param $form_data * * @since 5.6.1 */ public function show_additional_form_setting( $form_data ) { $editor_type = ! empty( $form_data['settings']['editor_type'] ) ? $form_data['settings']['editor_type'] : ''; $is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type; if ( $is_dnd_editor ) { $this->show_dnd_show_in_popup_settings( $form_data ); $this->show_success_message( $form_data ); } else { $this->show_classic_show_in_popup_settings( $form_data ); } } /** * Show success message * * @param $form_data * * @since 5.6.1 * * @modify 5.6.3 */ public function show_success_message( $form_data ) { $action_after_submit = ! empty( $form_data['settings']['action_after_submit'] ) ? $form_data['settings']['action_after_submit'] : 'show_success_message'; $success_message = ! empty( $form_data['settings']['success_message'] ) ? $form_data['settings']['success_message'] : ''; ?>
/>

update( IG_FORMS_TABLE, $form_data, array( 'id' => $id ) ); } else { $return = $wpdb->insert( IG_FORMS_TABLE, $form_data ); } return $return; } public static function prepare_form_data( $data ) { $form_data = array(); $name = ! empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : ''; $editor_type = ! empty( $data['settings']['editor_type'] ) ? sanitize_text_field( $data['settings']['editor_type'] ) : ''; $is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type; $es_form_popup = ! empty( $data['show_in_popup'] ) ? 'yes' : 'no'; $es_popup_headline = ! empty( $data['popup_headline'] ) ? sanitize_text_field( $data['popup_headline'] ) : ''; if ( ! $is_dnd_editor ) { $desc = ! empty( $data['desc'] ) ? wp_kses_post( trim( wp_unslash( $data['desc'] ) ) ) : ''; $email_label = ! empty( $data['email_label'] ) ? sanitize_text_field( $data['email_label'] ) : ''; $email_place_holder = ! empty( $data['email_place_holder'] ) ? sanitize_text_field( $data['email_place_holder'] ) : ''; $name_label = ! empty( $data['name_label'] ) ? sanitize_text_field( $data['name_label'] ) : ''; $name_place_holder = ! empty( $data['name_place_holder'] ) ? sanitize_text_field( $data['name_place_holder'] ) : ''; $button_label = ! empty( $data['button_label'] ) ? sanitize_text_field( $data['button_label'] ) : ''; $name_visible = ( ! empty( $data['name_visible'] ) && 'yes' === $data['name_visible'] ) ? true : false; $name_required = ( ! empty( $data['name_required'] ) && 'yes' === $data['name_required'] ) ? true : false; $list_label = ! empty( $data['list_label'] ) ? sanitize_text_field( $data['list_label'] ) : ''; $list_visible = ( ! empty( $data['list_visible'] ) && 'yes' === $data['list_visible'] ) ? true : false; $list_required = true; $list_ids = ! empty( $data['lists'] ) ? $data['lists'] : array(); $gdpr_consent = ! empty( $data['gdpr_consent'] ) ? sanitize_text_field( $data['gdpr_consent'] ) : 'no'; $gdpr_consent_text = ! empty( $data['gdpr_consent_text'] ) ? wp_kses_post( $data['gdpr_consent_text'] ) : ''; $captcha = ! empty( $data['captcha'] ) ? ES_Common::get_captcha_setting( null, $data ) : 'no'; $body = array( array( 'type' => 'text', 'name' => 'Name', 'id' => 'name', 'params' => array( 'label' => $name_label, 'place_holder' => $name_place_holder, 'show' => $name_visible, 'required' => $name_required, ), 'position' => 1, ), array( 'type' => 'text', 'name' => 'Email', 'id' => 'email', 'params' => array( 'label' => $email_label, 'place_holder' => $email_place_holder, 'show' => true, 'required' => true, ), 'position' => 2, ), array( 'type' => 'checkbox', 'name' => 'Lists', 'id' => 'lists', 'params' => array( 'label' => $list_label, 'show' => $list_visible, 'required' => $list_required, 'values' => $list_ids, ), 'position' => 3, ), ); $form_body = apply_filters( 'es_add_custom_fields_data_in_form_body', $body, $data ); $submit_button_position = count( $form_body ) + 1; $submit_data = array( array( 'type' => 'submit', 'name' => 'submit', 'id' => 'submit', 'params' => array( 'label' => $button_label, 'show' => true, 'required' => true, ), 'position' => $submit_button_position, ), ); $body = array_merge( $form_body, $submit_data ); $settings = array( 'lists' => $list_ids, 'desc' => $desc, 'form_version' => ES()->forms_db->version, 'captcha' => $captcha, 'gdpr' => array( 'consent' => $gdpr_consent, 'consent_text' => $gdpr_consent_text, ), 'es_form_popup' => array( 'show_in_popup' => $es_form_popup, 'popup_headline' => $es_popup_headline, ), ); $settings = apply_filters( 'ig_es_form_settings', $settings, $data ); $form_data['body'] = maybe_serialize( $body ); } else { $form_data['body'] = ES_Form_Admin::process_form_body($data['body']); $settings = $data['settings']; } $af_id = ! empty( $data['af_id'] ) ? $data['af_id'] : 0; $form_data['name'] = $name; $form_data['settings'] = maybe_serialize( $settings ); $form_data['styles'] = null; $form_data['created_at'] = ig_get_current_date_time(); $form_data['updated_at'] = null; $form_data['deleted_at'] = null; $form_data['af_id'] = $af_id; return $form_data; } public static function get_form_data_from_body( $data ) { $name = ! empty( $data['name'] ) ? $data['name'] : ''; $id = ! empty( $data['id'] ) ? $data['id'] : ''; $af_id = ! empty( $data['af_id'] ) ? $data['af_id'] : ''; $body_data = maybe_unserialize( $data['body'] ); $settings_data = maybe_unserialize( $data['settings'] ); $desc = ! empty( $settings_data['desc'] ) ? $settings_data['desc'] : ''; $form_version = ! empty( $settings_data['form_version'] ) ? $settings_data['form_version'] : '0.1'; $editor_type = ! empty( $settings_data['editor_type'] ) ? $settings_data['editor_type'] : ''; $is_dnd_editor = IG_ES_DRAG_AND_DROP_EDITOR === $editor_type; if ( ! $is_dnd_editor ) { $gdpr_consent = 'no'; $gdpr_consent_text = ''; $es_form_popup = ! empty( $settings_data['es_form_popup']['show_in_popup'] ) ? $settings_data['es_form_popup']['show_in_popup'] : 'no'; $es_popup_headline = ! empty( $settings_data['es_form_popup']['popup_headline'] ) ? $settings_data['es_form_popup']['popup_headline'] : ''; $captcha = ES_Common::get_captcha_setting( $id, $settings_data ); if ( ! empty( $settings_data['gdpr'] ) ) { $gdpr_consent = ! empty( $settings_data['gdpr']['consent'] ) ? $settings_data['gdpr']['consent'] : 'no'; $gdpr_consent_text = ! empty( $settings_data['gdpr']['consent_text'] ) ? $settings_data['gdpr']['consent_text'] : ''; } $form_data = array( 'form_id' => $id, 'name' => $name, 'af_id' => $af_id, 'desc' => $desc, 'form_version' => $form_version, 'gdpr_consent' => $gdpr_consent, 'gdpr_consent_text' => $gdpr_consent_text, 'captcha' => $captcha, 'show_in_popup' => $es_form_popup, 'popup_headline' => $es_popup_headline, 'editor_type' => $editor_type, ); foreach ( $body_data as $d ) { if ( 'name' === $d['id'] ) { $form_data['name_visible'] = ( true === $d['params']['show'] ) ? 'yes' : ''; $form_data['name_required'] = ( true === $d['params']['required'] ) ? 'yes' : ''; $form_data['name_label'] = ! empty( $d['params']['label'] ) ? $d['params']['label'] : ''; $form_data['name_place_holder'] = ! empty( $d['params']['place_holder'] ) ? $d['params']['place_holder'] : ''; } elseif ( 'lists' === $d['id'] ) { $form_data['list_label'] = ! empty( $d['params']['label'] ) ? $d['params']['label'] : ''; $form_data['list_visible'] = ( true === $d['params']['show'] ) ? 'yes' : ''; $form_data['list_required'] = ( true === $d['params']['required'] ) ? 'yes' : ''; $form_data['lists'] = ! empty( $d['params']['values'] ) ? $d['params']['values'] : array(); } elseif ( 'email' === $d['id'] ) { $form_data['email_label'] = ! empty( $d['params']['label'] ) ? $d['params']['label'] : ''; $form_data['email_place_holder'] = ! empty( $d['params']['place_holder'] ) ? $d['params']['place_holder'] : ''; } elseif ( 'submit' === $d['id'] ) { $form_data['button_label'] = ! empty( $d['params']['label'] ) ? $d['params']['label'] : ''; } elseif ( $d['is_custom_field'] ) { $form_data['custom_fields'][] = $d; } } $form_data = apply_filters('ig_es_form_fields_data', $form_data, $settings_data, $body_data); } else { $form_data = array( 'form_id' => $id, 'body' => $body_data, 'name' => $name, 'af_id' => $af_id, 'form_version' => $form_version, 'settings' => $settings_data, ); } return $form_data; } /** * Retrieve lists data from the database * * @param int $per_page * @param int $page_number * * @return mixed */ public function get_lists( $per_page = 5, $page_number = 1, $do_count_only = false ) { global $wpdb, $wpbd; $order_by = sanitize_sql_orderby( ig_es_get_request_data( 'orderby' ) ); $order = ig_es_get_request_data( 'order' ); $search = ig_es_get_request_data( 's' ); $forms_table = IG_FORMS_TABLE; if ( $do_count_only ) { $sql = "SELECT count(*) as total FROM {$forms_table}"; } else { $sql = "SELECT * FROM {$forms_table}"; } $args = array(); $query = array(); $add_where_clause = false; if ( ! empty( $search ) ) { $query[] = ' name LIKE %s '; $args[] = '%' . $wpdb->esc_like( $search ) . '%'; $add_where_clause = true; } if ( $add_where_clause ) { $sql .= ' WHERE '; if ( count( $query ) > 0 ) { $sql .= implode( ' AND ', $query ); if ( count( $args ) > 0 ) { $sql = $wpbd->prepare( $sql, $args ); } } } if ( ! $do_count_only ) { $order = ! empty( $order ) ? strtolower( $order ) : 'desc'; $expected_order_values = array( 'asc', 'desc' ); if ( ! in_array( $order, $expected_order_values ) ) { $order = 'desc'; } $default_order_by = esc_sql( 'created_at' ); $expected_order_by_values = array( 'name', 'created_at' ); if ( ! in_array( $order_by, $expected_order_by_values ) ) { $order_by_clause = " ORDER BY {$default_order_by} DESC"; } else { $order_by = esc_sql( $order_by ); $order_by_clause = " ORDER BY {$order_by} {$order}, {$default_order_by} DESC"; } $sql .= $order_by_clause; $sql .= " LIMIT $per_page"; $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; $result = $wpbd->get_results( $sql, 'ARRAY_A' ); } else { $result = $wpbd->get_var( $sql ); } return $result; } /** * Render a column when no column specific method exist. * * @param array $item * @param string $column_name * * @return mixed */ public function column_default( $item, $column_name ) { switch ( $column_name ) { // case 'status': // return $this->status_label_map( $item[ $column_name ] ); case 'created_at': return ig_es_format_date_time( $item[ $column_name ] ); break; case 'shortcode': $shortcode = '[email-subscribers-form id="' . $item['id'] . '"]'; return '' . $shortcode . ''; break; case 'total_active_subscribers': $total_active_subscribers = ES()->contacts_db->get_total_contacts_by_form_id( $item['id'] ); return number_format( $total_active_subscribers ); default: return ''; } } /** * Render the bulk edit checkbox * * @param array $item * * @return string */ public function column_cb( $item ) { return sprintf( '', $item['id'] ); } /** * Method for name column * * @param array $item an array of DB data * * @return string */ public function column_name( $item ) { $list_nonce = wp_create_nonce( 'es_form' ); $title = '' . stripslashes( $item['name'] ) . ''; $page = ig_es_get_request_data( 'page' ); $actions = array( 'edit' => '' . esc_html__( 'Edit', 'email-subscribers' ) . '', 'delete' => '' . esc_html__( 'Delete', 'email-subscribers' ) . '', ); $actions = apply_filters('ig_es_form_table_row_actions', $actions, $item); return $title . $this->row_actions( $actions ); } /** * Associative array of columns * * @return array */ public function get_columns() { $columns = array( 'cb' => '', 'name' => __( 'Name', 'email-subscribers' ), 'shortcode' => __( 'Shortcode', 'email-subscribers' ), 'total_active_subscribers' => __( 'Subscribers', 'email-subscribers' ), 'created_at' => __( 'Created', 'email-subscribers' ), ); return $columns; } /** * Columns to make sortable. * * @return array */ public function get_sortable_columns() { $sortable_columns = array( 'name' => array( 'name', true ), 'created_at' => array( 'created_at', true ), ); return $sortable_columns; } /** * Returns an associative array containing the bulk action * * @return array */ public function get_bulk_actions() { return array( 'bulk_delete' => __( 'Delete', 'email-subscribers' ), ); } public function process_bulk_action() { if ( 'delete' === $this->current_action() ) { // In our file that handles the request, verify the nonce. $nonce = ig_es_get_request_data( '_wpnonce' ); if ( ! wp_verify_nonce( $nonce, 'es_form' ) ) { $message = __( 'You do not have permission to delete this form.', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); } else { $form = ig_es_get_request_data( 'form' ); $this->db->delete_forms( array( $form ) ); $message = __( 'Form deleted successfully!', 'email-subscribers' ); ES_Common::show_message( $message, 'success' ); } } $action = ig_es_get_request_data( 'action' ); $action2 = ig_es_get_request_data( 'action2' ); // If the delete bulk action is triggered if ( ( 'bulk_delete' === $action ) || ( 'bulk_delete' === $action2 ) ) { $forms = ig_es_get_request_data( 'forms' ); if ( ! empty( $forms ) > 0 ) { $this->db->delete_forms( $forms ); $message = __( 'Form(s) deleted successfully!', 'email-subscribers' ); ES_Common::show_message( $message, 'success' ); } else { $message = __( 'Please select form(s) to delete.', 'email-subscribers' ); ES_Common::show_message( $message, 'error' ); return; } } } public function status_label_map( $status ) { $statuses = array( 'enable' => __( 'Enable', 'email-subscribers' ), 'disable' => __( 'Disable', 'email-subscribers' ), ); if ( ! in_array( $status, array_keys( $statuses ) ) ) { return ''; } return $statuses[ $status ]; } public function search_box( $text, $input_id ) { ?>