plugin_file = $plugin_file; } public function hook() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'init', array( $this, 'register_settings' ) ); add_action( 'admin_init', array( $this, 'run_migrations' ) ); add_action( 'admin_init', array( $this, 'listen' ) ); add_action( 'admin_print_styles', array( $this, 'assets' ) ); add_action( 'admin_head', array( $this, 'add_screen_options' ) ); add_action( 'hf_admin_action_create_form', array( $this, 'process_create_form' ) ); add_action( 'hf_admin_action_save_form', array( $this, 'process_save_form' ) ); add_action( 'hf_admin_action_bulk_delete_submissions', array( $this, 'process_bulk_delete_submissions' ) ); add_action( 'hf_admin_output_form_tab_fields', array( $this, 'tab_fields' ) ); add_action( 'hf_admin_output_form_tab_messages', array( $this, 'tab_messages' ) ); add_action( 'hf_admin_output_form_tab_settings', array( $this, 'tab_settings' ) ); add_action( 'hf_admin_output_form_tab_actions', array( $this, 'tab_actions' ) ); add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_list' ) ); add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_detail' ) ); add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_assets' ) ); } public function enqueue_gutenberg_assets() { wp_enqueue_script( 'html-forms-block', plugins_url( 'assets/js/gutenberg-block.js', $this->plugin_file ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' ) ); $forms = hf_get_forms(); $data = array(); foreach ( $forms as $form ) { $data[] = array( 'title' => $form->title, 'slug' => $form->slug, 'id' => $form->ID, ); } wp_localize_script( 'html-forms-block', 'html_forms', $data ); } public function register_settings() { // register settings register_setting( 'hf_settings', 'hf_settings', array( $this, 'sanitize_settings' ) ); } public function run_migrations() { $version_from = get_option( 'hf_version', '0.0' ); $version_to = HTML_FORMS_VERSION; if ( version_compare( $version_from, $version_to, '>=' ) ) { return; } $migrations = new Migrations( $version_from, $version_to, dirname( $this->plugin_file ) . '/migrations' ); $migrations->run(); update_option( 'hf_version', HTML_FORMS_VERSION ); } /** * @param array $dirty * @return array */ public function sanitize_settings( $dirty ) { return $dirty; } public function listen() { if ( isset( $_GET['_hf_admin_action'] ) ) { $action = (string) $_GET['_hf_admin_action']; } elseif ( isset( $_POST['_hf_admin_action'] ) ) { $action = (string) $_POST['_hf_admin_action']; } else { return; } // verify nonce if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], '_hf_admin_action' ) ) { wp_nonce_ays( $action ); exit; } // do nothing if logged in user is not of role administrator if ( ! current_user_can( 'edit_forms' ) ) { return; } /** * Allows you to hook into requests containing `_hf_admin_action` => action name. * * The dynamic portion of the hook name, `$action`, refers to the action name. * * By the time this hook is fired, the user is already authorized. After processing all the registered hooks, * the request is redirected back to the referring URL. * * @since 3.0 */ do_action( 'hf_admin_action_' . $action ); // redirect back to where we came from $redirect_url = ! empty( $_REQUEST['_redirect_to'] ) ? $_REQUEST['_redirect_to'] : remove_query_arg( '_hf_admin_action' ); wp_safe_redirect( $redirect_url ); exit; } public function assets() { if ( empty( $_GET['page'] ) || strpos( $_GET['page'], 'html-forms' ) !== 0 ) { return; } wp_enqueue_style( 'html-forms-admin', plugins_url( 'assets/css/admin.css', $this->plugin_file ), array(), HTML_FORMS_VERSION ); wp_enqueue_script( 'html-forms-admin', plugins_url( 'assets/js/admin.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true ); wp_localize_script( 'html-forms-admin', 'hf_options', array( 'page' => $_GET['page'], 'view' => empty( $_GET['view'] ) ? '' : $_GET['view'], 'form_id' => empty( $_GET['form_id'] ) ? 0 : (int) $_GET['form_id'], ) ); } public function menu() { $capability = 'edit_forms'; $svg_icon = ''; add_menu_page( 'HTML Forms', 'HTML Forms', $capability, 'html-forms', array( $this, 'page_overview' ), 'data:image/svg+xml;base64,' . base64_encode( $svg_icon ), '99.88491' ); add_submenu_page( 'html-forms', __( 'Forms', 'html-forms' ), __( 'All Forms', 'html-forms' ), $capability, 'html-forms', array( $this, 'page_overview' ) ); add_submenu_page( 'html-forms', __( 'Add new form', 'html-forms' ), __( 'Add New', 'html-forms' ), $capability, 'html-forms-add-form', array( $this, 'page_new_form' ) ); add_submenu_page( 'html-forms', __( 'Settings', 'html-forms' ), __( 'Settings', 'html-forms' ), $capability, 'html-forms-settings', array( $this, 'page_settings' ) ); if ( ! defined( 'HF_PREMIUM_VERSION' ) ) { add_submenu_page( 'html-forms', 'Premium', 'Premium', $capability, 'html-forms-premium', array( $this, 'page_premium' ) ); } } public function add_screen_options() { // only run on the submissions overview page (not detail) if ( empty( $_GET['page'] ) || $_GET['page'] !== 'html-forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit' || empty( $_GET['form_id'] ) || ! empty( $_GET['submission_id'] ) ) { return; } // don't run if form does not have submissions enabled $form = hf_get_form( $_GET['form_id'] ); if ( ! $form->settings['save_submissions'] ) { return; } // tell screen options to show columns option $submissions = hf_get_form_submissions( $_GET['form_id'] ); $columns = $this->get_submission_columns( $submissions ); add_filter( 'manage_toplevel_page_html-forms_columns', function( $unused ) use ( $columns ) { return $columns; } ); add_screen_option( 'layout_columns' ); } public function page_overview() { if ( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) { $this->page_edit_form(); return; } $settings = hf_get_settings(); require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; $table = new Table( $settings ); require dirname( $this->plugin_file ) . '/views/page-overview.php'; } public function page_new_form() { require dirname( $this->plugin_file ) . '/views/page-add-form.php'; } public function page_settings() { $settings = hf_get_settings(); require dirname( $this->plugin_file ) . '/views/page-global-settings.php'; } public function page_premium() { require dirname( $this->plugin_file ) . '/views/page-premium.php'; } public function page_edit_form() { $active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields'; $form_id = (int) $_GET['form_id']; $form = hf_get_form( $form_id ); $settings = hf_get_settings(); require dirname( $this->plugin_file ) . '/views/page-edit-form.php'; } public function tab_fields( Form $form ) { $form_preview_url = add_query_arg( array( 'hf_preview_form' => $form->ID, ), site_url( '/', 'admin' ) ); require dirname( $this->plugin_file ) . '/views/tab-fields.php'; } public function tab_messages( Form $form ) { require dirname( $this->plugin_file ) . '/views/tab-messages.php'; } public function tab_settings( Form $form ) { require dirname( $this->plugin_file ) . '/views/tab-settings.php'; } public function tab_actions( Form $form ) { require dirname( $this->plugin_file ) . '/views/tab-actions.php'; } public function get_submission_columns( array $submissions ) { $columns = array(); foreach ( $submissions as $s ) { if ( ! is_array( $s->data ) ) { continue; } foreach ( $s->data as $field => $value ) { if ( ! isset( $columns[ $field ] ) ) { $columns[ $field ] = esc_html( ucfirst( strtolower( str_replace( '_', ' ', $field ) ) ) ); } } } return $columns; } public function tab_submissions_list( Form $form ) { if ( ! empty( $_GET['submission_id'] ) ) { return; } $submissions = hf_get_form_submissions( $form->ID ); $columns = $this->get_submission_columns( $submissions ); $hidden_columns = get_hidden_columns( get_current_screen() ); require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php'; } public function tab_submissions_detail( Form $form ) { if ( empty( $_GET['submission_id'] ) ) { return; } $submission = hf_get_form_submission( (int) $_GET['submission_id'] ); require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php'; } public function process_create_form() { // Fix for MultiSite stripping KSES for roles other than administrator remove_all_filters( 'content_save_pre' ); $data = $_POST['form']; $form_title = sanitize_text_field( $data['title'] ); $form_id = wp_insert_post( array( 'post_type' => 'html-form', 'post_status' => 'publish', 'post_title' => $form_title, 'post_content' => $this->get_default_form_content(), ) ); wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id ) ); exit; } public function process_save_form() { $form_id = (int) $_POST['form_id']; $form = hf_get_form( $form_id ); $data = $_POST['form']; // Fix for MultiSite stripping KSES for roles other than administrator remove_all_filters( 'content_save_pre' ); // strip