builder
3 years ago
plugin-updates
8 years ago
settings
3 years ago
views
3 years ago
class-evf-admin-addons.php
4 years ago
class-evf-admin-assets.php
3 years ago
class-evf-admin-builder.php
8 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
5 years ago
class-evf-admin-notices.php
3 years ago
class-evf-admin-settings.php
3 years ago
class-evf-admin-tools.php
4 years ago
class-evf-admin-welcome.php
5 years ago
class-evf-admin.php
3 years ago
evf-admin-functions.php
3 years ago
class-evf-admin-menus.php
344 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Setup menus in WP admin. |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @version 1.2.0 |
| 7 | * @since 1.0.0 |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | if ( class_exists( 'EVF_Admin_Menus', false ) ) { |
| 13 | return new EVF_Admin_Menus(); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * EVF_Admin_Menus Class. |
| 18 | */ |
| 19 | class EVF_Admin_Menus { |
| 20 | |
| 21 | /** |
| 22 | * Hook in tabs. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | // Add menus. |
| 26 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 ); |
| 27 | add_action( 'admin_menu', array( $this, 'builder_menu' ), 20 ); |
| 28 | add_action( 'admin_menu', array( $this, 'entries_menu' ), 30 ); |
| 29 | add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 ); |
| 30 | add_action( 'admin_menu', array( $this, 'tools_menu' ), 60 ); |
| 31 | |
| 32 | if ( apply_filters( 'everest_forms_show_addons_page', true ) ) { |
| 33 | add_action( 'admin_menu', array( $this, 'addons_menu' ), 70 ); |
| 34 | } |
| 35 | |
| 36 | add_action( 'admin_head', array( $this, 'menu_highlight' ) ); |
| 37 | add_action( 'admin_head', array( $this, 'custom_menu_count' ) ); |
| 38 | add_action( 'admin_head', array( $this, 'hide_submenu_items' ) ); |
| 39 | add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) ); |
| 40 | add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 11, 3 ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns a base64 URL for the SVG for use in the menu. |
| 45 | * |
| 46 | * @param string $fill SVG Fill color code. Default: '#82878c'. |
| 47 | * @param bool $base64 Whether or not to return base64-encoded SVG. |
| 48 | * @return string |
| 49 | */ |
| 50 | public static function get_icon_svg( $fill = '#82878c', $base64 = true ) { |
| 51 | $svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path fill="' . $fill . '" d="M18.1 4h-3.8l1.2 2h3.9zM20.6 8h-3.9l1.2 2h3.9zM20.6 18H5.8L12 7.9l2.5 4.1H12l-1.2 2h7.3L12 4.1 2.2 20h19.6z"/></g></svg>'; |
| 52 | |
| 53 | if ( $base64 ) { |
| 54 | return 'data:image/svg+xml;base64,' . base64_encode( $svg ); |
| 55 | } |
| 56 | |
| 57 | return $svg; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add menu items. |
| 62 | */ |
| 63 | public function admin_menu() { |
| 64 | add_menu_page( esc_html__( 'Everest Forms', 'everest-forms' ), esc_html__( 'Everest Forms', 'everest-forms' ), 'manage_everest_forms', 'everest-forms', null, self::get_icon_svg(), '55.5' ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add menu items. |
| 69 | */ |
| 70 | public function builder_menu() { |
| 71 | $builder_page = add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms Builder', 'everest-forms' ), esc_html__( 'All Forms', 'everest-forms' ), current_user_can( 'everest_forms_create_forms' ) ? 'everest_forms_create_forms' : 'everest_forms_view_forms', 'evf-builder', array( $this, 'builder_page' ) ); |
| 72 | |
| 73 | add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms Setup', 'everest-forms' ), esc_html__( 'Add New', 'everest-forms' ), current_user_can( 'everest_forms_create_forms' ) ? 'everest_forms_create_forms' : 'everest_forms_edit_forms', 'evf-builder&create-form=1', array( $this, 'builder_page' ) ); |
| 74 | |
| 75 | add_action( 'load-' . $builder_page, array( $this, 'builder_page_init' ) ); |
| 76 | |
| 77 | /* |
| 78 | * Page redirects based on user's capability as 'All Forms' and 'Add New' both have same handle. |
| 79 | * |
| 80 | * - If only `everest_forms_create_forms` roles - dont show view all forms list table. |
| 81 | * - If only `everest_forms_view_forms` roles - dont show create new template selection. |
| 82 | */ |
| 83 | if ( ! current_user_can( 'manage_everest_forms' ) ) { |
| 84 | if ( ! current_user_can( 'everest_forms_create_forms' ) ) { |
| 85 | if ( isset( $_GET['page'], $_GET['create-form'] ) && 'evf-builder' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 86 | wp_safe_redirect( admin_url( 'admin.php?page=evf-builder' ) ); |
| 87 | exit; |
| 88 | } |
| 89 | } elseif ( ! current_user_can( 'everest_forms_view_forms' ) ) { |
| 90 | if ( ! isset( $_GET['create-form'] ) && ( ! empty( $_GET['page'] ) && 'evf-builder' === $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 91 | wp_safe_redirect( admin_url( 'admin.php?page=evf-builder&create-form=1' ) ); |
| 92 | exit; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Loads builder page. |
| 100 | */ |
| 101 | public function builder_page_init() { |
| 102 | global $current_tab, $forms_table_list; |
| 103 | |
| 104 | evf()->form_fields(); |
| 105 | |
| 106 | // Include builder pages. |
| 107 | EVF_Admin_Builder::get_builder_pages(); |
| 108 | |
| 109 | // Get current tab/section. |
| 110 | $current_tab = empty( $_GET['tab'] ) ? 'fields' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 111 | |
| 112 | if ( ! isset( $_GET['tab'], $_GET['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 113 | $forms_table_list = new EVF_Admin_Forms_Table_List(); |
| 114 | |
| 115 | // Add screen option. |
| 116 | add_screen_option( |
| 117 | 'per_page', |
| 118 | array( |
| 119 | 'default' => 20, |
| 120 | 'option' => 'evf_forms_per_page', |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | do_action( 'everest_forms_builder_page_init' ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add menu item. |
| 130 | */ |
| 131 | public function entries_menu() { |
| 132 | $entries_page = add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms Entries', 'everest-forms' ), esc_html__( 'Entries', 'everest-forms' ), current_user_can( 'everest_forms_view_entries' ) ? 'everest_forms_view_entries' : 'everest_forms_view_others_entries', 'evf-entries', array( $this, 'entries_page' ) ); |
| 133 | add_action( 'load-' . $entries_page, array( $this, 'entries_page_init' ) ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Loads entries into memory. |
| 138 | */ |
| 139 | public function entries_page_init() { |
| 140 | global $entries_table_list; |
| 141 | |
| 142 | if ( ! isset( $_GET['view-entry'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 143 | $entries_table_list = new EVF_Admin_Entries_Table_List(); |
| 144 | |
| 145 | // Add screen option. |
| 146 | add_screen_option( |
| 147 | 'per_page', |
| 148 | array( |
| 149 | 'default' => 20, |
| 150 | 'option' => 'evf_entries_per_page', |
| 151 | ) |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | do_action( 'everest_forms_entries_page_init' ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Add menu item. |
| 160 | */ |
| 161 | public function settings_menu() { |
| 162 | $settings_page = add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms settings', 'everest-forms' ), esc_html__( 'Settings', 'everest-forms' ), 'manage_everest_forms', 'evf-settings', array( $this, 'settings_page' ) ); |
| 163 | |
| 164 | add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Loads settings page. |
| 169 | */ |
| 170 | public function settings_page_init() { |
| 171 | global $current_tab, $current_section; |
| 172 | |
| 173 | // Include settings pages. |
| 174 | EVF_Admin_Settings::get_settings_pages(); |
| 175 | |
| 176 | // Get current tab/section. |
| 177 | $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 178 | $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 179 | |
| 180 | // Save settings if data has been posted. |
| 181 | if ( apply_filters( '' !== $current_section ? "everest_forms_save_settings_{$current_tab}_{$current_section}" : "everest_forms_save_settings_{$current_tab}", ! empty( $_POST ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 182 | EVF_Admin_Settings::save(); |
| 183 | } |
| 184 | |
| 185 | // Add any posted messages. |
| 186 | if ( ! empty( $_GET['evf_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 187 | EVF_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['evf_error'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 188 | } |
| 189 | |
| 190 | if ( ! empty( $_GET['evf_message'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 191 | EVF_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['evf_message'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 192 | } |
| 193 | |
| 194 | do_action( 'everest_forms_settings_page_init' ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Add menu item. |
| 199 | */ |
| 200 | public function tools_menu() { |
| 201 | add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms tools', 'everest-forms' ), esc_html__( 'Tools', 'everest-forms' ), 'manage_everest_forms', 'evf-tools', array( $this, 'tools_page' ) ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Addons menu item. |
| 206 | */ |
| 207 | public function addons_menu() { |
| 208 | add_submenu_page( 'everest-forms', esc_html__( 'Everest Forms Add-ons', 'everest-forms' ), esc_html__( 'Add-ons', 'everest-forms' ), 'manage_everest_forms', 'evf-addons', array( $this, 'addons_page' ) ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Highlights the correct top level admin menu item. |
| 213 | */ |
| 214 | public function menu_highlight() { |
| 215 | global $parent_file, $submenu_file; |
| 216 | |
| 217 | $screen = get_current_screen(); |
| 218 | $screen_id = $screen ? $screen->id : ''; |
| 219 | |
| 220 | // Check to make sure we're on a EverestForms builder setup page. |
| 221 | if ( isset( $_GET['create-form'] ) && in_array( $screen_id, array( 'everest-forms_page_evf-builder' ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 222 | $parent_file = 'everest-forms'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 223 | $submenu_file = 'evf-builder&create-form=1'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adds the custom count to the menu. |
| 229 | */ |
| 230 | public function custom_menu_count() { |
| 231 | global $submenu; |
| 232 | |
| 233 | // Add count if user has access. |
| 234 | if ( isset( $submenu['everest-forms'] ) ) { |
| 235 | if ( apply_filters( 'everest_forms_include_count_in_menu', true ) && current_user_can( 'manage_everest_forms' ) ) { |
| 236 | do_action( 'everest_forms_custom_menu_count' ); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Hide submenu menu item if a user can't access. |
| 243 | * |
| 244 | * @since 1.7.5 |
| 245 | */ |
| 246 | public function hide_submenu_items() { |
| 247 | global $submenu; |
| 248 | |
| 249 | if ( ! isset( $submenu['everest-forms'] ) ) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Remove 'Everest Forms' sub menu item. |
| 254 | foreach ( $submenu['everest-forms'] as $key => $item ) { |
| 255 | if ( isset( $item[2] ) && 'everest-forms' === $item[2] ) { |
| 256 | unset( $submenu['everest-forms'][ $key ] ); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Remove 'All Forms' sub menu item if a user can't read forms. |
| 262 | if ( ! current_user_can( 'everest_forms_view_forms' ) ) { |
| 263 | foreach ( $submenu['everest-forms'] as $key => $item ) { |
| 264 | if ( isset( $item[2] ) && 'evf-builder' === $item[2] ) { |
| 265 | unset( $submenu['everest-forms'][ $key ] ); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Remove 'Add New' sub menu item if a user can't create forms. |
| 272 | if ( ! current_user_can( 'everest_forms_create_forms' ) ) { |
| 273 | foreach ( $submenu['everest-forms'] as $key => $item ) { |
| 274 | if ( isset( $item[2] ) && 'evf-builder&create-form=1' === $item[2] ) { |
| 275 | unset( $submenu['everest-forms'][ $key ] ); |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Custom menu order. |
| 284 | * |
| 285 | * @param bool $enabled Whether custom menu ordering is already enabled. |
| 286 | * @return bool |
| 287 | */ |
| 288 | public function custom_menu_order( $enabled ) { |
| 289 | return $enabled || current_user_can( 'manage_everest_forms' ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Validate screen options on update. |
| 294 | * |
| 295 | * @param bool|int $status Screen option value. Default false to skip. |
| 296 | * @param string $option The option name. |
| 297 | * @param int $value The number of rows to use. |
| 298 | */ |
| 299 | public function set_screen_option( $status, $option, $value ) { |
| 300 | if ( in_array( $option, array( 'evf_forms_per_page', 'evf_entries_per_page' ), true ) ) { |
| 301 | return $value; |
| 302 | } |
| 303 | |
| 304 | return $status; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Init the settings page. |
| 309 | */ |
| 310 | public function builder_page() { |
| 311 | EVF_Admin_Forms::page_output(); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Init the entries page. |
| 316 | */ |
| 317 | public function entries_page() { |
| 318 | EVF_Admin_Entries::page_output(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Init the settings page. |
| 323 | */ |
| 324 | public function settings_page() { |
| 325 | EVF_Admin_Settings::output(); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Init the status page. |
| 330 | */ |
| 331 | public function tools_page() { |
| 332 | EVF_Admin_Tools::output(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Init the addons page. |
| 337 | */ |
| 338 | public function addons_page() { |
| 339 | EVF_Admin_Addons::output(); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return new EVF_Admin_Menus(); |
| 344 |