'; return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Menu icon data URI. } /** * Registers the admin menu. */ public function menu() { $cap = Post_Type::capability(); add_menu_page( __( 'Buildify', 'buildify' ), __( 'Buildify', 'buildify' ), $cap, 'buildify', array( $this, 'render_dashboard' ), $this->menu_icon(), 58.7 ); add_submenu_page( 'buildify', __( 'Buildify Dashboard', 'buildify' ), __( 'Dashboard', 'buildify' ), $cap, 'buildify', array( $this, 'render_dashboard' ) ); add_submenu_page( 'buildify', __( 'Theme Templates', 'buildify' ), __( 'Theme Templates', 'buildify' ), $cap, 'edit.php?post_type=' . Post_Type::POST_TYPE ); if ( Settings::is_on( 'popups' ) ) { add_submenu_page( 'buildify', __( 'Popups', 'buildify' ), __( 'Popups', 'buildify' ), $cap, 'edit.php?post_type=' . Post_Type::POST_TYPE . '&buildify_type=popup' ); } add_submenu_page( 'buildify', __( 'Buildify Settings', 'buildify' ), __( 'Settings', 'buildify' ), 'manage_options', 'buildify-settings', array( $this, 'render_settings' ) ); // A page without a menu item: reached from the templates list and the Elementor editor. $hook = add_submenu_page( '', __( 'Display Conditions', 'buildify' ), '', $cap, 'buildify-conditions', array( $this, 'render_conditions_page' ) ); if ( $hook ) { add_action( 'load-' . $hook, static function () { // Pages without a menu item have no title in the admin header otherwise. $GLOBALS['title'] = __( 'Display Conditions', 'buildify' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Sets the admin page title for a page without a menu item. } ); } } /** * Keeps the Buildify menu open on template screens. * * @param string $parent_file Parent file. * @return string */ public function parent_file( $parent_file ) { $screen = get_current_screen(); if ( $screen && Post_Type::POST_TYPE === $screen->post_type ) { return 'buildify'; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Menu highlighting only. if ( isset( $_GET['page'] ) && 'buildify-conditions' === $_GET['page'] ) { return 'buildify'; } return $parent_file; } /** * Highlights the right submenu item. * * @param string|null $submenu_file Submenu file. * @return string|null */ public function submenu_file( $submenu_file ) { $screen = get_current_screen(); if ( $screen && Post_Type::POST_TYPE === $screen->post_type ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Menu highlighting only. $type = isset( $_GET['buildify_type'] ) ? sanitize_key( wp_unslash( $_GET['buildify_type'] ) ) : ''; return 'popup' === $type ? 'edit.php?post_type=' . Post_Type::POST_TYPE . '&buildify_type=popup' : 'edit.php?post_type=' . Post_Type::POST_TYPE; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Menu highlighting only. if ( isset( $_GET['page'] ) && 'buildify-conditions' === $_GET['page'] ) { return 'edit.php?post_type=' . Post_Type::POST_TYPE; } return $submenu_file; } /** * Plugin row links. * * @param string[] $links Links. * @return string[] */ public function action_links( $links ) { array_unshift( $links, sprintf( '%2$s', esc_url( admin_url( 'admin.php?page=buildify' ) ), esc_html__( 'Dashboard', 'buildify' ) ), sprintf( '%2$s', esc_url( admin_url( 'admin.php?page=buildify-settings' ) ), esc_html__( 'Settings', 'buildify' ) ) ); return $links; } /** * Registers the settings option. */ public function register_settings() { register_setting( 'buildify_settings', Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( Settings::class, 'sanitize' ), 'default' => Settings::defaults(), ) ); // Settings that change which modules load need a fresh conditions index and rewrite of widget lists. add_action( 'update_option_' . Settings::OPTION, array( Conditions::class, 'flush_index' ) ); } /** * Whether the current screen belongs to Buildify. * * @param string $hook Admin page hook. * @return bool */ private function is_buildify_screen( $hook ) { $screen = get_current_screen(); return false !== strpos( $hook, 'buildify' ) || ( $screen && Post_Type::POST_TYPE === $screen->post_type ); } /** * Enqueues admin assets. * * @param string $hook Admin page hook. */ public function enqueue( $hook ) { if ( ! $this->is_buildify_screen( $hook ) ) { return; } wp_enqueue_style( 'buildify-admin', \Buildify\Assets::url( 'css/admin', 'css' ), array(), BUILDIFY_VERSION ); wp_enqueue_script( 'buildify-admin', \Buildify\Assets::url( 'js/admin', 'js' ), array( 'wp-api-fetch', 'wp-i18n' ), BUILDIFY_VERSION, true ); wp_set_script_translations( 'buildify-admin', 'buildify' ); $rules = array(); $defs = Conditions::rule_definitions(); $names = Conditions::rule_labels(); foreach ( $defs as $key => $def ) { $rules[] = array( 'key' => $key, 'label' => isset( $names[ $key ] ) ? $names[ $key ] : $key, 'group' => $def['group'], 'sub' => $def['sub'], 'object' => $def['object'], ); } $roles = array(); foreach ( wp_roles()->get_names() as $role => $name ) { $roles[ $role ] = translate_user_role( $name ); } wp_localize_script( 'buildify-admin', 'buildifyAdmin', array( 'rules' => $rules, 'groups' => Conditions::group_labels(), 'postTypes' => Helpers::get_post_types(), 'taxonomies' => Helpers::get_taxonomies(), 'roles' => $roles, 'restPath' => '/' . \Buildify\Rest_Api::NAMESPACE_V1, 'i18n' => array( 'modeInclude' => __( 'Include', 'buildify' ), 'modeExclude' => __( 'Exclude', 'buildify' ), 'addCondition' => __( 'Add condition', 'buildify' ), 'remove' => __( 'Remove', 'buildify' ), 'all' => __( 'All', 'buildify' ), 'search' => __( 'Type to search…', 'buildify' ), 'save' => __( 'Save conditions', 'buildify' ), 'saving' => __( 'Saving…', 'buildify' ), 'saved' => __( 'Conditions saved.', 'buildify' ), 'error' => __( 'The conditions could not be saved. Please try again.', 'buildify' ), 'noConditions' => __( 'This template is not used anywhere yet. Add a condition to choose where it appears.', 'buildify' ), 'visibility' => __( 'Show to', 'buildify' ), 'everyone' => __( 'Everyone', 'buildify' ), 'loggedIn' => __( 'Logged-in users', 'buildify' ), 'loggedOut' => __( 'Logged-out visitors', 'buildify' ), 'roles' => __( 'Only these roles (optional)', 'buildify' ), 'priority' => __( 'Priority', 'buildify' ), 'priorityHelp' => __( 'When several templates match a page, the one with the highest priority is used. On equal priority, the most specific condition wins.', 'buildify' ), 'anyItem' => __( 'Any item', 'buildify' ), 'chooseItem' => __( 'Choose…', 'buildify' ), 'noResults' => __( 'Nothing found.', 'buildify' ), 'close' => __( 'Close', 'buildify' ), 'conditionsFor' => __( 'Display conditions', 'buildify' ), 'draftWarning' => __( 'This template is a draft. Publish it in Elementor to make it live.', 'buildify' ), 'confirmLeave' => __( 'You have unsaved changes.', 'buildify' ), 'loading' => __( 'Loading…', 'buildify' ), 'designNone' => __( 'Blank template', 'buildify' ), 'chooseTemplate' => __( 'Choose a template type first.', 'buildify' ), ), ) ); } /** * Notices after redirects. */ public function result_notices() { $screen = get_current_screen(); if ( ! $screen || ( false === strpos( (string) $screen->id, 'buildify' ) && Post_Type::POST_TYPE !== $screen->post_type ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Display only. if ( empty( $_GET['buildify_notice'] ) ) { return; } $notice = sanitize_key( wp_unslash( $_GET['buildify_notice'] ) ); $count = isset( $_GET['count'] ) ? absint( $_GET['count'] ) : 0; // phpcs:enable $messages = array( /* translators: %d: number of templates. */ 'imported' => array( 'success', sprintf( _n( '%d template imported as a draft.', '%d templates imported as drafts.', $count, 'buildify' ), $count ) ), 'import_missing' => array( 'error', __( 'Choose a JSON file to import.', 'buildify' ) ), 'import_invalid' => array( 'error', __( 'This file is not a valid Buildify export.', 'buildify' ) ), 'cache_cleared' => array( 'success', __( 'Page caches and the conditions cache were cleared.', 'buildify' ) ), 'css_cleared' => array( 'success', __( 'Elementor CSS files will be regenerated on the next page view.', 'buildify' ) ), 'stats_reset' => array( 'success', __( 'Popup statistics were reset.', 'buildify' ) ), 'status_changed' => array( 'success', __( 'Template status updated.', 'buildify' ) ), ); if ( ! isset( $messages[ $notice ] ) ) { return; } printf( '

%2$s

', esc_attr( $messages[ $notice ][0] ), esc_html( $messages[ $notice ][1] ) ); } /** * Handles the tools form. */ public function handle_tools() { check_admin_referer( 'buildify_tools' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'buildify' ), '', array( 'response' => 403 ) ); } $tool = isset( $_POST['tool'] ) ? sanitize_key( wp_unslash( $_POST['tool'] ) ) : ''; $notice = ''; switch ( $tool ) { case 'clear_cache': Conditions::flush_index(); \Buildify\Cache::purge_all(); $notice = 'cache_cleared'; break; case 'regenerate_css': if ( class_exists( '\Elementor\Plugin' ) ) { \Elementor\Plugin::$instance->files_manager->clear_cache(); } $notice = 'css_cleared'; break; case 'reset_stats': foreach ( Post_Type::get_ids( 'popup', 'any' ) as $popup_id ) { Popup_Stats::reset( $popup_id ); } $notice = 'stats_reset'; break; } wp_safe_redirect( add_query_arg( 'buildify_notice', $notice, admin_url( 'admin.php?page=buildify-settings&tab=tools' ) ) ); exit; } /** * Dashboard screen. */ public function render_dashboard() { if ( ! current_user_can( Post_Type::capability() ) ) { return; } $types = Template_Types::definitions(); $labels = Template_Types::labels(); $descs = Template_Types::descriptions(); $designs = Import_Export::starter_designs(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Preselects a type in the form. $preselect = isset( $_GET['new'] ) ? sanitize_key( wp_unslash( $_GET['new'] ) ) : ''; $active = array(); foreach ( Conditions::get_index() as $type => $templates ) { $active[ $type ] = count( $templates ); } include BUILDIFY_PATH . 'includes/admin/views/dashboard.php'; } /** * Settings screen. */ public function render_settings() { if ( ! current_user_can( 'manage_options' ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Tab navigation only. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'general'; $tabs = array( 'general' => __( 'General', 'buildify' ), 'popups' => __( 'Popups', 'buildify' ), 'tools' => __( 'Tools', 'buildify' ), ); $tab = isset( $tabs[ $tab ] ) ? $tab : 'general'; $settings = Settings::all(); include BUILDIFY_PATH . 'includes/admin/views/settings.php'; } /** * Display conditions screen. */ public function render_conditions_page() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen; saving uses the REST API with a nonce. $template_id = isset( $_GET['template'] ) ? absint( $_GET['template'] ) : 0; if ( ! $template_id || Post_Type::POST_TYPE !== get_post_type( $template_id ) || ! current_user_can( 'edit_post', $template_id ) ) { wp_die( esc_html__( 'Template not found.', 'buildify' ) ); } $type = Template_Types::of( $template_id ); $definition = Template_Types::get( $type ); $document = class_exists( '\Elementor\Plugin' ) ? \Elementor\Plugin::$instance->documents->get( $template_id ) : null; include BUILDIFY_PATH . 'includes/admin/views/conditions.php'; } /** * Plugin instance shortcut for views. * * @return Plugin */ public static function plugin() { return Plugin::instance(); } }