builder
2 years ago
plugin-updates
8 years ago
settings
2 years ago
views
2 years ago
class-evf-admin-addons.php
4 years ago
class-evf-admin-assets.php
2 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-deactivation-feedback.php
3 years ago
class-evf-admin-editor.php
4 years ago
class-evf-admin-entries-table-list.php
3 years ago
class-evf-admin-entries.php
4 years ago
class-evf-admin-form-templates.php
3 years ago
class-evf-admin-forms-table-list.php
3 years ago
class-evf-admin-forms.php
3 years ago
class-evf-admin-import-export.php
4 years ago
class-evf-admin-menus.php
2 years ago
class-evf-admin-notices.php
3 years ago
class-evf-admin-settings.php
2 years ago
class-evf-admin-tools.php
4 years ago
class-evf-admin-welcome.php
2 years ago
class-evf-admin.php
2 years ago
evf-admin-functions.php
3 years ago
class-evf-admin-forms.php
190 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 | add_filter( 'wp_untrash_post_status', array( $this, 'untrash_form_status' ), 10, 2 ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Check if is forms page. |
| 27 | * |
| 28 | * @return bool |
| 29 | */ |
| 30 | private function is_forms_page() { |
| 31 | return isset( $_GET['page'] ) && 'evf-builder' === $_GET['page']; // phpcs:ignore WordPress.Security.NonceVerification |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Page output. |
| 36 | */ |
| 37 | public static function page_output() { |
| 38 | global $current_tab; |
| 39 | |
| 40 | if ( isset( $_GET['form_id'] ) && $current_tab ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 41 | $form = evf()->form->get( absint( $_GET['form_id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 42 | $form_id = is_object( $form ) ? absint( $form->ID ) : absint( $_GET['form_id'] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 43 | $form_data = is_object( $form ) ? evf_decode( $form->post_content ) : false; |
| 44 | |
| 45 | include 'views/html-admin-page-builder.php'; |
| 46 | } elseif ( isset( $_GET['create-form'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 47 | |
| 48 | EVF_Admin_Form_Templates::load_template_view(); |
| 49 | } else { |
| 50 | self::table_list_output(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Table list output. |
| 56 | */ |
| 57 | public static function table_list_output() { |
| 58 | global $forms_table_list; |
| 59 | |
| 60 | $forms_table_list->process_bulk_action(); |
| 61 | $forms_table_list->prepare_items(); |
| 62 | ?> |
| 63 | <div class="wrap"> |
| 64 | <h1 class="wp-heading-inline"><?php esc_html_e( 'All Forms', 'everest-forms' ); ?></h1> |
| 65 | <?php if ( current_user_can( 'everest_forms_create_forms' ) ) : ?> |
| 66 | <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> |
| 67 | <?php endif; ?> |
| 68 | <hr class="wp-header-end"> |
| 69 | |
| 70 | <?php settings_errors(); ?> |
| 71 | |
| 72 | <form id="form-list" method="post"> |
| 73 | <input type="hidden" name="page" value="everest-forms"/> |
| 74 | <?php |
| 75 | $forms_table_list->views(); |
| 76 | $forms_table_list->search_box( __( 'Search Forms', 'everest-forms' ), 'everest-forms' ); |
| 77 | $forms_table_list->display(); |
| 78 | |
| 79 | wp_nonce_field( 'save', 'everest-forms_nonce' ); |
| 80 | ?> |
| 81 | </form> |
| 82 | </div> |
| 83 | <?php |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Forms admin actions. |
| 88 | */ |
| 89 | public function actions() { |
| 90 | if ( $this->is_forms_page() ) { |
| 91 | // Empty trash. |
| 92 | if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 93 | $this->empty_trash(); |
| 94 | } |
| 95 | |
| 96 | // Duplicate form. |
| 97 | if ( isset( $_REQUEST['action'] ) && 'duplicate_form' === $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 98 | $this->duplicate_form(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Empty Trash. |
| 105 | */ |
| 106 | private function empty_trash() { |
| 107 | check_admin_referer( 'bulk-forms' ); |
| 108 | |
| 109 | $count = 0; |
| 110 | $form_ids = get_posts( |
| 111 | array( |
| 112 | 'post_type' => 'everest_form', |
| 113 | 'ignore_sticky_posts' => true, |
| 114 | 'nopaging' => true, |
| 115 | 'post_status' => 'trash', |
| 116 | 'fields' => 'ids', |
| 117 | ) |
| 118 | ); |
| 119 | |
| 120 | foreach ( $form_ids as $form_id ) { |
| 121 | if ( wp_delete_post( $form_id, true ) ) { |
| 122 | $count ++; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | add_settings_error( |
| 127 | 'empty_trash', |
| 128 | 'empty_trash', |
| 129 | /* translators: %d: number of forms */ |
| 130 | sprintf( _n( '%d form permanently deleted.', '%d forms permanently deleted.', $count, 'everest-forms' ), $count ), |
| 131 | 'updated' |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Duplicate form. |
| 137 | */ |
| 138 | private function duplicate_form() { |
| 139 | if ( empty( $_REQUEST['form_id'] ) ) { |
| 140 | wp_die( esc_html__( 'No form to duplicate has been supplied!', 'everest-forms' ) ); |
| 141 | } |
| 142 | |
| 143 | $form_id = isset( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; |
| 144 | |
| 145 | check_admin_referer( 'everest-forms-duplicate-form_' . $form_id ); |
| 146 | |
| 147 | $duplicate_id = evf()->form->duplicate( $form_id ); |
| 148 | |
| 149 | // Redirect to the edit screen for the new form page. |
| 150 | wp_safe_redirect( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $duplicate_id ) ); |
| 151 | exit; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Remove entry and its associated meta. |
| 156 | * |
| 157 | * When form is deleted then it also deletes its entries meta. |
| 158 | * |
| 159 | * @param int $postid Post ID. |
| 160 | */ |
| 161 | public function delete_entries( $postid ) { |
| 162 | global $wpdb; |
| 163 | |
| 164 | $entries = evf_get_entries_ids( $postid ); |
| 165 | |
| 166 | // Delete entry. |
| 167 | if ( ! empty( $entries ) ) { |
| 168 | foreach ( $entries as $entry_id ) { |
| 169 | $wpdb->delete( $wpdb->prefix . 'evf_entries', array( 'entry_id' => $entry_id ), array( '%d' ) ); |
| 170 | $wpdb->delete( $wpdb->prefix . 'evf_entrymeta', array( 'entry_id' => $entry_id ), array( '%d' ) ); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Untrash form status. |
| 177 | * |
| 178 | * @since 1.7.5 |
| 179 | * |
| 180 | * @param string $new_status The new status of the post being restored. |
| 181 | * @param int $post_id The ID of the post being restored. |
| 182 | * @return string |
| 183 | */ |
| 184 | public function untrash_form_status( $new_status, $post_id ) { |
| 185 | return current_user_can( 'everest_forms_edit_forms', $post_id ) ? 'publish' : $new_status; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | new EVF_Admin_Forms(); |
| 190 |