beta-features
1 year ago
post-types
1 year ago
tools
1 year ago
views
1 year ago
admin-commands.php
1 year ago
admin-internal-post-type-list.php
1 year ago
admin-internal-post-type.php
1 year ago
admin-notices.php
1 year ago
admin-tools.php
1 year ago
admin-upgrade.php
1 year ago
admin.php
1 year ago
beta-features.php
1 year ago
class-acf-admin-options-page.php
1 year ago
index.php
1 year ago
beta-features.php
288 lines
| 1 | <?php // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed |
| 2 | /** |
| 3 | * Admin Beta Features |
| 4 | * |
| 5 | * This file contains the admin beta features functionality for Secure Custom Fields. |
| 6 | * |
| 7 | * @package Secure Custom Fields |
| 8 | * @since SCF 6.5.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'SCF_Admin_Beta_Features' ) ) : |
| 16 | /** |
| 17 | * Class SCF_Admin_Beta_Features |
| 18 | * |
| 19 | * This class provides different beta features that eventually will land on secure custom fields. |
| 20 | */ |
| 21 | class SCF_Admin_Beta_Features { |
| 22 | |
| 23 | /** |
| 24 | * Contains an array of admin beta feature instances. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private $beta_features = array(); |
| 29 | |
| 30 | /** |
| 31 | * This function will setup the class functionality |
| 32 | * |
| 33 | * @since SCF 6.5.0 |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | // Temporarily disabled - will be enabled when beta feature is ready |
| 39 | // add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * This function will store an beta feature class instance in the beta features array. |
| 44 | * |
| 45 | * @since SCF 6.5.0 |
| 46 | * |
| 47 | * @param string $beta_feature Class name. |
| 48 | * @return void |
| 49 | */ |
| 50 | public function register_beta_feature( $beta_feature ) { |
| 51 | $instance = new $beta_feature(); |
| 52 | $this->beta_features[ $instance->name ] = $instance; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * This function will return an beta feature class or null if not found. |
| 57 | * |
| 58 | * @since SCF 6.5.0 |
| 59 | * |
| 60 | * @param string $name Name of beta feature. |
| 61 | * @return mixed (SCF_Admin_Beta_Feature|null) |
| 62 | */ |
| 63 | public function get_beta_feature( $name ) { |
| 64 | return isset( $this->beta_features[ $name ] ) ? $this->beta_features[ $name ] : null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This function will return an array of all beta feature instances. |
| 69 | * |
| 70 | * @since SCF 6.5.0 |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public function get_beta_features() { |
| 75 | // Include beta features |
| 76 | $this->include_beta_features(); |
| 77 | |
| 78 | return $this->beta_features; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Localizes the beta features data. |
| 83 | * |
| 84 | * @since SCF 6.5.0 |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function localize_beta_features() { |
| 89 | $beta_features = array(); |
| 90 | foreach ( $this->get_beta_features() as $name => $beta_feature ) { |
| 91 | $beta_features[ $name ] = $beta_feature->is_enabled(); |
| 92 | } |
| 93 | |
| 94 | acf_localize_data( |
| 95 | array( |
| 96 | 'betaFeatures' => $beta_features, |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * This function will add the SCF beta features menu item to the WP admin |
| 103 | * |
| 104 | * @type action (admin_menu) |
| 105 | * @since SCF 6.5.0 |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function admin_menu() { |
| 110 | // bail early if no show_admin |
| 111 | if ( ! acf_get_setting( 'show_admin' ) ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $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' ) ); |
| 116 | |
| 117 | add_action( 'load-' . $page, array( $this, 'load' ) ); |
| 118 | add_action( 'admin_enqueue_scripts', array( $this, 'localize_beta_features' ), 20 ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Loads the admin beta features page. |
| 123 | * |
| 124 | * @since SCF 6.5.0 |
| 125 | * |
| 126 | * @return void |
| 127 | */ |
| 128 | public function load() { |
| 129 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 130 | // Include and register beta features before checking submit |
| 131 | $this->include_beta_features(); |
| 132 | |
| 133 | $this->check_submit(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Modifies the admin body class. |
| 138 | * |
| 139 | * @since SCF 6.5.0 |
| 140 | * |
| 141 | * @param string $classes Space-separated list of CSS classes. |
| 142 | * @return string |
| 143 | */ |
| 144 | public function admin_body_class( $classes ) { |
| 145 | $classes .= ' acf-admin-page'; |
| 146 | return $classes; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Includes various beta feature-related files. |
| 151 | * |
| 152 | * @since SCF 6.5.0 |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | private function include_beta_features() { |
| 157 | acf_include( 'includes/admin/beta-features/class-scf-beta-feature.php' ); |
| 158 | acf_include( 'includes/admin/beta-features/class-scf-beta-feature-editor-sidebar.php' ); |
| 159 | |
| 160 | add_action( 'scf/include_admin_beta_features', array( $this, 'register_beta_features' ) ); |
| 161 | |
| 162 | do_action( 'scf/include_admin_beta_features' ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Register default beta features. |
| 167 | * |
| 168 | * @since SCF 6.5.0 |
| 169 | * |
| 170 | * @return void |
| 171 | */ |
| 172 | public function register_beta_features() { |
| 173 | scf_register_admin_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Verifies the nonces and submits the value if it passes. |
| 178 | * |
| 179 | * @since SCF 6.5.0 |
| 180 | * |
| 181 | * @return void |
| 182 | */ |
| 183 | public function check_submit() { |
| 184 | // Check if form was submitted. |
| 185 | if ( ! isset( $_POST['scf_beta_features_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['scf_beta_features_nonce'] ), 'scf_beta_features_update' ) ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $beta_features = $this->get_beta_features(); |
| 190 | $updated = false; |
| 191 | |
| 192 | foreach ( $beta_features as $name => $beta_feature ) { |
| 193 | $enabled = isset( $_POST['scf_beta_features'][ $name ] ) && '1' === $_POST['scf_beta_features'][ $name ]; |
| 194 | $beta_feature->set_enabled( $enabled ); |
| 195 | $updated = true; |
| 196 | } |
| 197 | |
| 198 | if ( $updated ) { |
| 199 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Display admin notices. |
| 205 | * |
| 206 | * @since SCF 6.5.0 |
| 207 | * |
| 208 | * @return void |
| 209 | */ |
| 210 | public function admin_notices() { |
| 211 | ?> |
| 212 | <div class="notice notice-success is-dismissible"> |
| 213 | <p><?php esc_html_e( 'Beta feature settings updated successfully.', 'secure-custom-fields' ); ?></p> |
| 214 | </div> |
| 215 | <?php |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Admin Beta Features html |
| 220 | * |
| 221 | * @since SCF 6.5.0 |
| 222 | * |
| 223 | * @return void |
| 224 | */ |
| 225 | public function html() { |
| 226 | // vars |
| 227 | $screen = get_current_screen(); |
| 228 | |
| 229 | // view |
| 230 | $view = array( |
| 231 | 'screen_id' => $screen->id, |
| 232 | ); |
| 233 | |
| 234 | foreach ( $this->get_beta_features() as $name => $beta_feature ) { |
| 235 | 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 ) ); |
| 236 | } |
| 237 | |
| 238 | acf_get_view( 'beta-features/beta-features', $view ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Output the metabox HTML for specific beta features |
| 243 | * |
| 244 | * @since SCF 6.5.0 |
| 245 | * |
| 246 | * @param mixed $post The post this metabox is being displayed on, should be an empty string always for us on an beta features page. |
| 247 | * @param array $metabox An array of the metabox attributes. |
| 248 | */ |
| 249 | public function metabox_html( $post, $metabox ) { |
| 250 | $beta_feature = $this->get_beta_feature( $metabox['args']['beta_feature'] ); |
| 251 | $form_attrs = array( 'method' => 'post' ); |
| 252 | |
| 253 | printf( '<form %s>', acf_esc_attrs( $form_attrs ) ); |
| 254 | $beta_feature->html(); |
| 255 | acf_nonce_input( $beta_feature->name ); |
| 256 | echo '</form>'; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // initialize |
| 261 | acf()->admin_beta_features = new SCF_Admin_Beta_Features(); |
| 262 | endif; // class_exists check |
| 263 | |
| 264 | /** |
| 265 | * Alias of acf()->admin_beta_features->register_beta_feature() |
| 266 | * |
| 267 | * @type function |
| 268 | * @since SCF 6.5.0 |
| 269 | * |
| 270 | * @param string $beta_feature The beta feature class. |
| 271 | * @return void |
| 272 | */ |
| 273 | function scf_register_admin_beta_feature( $beta_feature ) { |
| 274 | acf()->admin_beta_features->register_beta_feature( $beta_feature ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * This function will return the admin URL to the beta features page |
| 279 | * |
| 280 | * @type function |
| 281 | * @since SCF 6.5.0 |
| 282 | * |
| 283 | * @return string The URL to the beta features page. |
| 284 | */ |
| 285 | function scf_get_admin_beta_features_url() { |
| 286 | return admin_url( 'edit.php?post_type=acf-field-group&page=scf-beta-features' ); |
| 287 | } |
| 288 |