-
Notifications
You must be signed in to change notification settings - Fork 41
Framework and admin screen to add new features as opt-in #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f761742
Just in case commit
cbravobernal 65768f5
Use only one file
cbravobernal 1050e0c
Fix header
cbravobernal 62de2ac
Add sidebar experiment
cbravobernal 46eafd1
Add prettier, typescript and hide the experiment for production site
cbravobernal df5362e
Update wp scripts
cbravobernal b940016
Add some small fixes
cbravobernal a139f6f
Revert "Update wp scripts"
cbravobernal e55102f
Use acf object, fix some version numbers
cbravobernal 8cdd4dc
Simplify js files
cbravobernal 8636385
Small refactor, remove readmes
cbravobernal 910a8b4
Remove redundant comments
cbravobernal d11fd6b
Add no experiments view
cbravobernal 1bba9e9
Move enqueue and localize to the same function
cbravobernal 86f2a47
Address some changes
cbravobernal 899cac7
Remove dates
cbravobernal 0cfa960
Mega movement to use acf localize and rename to beta features
cbravobernal c3930ad
Some refactor
cbravobernal 4881188
fix
cbravobernal cf4db82
Remove not needed TS (yet)
cbravobernal 8b71adb
Leave it disabled
cbravobernal 1e32490
Remove unnedded stuff
cbravobernal da68974
Remove disable filters
cbravobernal d17afcd
Update version, protect everything possible
cbravobernal a3b2021
Update version
cbravobernal 5a66327
Disable the experiment, update all
cbravobernal 585ff7b
Check submit must be public
cbravobernal caa8a1d
Remove not needed function
cbravobernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| <?php // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed | ||
| /** | ||
| * Admin Beta Features | ||
| * | ||
| * This file contains the admin beta features functionality for Secure Custom Fields. | ||
| * | ||
| * @package Secure Custom Fields | ||
| * @since 6.5.0 | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; // Exit if accessed directly | ||
| } | ||
|
|
||
| if ( ! class_exists( 'SCF_Admin_Beta_Features' ) ) : | ||
| /** | ||
| * Class SCF_Admin_Beta_Features | ||
| * | ||
| * This class provides different beta features that eventually will land on secure custom fields. | ||
| */ | ||
| class SCF_Admin_Beta_Features { | ||
|
|
||
| /** | ||
| * Contains an array of admin beta feature instances. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $beta_features = array(); | ||
|
|
||
| /** | ||
| * This function will setup the class functionality | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function __construct() { | ||
| // Temporarily disabled - will be enabled when beta feature is ready | ||
| // add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); | ||
| } | ||
|
|
||
| /** | ||
| * This function will store an beta feature class instance in the beta features array. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @param string $beta_feature Class name. | ||
| * @return void | ||
| */ | ||
| public function register_beta_feature( $beta_feature ) { | ||
| $instance = new $beta_feature(); | ||
| $this->beta_features[ $instance->name ] = $instance; | ||
| } | ||
|
|
||
| /** | ||
| * This function will return an beta feature class or null if not found. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @param string $name Name of beta feature. | ||
| * @return mixed (SCF_Admin_Beta_Feature|null) | ||
| */ | ||
| public function get_beta_feature( $name ) { | ||
| return isset( $this->beta_features[ $name ] ) ? $this->beta_features[ $name ] : null; | ||
| } | ||
|
|
||
| /** | ||
| * This function will return an array of all beta feature instances. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_beta_features() { | ||
| // Include beta features | ||
| $this->include_beta_features(); | ||
|
|
||
| return $this->beta_features; | ||
| } | ||
|
|
||
| /** | ||
| * Localizes the beta features data. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function localize_beta_features() { | ||
| $beta_features = array(); | ||
| foreach ( $this->get_beta_features() as $name => $beta_feature ) { | ||
| $beta_features[ $name ] = $beta_feature->is_enabled(); | ||
| } | ||
|
|
||
| acf_localize_data( | ||
| array( | ||
| 'betaFeatures' => $beta_features, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * This function will add the SCF beta features menu item to the WP admin | ||
| * | ||
| * @type action (admin_menu) | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function admin_menu() { | ||
| // bail early if no show_admin | ||
| if ( ! acf_get_setting( 'show_admin' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $page = add_submenu_page( 'edit.php?post_type=acf-field-group', __( 'Beta Features', 'secure-custom-fields' ), __( 'Beta Features', 'secure-custom-fields' ), acf_get_setting( 'capability' ), 'scf-beta-features', array( $this, 'html' ) ); | ||
|
|
||
| add_action( 'load-' . $page, array( $this, 'load' ) ); | ||
| add_action( 'admin_enqueue_scripts', array( $this, 'localize_beta_features' ), 20 ); | ||
| } | ||
|
|
||
| /** | ||
| * Loads the admin beta features page. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function load() { | ||
| add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); | ||
| // Include and register beta features before checking submit | ||
| $this->include_beta_features(); | ||
|
|
||
| $this->check_submit(); | ||
| } | ||
|
|
||
| /** | ||
| * Modifies the admin body class. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @param string $classes Space-separated list of CSS classes. | ||
| * @return string | ||
| */ | ||
| public function admin_body_class( $classes ) { | ||
| $classes .= ' acf-admin-page'; | ||
| return $classes; | ||
| } | ||
|
|
||
| /** | ||
| * Includes various beta feature-related files. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| private function include_beta_features() { | ||
| acf_include( 'includes/admin/beta-features/class-scf-beta-feature.php' ); | ||
| acf_include( 'includes/admin/beta-features/class-scf-beta-feature-editor-sidebar.php' ); | ||
|
|
||
| add_action( 'scf/include_admin_beta_features', array( $this, 'register_beta_features' ) ); | ||
|
|
||
| do_action( 'scf/include_admin_beta_features' ); | ||
| } | ||
|
|
||
| /** | ||
| * Register default beta features. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function register_beta_features() { | ||
| scf_register_admin_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' ); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies the nonces and submits the value if it passes. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function check_submit() { | ||
| // Check if form was submitted. | ||
| if ( ! isset( $_POST['scf_beta_features_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['scf_beta_features_nonce'] ), 'scf_beta_features_update' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $beta_features = $this->get_beta_features(); | ||
| $updated = false; | ||
|
|
||
| foreach ( $beta_features as $name => $beta_feature ) { | ||
| $enabled = isset( $_POST['scf_beta_features'][ $name ] ) && '1' === $_POST['scf_beta_features'][ $name ]; | ||
| $beta_feature->set_enabled( $enabled ); | ||
| $updated = true; | ||
| } | ||
|
|
||
| if ( $updated ) { | ||
| add_action( 'admin_notices', array( $this, 'admin_notices' ) ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Display admin notices. | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function admin_notices() { | ||
| ?> | ||
| <div class="notice notice-success is-dismissible"> | ||
| <p><?php esc_html_e( 'Beta feature settings updated successfully.', 'secure-custom-fields' ); ?></p> | ||
| </div> | ||
| <?php | ||
| } | ||
|
|
||
| /** | ||
| * Admin Beta Features html | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return void | ||
| */ | ||
| public function html() { | ||
| // vars | ||
| $screen = get_current_screen(); | ||
|
|
||
| // view | ||
| $view = array( | ||
| 'screen_id' => $screen->id, | ||
| ); | ||
|
|
||
| foreach ( $this->get_beta_features() as $name => $beta_feature ) { | ||
| add_meta_box( 'scf-admin-beta-feature-' . $name, acf_esc_html( $beta_feature->title ), array( $this, 'metabox_html' ), $screen->id, 'normal', 'default', array( 'beta_feature' => $name ) ); | ||
| } | ||
|
|
||
| acf_get_view( 'beta-features/beta-features', $view ); | ||
| } | ||
|
|
||
| /** | ||
| * Output the metabox HTML for specific beta features | ||
| * | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @param mixed $post The post this metabox is being displayed on, should be an empty string always for us on an beta features page. | ||
| * @param array $metabox An array of the metabox attributes. | ||
| */ | ||
| public function metabox_html( $post, $metabox ) { | ||
| $beta_feature = $this->get_beta_feature( $metabox['args']['beta_feature'] ); | ||
| $form_attrs = array( 'method' => 'post' ); | ||
|
|
||
| printf( '<form %s>', acf_esc_attrs( $form_attrs ) ); | ||
| $beta_feature->html(); | ||
| acf_nonce_input( $beta_feature->name ); | ||
| echo '</form>'; | ||
| } | ||
| } | ||
|
|
||
| // initialize | ||
| acf()->admin_beta_features = new SCF_Admin_Beta_Features(); | ||
| endif; // class_exists check | ||
|
|
||
| /** | ||
| * Alias of acf()->admin_beta_features->register_beta_feature() | ||
| * | ||
| * @type function | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @param string $beta_feature The beta feature class. | ||
| * @return void | ||
| */ | ||
| function scf_register_admin_beta_feature( $beta_feature ) { | ||
| acf()->admin_beta_features->register_beta_feature( $beta_feature ); | ||
| } | ||
|
|
||
| /** | ||
| * This function will return the admin URL to the beta features page | ||
| * | ||
| * @type function | ||
| * @since SCF 6.5.0 | ||
| * | ||
| * @return string The URL to the beta features page. | ||
| */ | ||
| function scf_get_admin_beta_features_url() { | ||
| return admin_url( 'edit.php?post_type=acf-field-group&page=scf-beta-features' ); | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
includes/admin/beta-features/class-scf-beta-feature-editor-sidebar.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
| /** | ||
| * Editor Sidebar Beta Feature | ||
| * | ||
| * This beta feature allows moving field group elements to the editor sidebar. | ||
| * | ||
| * @package Secure Custom Fields | ||
| * @since 6.5.0 | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; // Exit if accessed directly | ||
| } | ||
|
|
||
| if ( ! class_exists( 'SCF_Admin_Beta_Feature_Editor_Sidebar' ) ) : | ||
| /** | ||
| * Class SCF_Admin_Beta_Feature_Editor_Sidebar | ||
| * | ||
| * Implements a beta feature to move field group elements to the editor sidebar | ||
| * for a cleaner interface. | ||
| * | ||
| * @package Secure Custom Fields | ||
| * @since 6.5.0 | ||
| */ | ||
| class SCF_Admin_Beta_Feature_Editor_Sidebar extends SCF_Admin_Beta_Feature { | ||
|
|
||
| /** | ||
| * Initialize the beta feature. | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function initialize() { | ||
| $this->name = 'editor-sidebar'; | ||
| $this->title = __( 'Move Elements to Editor Sidebar', 'secure-custom-fields' ); | ||
| $this->description = __( 'Moves field group elements to the editor sidebar for a cleaner interface.', 'secure-custom-fields' ); | ||
|
|
||
| if ( $this->is_enabled() ) { | ||
| add_action( 'admin_init', array( $this, 'setup_beta_feature' ) ); | ||
| } | ||
| } | ||
| } | ||
| endif; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do a follow-up and create a REST endpoint for this.