builder
6 years ago
plugin-updates
8 years ago
settings
6 years ago
views
6 years ago
class-evf-admin-addons.php
6 years ago
class-evf-admin-assets.php
6 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-editor.php
6 years ago
class-evf-admin-entries-table-list.php
6 years ago
class-evf-admin-entries.php
6 years ago
class-evf-admin-forms-table-list.php
6 years ago
class-evf-admin-forms.php
6 years ago
class-evf-admin-import-export.php
6 years ago
class-evf-admin-menus.php
6 years ago
class-evf-admin-notices.php
6 years ago
class-evf-admin-settings.php
6 years ago
class-evf-admin-tools.php
6 years ago
class-evf-admin-welcome.php
6 years ago
class-evf-admin.php
6 years ago
evf-admin-functions.php
6 years ago
class-evf-admin-forms.php
268 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Admin Forms Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @since 1.2.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Admin_Forms class. |
| 13 | */ |
| 14 | class EVF_Admin_Forms { |
| 15 | |
| 16 | /** |
| 17 | * Initialize the forms admin actions. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_init', array( $this, 'actions' ) ); |
| 21 | add_action( 'deleted_post', array( $this, 'delete_entries' ) ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Check if is forms page. |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | private function is_forms_page() { |
| 30 | return isset( $_GET['page'] ) && 'evf-builder' === $_GET['page']; // phpcs:ignore WordPress.Security.NonceVerification |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Page output. |
| 35 | */ |
| 36 | public static function page_output() { |
| 37 | global $current_tab; |
| 38 | |
| 39 | if ( isset( $_GET['form_id'] ) && $current_tab ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 40 | $form = evf()->form->get( absint( $_GET['form_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 41 | $form_id = is_object( $form ) ? absint( $form->ID ) : absint( $_GET['form_id'] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 42 | $form_data = is_object( $form ) ? evf_decode( $form->post_content ) : false; |
| 43 | |
| 44 | include 'views/html-admin-page-builder.php'; |
| 45 | } elseif ( isset( $_GET['create-form'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 46 | $templates = array(); |
| 47 | $refresh_url = add_query_arg( |
| 48 | array( |
| 49 | 'page' => 'evf-builder&create-form=1', |
| 50 | 'action' => 'evf-template-refresh', |
| 51 | 'evf-template-nonce' => wp_create_nonce( 'refresh' ), |
| 52 | ), |
| 53 | admin_url( 'admin.php' ) |
| 54 | ); |
| 55 | $license_plan = evf_get_license_plan(); |
| 56 | $current_section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : '_all'; // phpcs:ignore WordPress.Security.NonceVerification |
| 57 | |
| 58 | if ( '_featured' !== $current_section ) { |
| 59 | $category = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : 'free'; // phpcs:ignore WordPress.Security.NonceVerification |
| 60 | $templates = self::get_template_data( $category ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Addon page view. |
| 65 | * |
| 66 | * @uses $templates |
| 67 | * @uses $refresh_url |
| 68 | * @uses $current_section |
| 69 | */ |
| 70 | include 'views/html-admin-page-builder-setup.php'; |
| 71 | } else { |
| 72 | self::table_list_output(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get sections for the addons screen. |
| 78 | * |
| 79 | * @return array of objects |
| 80 | */ |
| 81 | public static function get_sections() { |
| 82 | $template_sections = get_transient( 'evf_template_sections' ); |
| 83 | |
| 84 | if ( false === $template_sections ) { |
| 85 | $raw_sections = wp_safe_remote_get( 'https://raw.githubusercontent.com/wpeverest/extensions-json/master/everest-forms/templates/template-sections.json' ); |
| 86 | |
| 87 | if ( ! is_wp_error( $raw_sections ) ) { |
| 88 | $template_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); |
| 89 | |
| 90 | if ( $template_sections ) { |
| 91 | set_transient( 'evf_template_sections', $template_sections, WEEK_IN_SECONDS ); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return apply_filters( 'everest_forms_template_sections', $template_sections ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get section content for the template screen. |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public static function get_template_data() { |
| 105 | $template_data = get_transient( 'evf_template_section' ); |
| 106 | |
| 107 | if ( false === $template_data ) { |
| 108 | $raw_templates = wp_safe_remote_get( 'https://raw.githubusercontent.com/wpeverest/extensions-json/master/everest-forms/templates/all_templates.json' ); |
| 109 | |
| 110 | if ( ! is_wp_error( $raw_templates ) ) { |
| 111 | $template_data = json_decode( wp_remote_retrieve_body( $raw_templates ) ); |
| 112 | |
| 113 | // Removing directory so the templates can be reinitialized. |
| 114 | $folder_path = untrailingslashit( plugin_dir_path( EVF_PLUGIN_FILE ) . '/assets/images/templates' ); |
| 115 | |
| 116 | foreach ( $template_data->templates as $template_tuple ) { |
| 117 | // We retrieve the image, then use them instead of the remote server. |
| 118 | $image = wp_remote_get( $template_tuple->image ); |
| 119 | $type = wp_remote_retrieve_header( $image, 'content-type' ); |
| 120 | |
| 121 | // Remote file check failed, we'll fallback to remote image. |
| 122 | if ( ! $type ) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | $temp_name = explode( '/', $template_tuple->image ); |
| 127 | $relative_path = $folder_path . '/' . end( $temp_name ); |
| 128 | $exists = file_exists( $relative_path ); |
| 129 | |
| 130 | // If it exists, utilize this file instead of remote file. |
| 131 | if ( $exists ) { |
| 132 | $template_tuple->image = plugin_dir_url( EVF_PLUGIN_FILE ) . 'assets/images/templates/' . end( $temp_name ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if ( ! empty( $template_data->templates ) ) { |
| 137 | set_transient( 'evf_template_section', $template_data, WEEK_IN_SECONDS ); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if ( ! empty( $template_data->templates ) ) { |
| 143 | return apply_filters( 'everest_forms_template_section_data', $template_data->templates ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Table list output. |
| 149 | */ |
| 150 | public static function table_list_output() { |
| 151 | global $forms_table_list; |
| 152 | |
| 153 | $forms_table_list->process_bulk_action(); |
| 154 | $forms_table_list->prepare_items(); |
| 155 | ?> |
| 156 | <div class="wrap"> |
| 157 | <h1 class="wp-heading-inline"><?php esc_html_e( 'All Forms', 'everest-forms' ); ?></h1> |
| 158 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=evf-builder&create-form=1' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Add New', 'everest-forms' ); ?></a> |
| 159 | <hr class="wp-header-end"> |
| 160 | |
| 161 | <?php settings_errors(); ?> |
| 162 | |
| 163 | <form id="form-list" method="post"> |
| 164 | <input type="hidden" name="page" value="everest-forms"/> |
| 165 | <?php |
| 166 | $forms_table_list->views(); |
| 167 | $forms_table_list->search_box( __( 'Search Forms', 'everest-forms' ), 'everest-forms' ); |
| 168 | $forms_table_list->display(); |
| 169 | |
| 170 | wp_nonce_field( 'save', 'everest-forms_nonce' ); |
| 171 | ?> |
| 172 | </form> |
| 173 | </div> |
| 174 | <?php |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Forms admin actions. |
| 179 | */ |
| 180 | public function actions() { |
| 181 | if ( $this->is_forms_page() ) { |
| 182 | // Empty trash. |
| 183 | if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 184 | $this->empty_trash(); |
| 185 | } |
| 186 | |
| 187 | // Duplicate form. |
| 188 | if ( isset( $_REQUEST['action'] ) && 'duplicate_form' === $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 189 | $this->duplicate_form(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Empty Trash. |
| 196 | */ |
| 197 | private function empty_trash() { |
| 198 | check_admin_referer( 'bulk-forms' ); |
| 199 | |
| 200 | $count = 0; |
| 201 | $form_ids = get_posts( |
| 202 | array( |
| 203 | 'post_type' => 'everest_form', |
| 204 | 'ignore_sticky_posts' => true, |
| 205 | 'nopaging' => true, |
| 206 | 'post_status' => 'trash', |
| 207 | 'fields' => 'ids', |
| 208 | ) |
| 209 | ); |
| 210 | |
| 211 | foreach ( $form_ids as $form_id ) { |
| 212 | if ( wp_delete_post( $form_id, true ) ) { |
| 213 | $count ++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | add_settings_error( |
| 218 | 'empty_trash', |
| 219 | 'empty_trash', |
| 220 | /* translators: %d: number of forms */ |
| 221 | sprintf( _n( '%d form permanently deleted.', '%d forms permanently deleted.', $count, 'everest-forms' ), $count ), |
| 222 | 'updated' |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Duplicate form. |
| 228 | */ |
| 229 | private function duplicate_form() { |
| 230 | if ( empty( $_REQUEST['form_id'] ) ) { |
| 231 | wp_die( esc_html__( 'No form to duplicate has been supplied!', 'everest-forms' ) ); |
| 232 | } |
| 233 | |
| 234 | $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; |
| 235 | |
| 236 | check_admin_referer( 'everest-forms-duplicate-form_' . $form_id ); |
| 237 | |
| 238 | $duplicate_id = evf()->form->duplicate( $form_id ); |
| 239 | |
| 240 | // Redirect to the edit screen for the new form page. |
| 241 | wp_safe_redirect( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $duplicate_id ) ); |
| 242 | exit; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Remove entry and its associated meta. |
| 247 | * |
| 248 | * When form is deleted then it also deletes its entries meta. |
| 249 | * |
| 250 | * @param int $postid Post ID. |
| 251 | */ |
| 252 | public function delete_entries( $postid ) { |
| 253 | global $wpdb; |
| 254 | |
| 255 | $entries = evf_get_entries_ids( $postid ); |
| 256 | |
| 257 | // Delete entry. |
| 258 | if ( ! empty( $entries ) ) { |
| 259 | foreach ( $entries as $entry_id ) { |
| 260 | $wpdb->delete( $wpdb->prefix . 'evf_entries', array( 'entry_id' => $entry_id ), array( '%d' ) ); |
| 261 | $wpdb->delete( $wpdb->prefix . 'evf_entrymeta', array( 'entry_id' => $entry_id ), array( '%d' ) ); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | new EVF_Admin_Forms(); |
| 268 |