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