Plugin Directory

Changeset 3064428


Ignore:
Timestamp:
04/04/2024 08:28:52 AM (2 years ago)
Author:
khr2003
Message:

Update version in main file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • editor-block-outline/tags/1.3.1/outline.php

    r3064422 r3064428  
    88License: GPLv2 or later
    99Text Domain: editor-outline
    10 */
    11 if (!defined('ABSPATH')) {
    12     exit;
     10 */
     11
     12if ( ! defined( 'ABSPATH' ) ) {
     13    exit;
    1314}
    1415
    1516require_once( 'promotions.php' );
    16 
    1717/**
    18  * Editor outline class to wrap plugin functions and actions.
     18 * Editor outline class to wrap plugin functions and actions
    1919 */
    20 class EditorOutline
    21 {
    22     /**
    23     * Constructor function.
    24     */
    25     public function __construct()
    26     {
    27         add_action('init', array($this, 'register_user_meta'));
    28         add_action('enqueue_block_editor_assets', array($this, 'add_editor_assets'));
    29         add_action('enqueue_block_editor_assets', array($this, 'add_init_scripts'));
    30     }
    31 
    32     /**
    33      * Register user meta to save settings for each user separately.
    34     *
    35     * @return void
    36     */
    37     public function registerUserMeta()
    38     {
    39         /**
     20class EditorOutline {
     21
     22    /**
     23    * Constructor function.
     24    */
     25    public function __construct() {
     26        add_action( 'init', array( $this, 'register_user_meta' ) );
     27        add_action( 'enqueue_block_editor_assets', array( $this, 'add_editor_assets' ) );
     28        add_action( 'enqueue_block_editor_assets', array( $this, 'add_init_scripts' ) );
     29    }
     30
     31
     32    /**
     33     * Register user meta to save settings for each user separately
     34    *
     35    * @return void
     36    */
     37    public function register_user_meta() {
     38
     39        /**
    4040         * Create default user meta options
    4141         */
    42         $default_user_meta = array(
    43             '_enable_block_outline' => array(
    44                 'type' => 'string',
    45                 'default' => 'hover',
    46             ),
    47             '_lock_block_outline' => array(
    48                 'type' => 'boolean',
    49                 'default' => true,
    50             ),
    51             '_show_block_name' => array(
    52                 'type' => 'boolean',
    53                 'default' => true,
    54             ),
    55             '_show_class_name' => array(
    56                 'type' => 'boolean',
    57                 'default' => false,
    58             ),
    59             '_block_outline_color' => array(
    60                 'type' => 'string',
    61                 'default' => '#bdc3c7',
    62             ),
    63             '_block_data_position' => array(
    64                 'type' => 'string',
    65                 'default' => 'outside',
    66             ),
    67             '_block_outline_style' => array(
    68                 'type' => 'string',
    69                 'default' => 'solid',
    70             ),
    71             '_block_outline_opacity' => array(
    72                 'type' => 'number',
    73                 'default' => 50,
    74             ),
    75             '_enable_block_outline_padding' => array(
    76                 'type' => 'boolean',
    77                 'default' => false,
    78             ),
    79             '_block_outline_padding' => array(
    80                 'type' => 'number',
    81                 'default' => 3,
    82             ),
    83         );
    84 
    85         $default_user_meta = $this->processMetaFilter($default_user_meta);
    86 
    87         $standard_options = array(
    88             'single' => true,
    89             'show_in_rest' => true,
    90             'auth_callback' => function() {
    91                 return current_user_can('edit_posts');
    92             },
    93         );
    94 
    95         // Merge default meta options and register meta
    96         foreach ($default_user_meta as $meta_key => $meta_value) {
    97             register_meta(
    98                 'user',
    99                 $meta_key,
    100                 array_merge($meta_value, $standard_options)
    101             );
    102         }
    103     }
    104 
    105     /**
    106      * Custom version of wp_die() to show error messages.
    107      *
    108      * @param {string|array} $message Message to show
    109      *
    110      * @return void
    111      */
    112     private function wpDie($message)
    113     {
    114         $message = is_array($message) ? $message : array($message);
    115 
    116         $heading = '<h2>Editor Block Outline</h2>';
    117         $heading .= '<h4>Filter: editor_outline_default_user_meta</h4>';
    118         array_unshift($message, $heading);
    119 
    120         error_log(print_r($message, 1));
    121         wp_die(implode('<br>', $message));
    122     }
    123 
    124     /**
    125      *
    126      *
    127      * @param array $default_user_meta
    128      * @return array
    129      */
    130     private function processMetaFilter(array $default_user_meta): array
    131     {
    132         // Create an associative array of default user meta options
    133         $user_meta_associative = array_map(function($item) {
    134                 return $item['default'];
    135         }, $default_user_meta);
    136 
    137         /**
     42        $default_user_meta = array(
     43            '_enable_block_outline' => array(
     44                'type'          => 'string',
     45                'default'       => 'hover',
     46            ),
     47            '_lock_block_outline' => array(
     48                'type'          => 'boolean',
     49                'default'       => true,
     50            ),
     51            '_show_block_name' => array(
     52                'type'          => 'boolean',
     53                'default'       => true,
     54            ),
     55            '_show_class_name' => array(
     56                'type'          => 'boolean',
     57                'default'       => false,
     58            ),
     59            '_block_outline_color' => array(
     60                'type'          => 'string',
     61                'default'       => '#bdc3c7',
     62            ),
     63            '_block_data_position' => array(
     64                'type'          => 'string',
     65                'default'       => 'outside',
     66            ),
     67            '_block_outline_style' => array(
     68                'type'          => 'string',
     69                'default'       => 'solid',
     70            ),
     71            '_block_outline_opacity' => array(
     72                'type'          => 'number',
     73                'default'       => 50,
     74            ),
     75            '_enable_block_outline_padding' => array(
     76                'type'          => 'boolean',
     77                'default'       => false,
     78            ),
     79            '_block_outline_padding' => array(
     80                'type'          => 'number',
     81                'default'       => 3,
     82            ),
     83        );
     84
     85        $default_user_meta = $this->process_meta_filter($default_user_meta);
     86
     87        $standard_options = array(
     88            'single'        => true,
     89            'show_in_rest'  => true,
     90            'auth_callback' => function () {
     91                return current_user_can( 'edit_posts' );
     92            },
     93        );
     94
     95        // Merge default meta options and register meta
     96        foreach ( $default_user_meta as $meta_key => $meta_value ) {
     97
     98            register_meta(
     99                'user',
     100                $meta_key,
     101                array_merge( $meta_value, $standard_options )
     102            );
     103        }
     104    }
     105
     106    /**
     107     * Custom version of wp_die() to show error messages
     108     *
     109     * @param {string|array} $message Message to show
     110     *
     111     * @return void
     112     */
     113    private function wp_die($message) {
     114        $message = is_array($message) ? $message : array($message);
     115
     116        $heading = "<h2>Editor Block Outline</h2>";
     117        $heading .= "<h4>Filter: editor_outline_default_user_meta</h4>";
     118        array_unshift($message, $heading);
     119
     120        error_log(print_r($message,1));
     121        wp_die(implode('<br>', $message));
     122    }
     123
     124
     125    private function process_meta_filter(array $default_user_meta): array {
     126        // Create an associative array of default user meta options
     127        $user_meta_associative = array_map( function( $item ) {
     128            return $item['default'];
     129        }, $default_user_meta );
     130
     131        /**
    138132         * Add filter to allow modifying user meta options
    139133         * @since 1.3.0
    140134         */
    141         $filtered_user_meta = apply_filters('editor_outline_default_user_meta', $user_meta_associative);
    142 
    143         // Make sure filtered user meta is an array
    144         if (!is_array($filtered_user_meta)) {
    145             $this->wpDie(
    146                 'Default user meta options must be an <strong>array</strong>. You passed <strong>' . gettype($filtered_user_meta) . '</strong>.',
    147             );
    148         }
    149 
    150         foreach ($filtered_user_meta as $meta_key => $meta_value) {
    151             // Check that key exists in default user meta
    152             if (!array_key_exists($meta_key, $default_user_meta)) {
    153                 $available_options = array_keys($default_user_meta);
    154                 $formatted_options = array_map(function($item) {
    155                         return "<li>{$item}</li>";
    156                 }, $available_options);
    157 
    158                 $joined_options = implode('', $formatted_options);
    159                 $this->wpDie(array(
    160                         "User meta option <strong>{$meta_key}</strong> does not exist in default user meta.",
    161                         "Available options are: <ul>{$joined_options}</ul>",
    162                     ));
    163             }
    164 
    165             // Check that value type matches default user meta
    166             $default_key_type = $default_user_meta[ $meta_key ]['type'];
    167             $filtered_meta_type = gettype($meta_value);
    168 
    169             if ($filtered_meta_type === 'integer') {
    170                 $filtered_meta_type = 'number';
    171             }
    172 
    173             if ($filtered_meta_type !== $default_key_type) {
    174                 $this->wpDie(array(
    175                         "User meta option <strong>{$meta_key}</strong> must be of type <strong>$default_key_type</strong>.",
    176                         'You passed <strong>' . gettype($meta_value) . '</strong>.',
    177                     ));
    178             }
    179 
    180             // Boolean values are passed as strings, so we need to convert them back to booleans
    181             if ($default_key_type === 'boolean') {
    182                 $meta_value = filter_var($meta_value, FILTER_VALIDATE_BOOLEAN);
    183             }
    184 
    185             // Override default user meta with filtered user meta
    186             $default_user_meta[ $meta_key ]['default'] = $meta_value;
    187         }
    188 
    189         return $default_user_meta;
    190     }
    191 
    192     /**
    193      * Add js/css file to Gutenberg editor.
    194      *
    195      * @return void
    196      */
    197     public function addEditorAssets()
    198     {
    199         wp_enqueue_script('jquery-color');
    200 
    201         // Enqueue scripts
    202         $scripts = array(
    203             array(
    204                 'file_path' => 'controls.js',
    205                 'deps' => array(
    206                     'wp-element',
    207                     'wp-components',
    208                     'wp-data',
    209                 ),
    210             ),
    211             array(
    212                 'file_path' => 'controls/lines-option.js',
    213                 'deps' => array('wp-data'),
    214             ),
    215             array(
    216                 'file_path' => 'controls/block-name-option.js',
    217                 'deps' => array('wp-data'),
    218             ),
    219             array(
    220                 'file_path' => 'controls/block-class-option.js',
    221                 'deps' => array('wp-data'),
    222             ),
    223             array(
    224                 'file_path' => 'controls/lock-block-outline.js',
    225                 'deps' => array('wp-data'),
    226             ),
    227             array(
    228                 'file_path' => 'controls/line-color-option.js',
    229                 'deps' => array('wp-data'),
    230             ),
    231             array(
    232                 'file_path' => 'controls/line-style-option.js',
    233                 'deps' => array('wp-data'),
    234             ),
    235             array(
    236                 'file_path' => 'controls/block-data-position.js',
    237                 'deps' => array('wp-data'),
    238             ),
    239             array(
    240                 'file_path' => 'controls/line-opacity-option.js',
    241                 'deps' => array('wp-data'),
    242             ),
    243             array(
    244                 'file_path' => 'controls/enable-block-padding-option.js',
    245                 'deps' => array('wp-data'),
    246             ),
    247             array(
    248                 'file_path' => 'controls/block-padding-option.js',
    249                 'deps' => array('wp-data'),
    250             ),
    251             array(
    252                 'file_path' => 'sidebar.js',
    253                 'deps' => array(
    254                     'wp-i18n',
    255                     'wp-blocks',
    256                     'wp-edit-post',
    257                     'wp-element',
    258                     'wp-editor',
    259                     'wp-plugins',
    260                 ),
    261             ),
    262             array(
    263                 'file_path' => 'icon.js',
    264                 'deps' => array('wp-element'),
    265             ),
    266             array(
    267                 'file_path' => 'mouse-events.js',
    268                 'deps' => array('jquery'),
    269             ),
    270         );
    271 
    272         // Loop through scripts, prepare enqueue arguments and add
    273         foreach ($scripts as $script_details) {
    274             $find = array('.js', '/');
    275             $replace = array('', '-');
    276             $handle = str_replace($find, $replace, $script_details['file_path']);
    277 
    278             $src = plugins_url($script_details['file_path'], __FILE__);
    279             $deps = $script_details['deps'];
    280             $version = filemtime(dirname(__FILE__) . '/' . $script_details['file_path']);
    281 
    282             wp_enqueue_script($handle, $src, $deps, $version);
    283         }
    284 
    285         wp_enqueue_style(
    286             'gird-style',
    287             plugin_dir_url(__FILE__) . '/block-editor.css',
    288             false,
    289             filemtime(dirname(__FILE__) . '/block-editor.css'),
    290             'all'
    291         );
    292     }
    293 
    294     /**
    295      * Add init script to initialize data.
    296      *
    297      * @return void
    298      */
    299     public function addInitScripts()
    300     {
    301         wp_enqueue_script(
    302             'outlines-init',
    303             plugins_url('init.js', __FILE__),
    304             array('jquery'),
    305             filemtime(dirname(__FILE__) . '/init.js')
    306         );
    307 
    308         $current_user = get_current_user_id();
    309         $show_block_name = get_user_meta($current_user, '_show_block_name', true);
    310         $show_class_name = get_user_meta($current_user, '_show_class_name', true);
    311         $lock_outline = get_user_meta($current_user, '_lock_block_outline', true);
    312         $block_data_position = get_user_meta($current_user, '_block_data_position', true);
    313         $enable_outline_padding = get_user_meta($current_user, '_enable_outline_padding', true);
    314         $outline_options = array(
    315             'show_outline' => get_user_meta($current_user, '_enable_block_outline', true),
    316             'show_block_name' => ( $show_block_name ) ? 'true' : 'false',
    317             'show_class_name' => ( $show_class_name ) ? 'true' : 'false',
    318             'lock_block_outline' => ( $lock_outline ) ? 'true' : 'false',
    319             'block_data_position' => ( $block_data_position ) ? $block_data_position : 'outside',
    320             'outline_color' => get_user_meta($current_user, '_block_outline_color', true),
    321             'outline_style' => get_user_meta($current_user, '_block_outline_style', true),
    322             'outline_opacity' => get_user_meta($current_user, '_block_outline_opacity', true),
    323             'enable_outline_padding' => ( $enable_outline_padding ) ? 'true' : 'false',
    324             'outline_padding' => get_user_meta($current_user, '_block_outline_padding', true),
    325         );
    326         wp_localize_script('outlines-init', 'outlineUserOptions', $outline_options);
    327     }
     135        $filtered_user_meta = apply_filters( 'editor_outline_default_user_meta', $user_meta_associative );
     136
     137        // Make sure filtered user meta is an array
     138        if ( !is_array( $filtered_user_meta ) ) {
     139            $this->wp_die(
     140                "Default user meta options must be an <strong>array</strong>. You passed <strong>" . gettype( $filtered_user_meta ) . "</strong>.",
     141            );
     142        }
     143
     144        foreach ( $filtered_user_meta as $meta_key => $meta_value ) {
     145            // Check that key exists in default user meta
     146            if ( !array_key_exists( $meta_key, $default_user_meta ) ) {
     147                $available_options = array_keys( $default_user_meta );
     148                $formatted_options = array_map( function( $item ) {
     149                    return "<li>{$item}</li>";
     150                }, $available_options );
     151
     152                $joined_options = implode( '', $formatted_options );
     153                $this->wp_die([
     154                    "User meta option <strong>{$meta_key}</strong> does not exist in default user meta.",
     155                    "Available options are: <ul>{$joined_options}</ul>"
     156                ]);
     157            }
     158
     159            // Check that value type matches default user meta
     160            $default_key_type =  $default_user_meta[ $meta_key ]['type'];
     161            $filtered_meta_type = gettype( $meta_value );
     162
     163            if( $filtered_meta_type === 'integer' ) {
     164                $filtered_meta_type = 'number';
     165            }
     166
     167            if ( $filtered_meta_type !== $default_key_type ) {
     168                $this->wp_die([
     169                    "User meta option <strong>{$meta_key}</strong> must be of type <strong>$default_key_type</strong>.",
     170                    "You passed <strong>" . gettype( $meta_value ) . "</strong>."
     171                ]);
     172            }
     173
     174            // Boolean values are passed as strings, so we need to convert them back to booleans
     175            if ( $default_key_type === 'boolean' ) {
     176                $meta_value = filter_var($meta_value, FILTER_VALIDATE_BOOLEAN);
     177            }
     178
     179            // Override default user meta with filtered user meta
     180            $default_user_meta[ $meta_key ]['default'] = $meta_value;
     181        }
     182
     183        return $default_user_meta;
     184    }
     185
     186    /**
     187     * Add js/css file to Gutenberg editor
     188     *
     189     * @return void
     190     */
     191    public function add_editor_assets() {
     192        wp_enqueue_script( 'jquery-color' );
     193
     194        // Enqueue scripts
     195        $scripts = array(
     196            array(
     197                'file_path' => 'controls.js',
     198                'deps' => array(
     199                    'wp-element',
     200                    'wp-components',
     201                    'wp-data'
     202                ),
     203            ),
     204            array(
     205                'file_path' => 'controls/lines-option.js',
     206                'deps' => array( 'wp-data' ),
     207            ),
     208            array(
     209                'file_path' => 'controls/block-name-option.js',
     210                'deps' => array( 'wp-data' ),
     211            ),
     212            array(
     213                'file_path' => 'controls/block-class-option.js',
     214                'deps' => array( 'wp-data' ),
     215            ),
     216            array(
     217                'file_path' => 'controls/lock-block-outline.js',
     218                'deps' => array( 'wp-data' ),
     219            ),
     220            array(
     221                'file_path' => 'controls/line-color-option.js',
     222                'deps' => array( 'wp-data' ),
     223            ),
     224            array(
     225                'file_path' => 'controls/line-style-option.js',
     226                'deps' => array( 'wp-data' ),
     227            ),
     228            array(
     229                'file_path' => 'controls/block-data-position.js',
     230                'deps' => array( 'wp-data' ),
     231            ),
     232            array(
     233                'file_path' => 'controls/line-opacity-option.js',
     234                'deps' => array( 'wp-data' ),
     235            ),
     236            array(
     237                'file_path' => 'controls/enable-block-padding-option.js',
     238                'deps' => array( 'wp-data' ),
     239            ),
     240            array(
     241                'file_path' => 'controls/block-padding-option.js',
     242                'deps' => array( 'wp-data' ),
     243            ),
     244            array(
     245                'file_path' => 'sidebar.js',
     246                'deps' => array(
     247                    'wp-i18n',
     248                    'wp-blocks',
     249                    'wp-edit-post',
     250                    'wp-element',
     251                    'wp-editor',
     252                    'wp-plugins'
     253                ),
     254            ),
     255            array(
     256                'file_path' => 'icon.js',
     257                'deps' => array( 'wp-element' ),
     258            ),
     259            array(
     260                'file_path' => 'mouse-events.js',
     261                'deps' => array( 'jquery' ),
     262            )
     263        );
     264
     265        // Loop through scripts, prepare enqueue arguments and add
     266        foreach ( $scripts as $script_details ) {
     267            $find = array( '.js', '/' );
     268            $replace = array( '', '-' );
     269            $handle = str_replace( $find, $replace, $script_details['file_path'] );
     270
     271            $src = plugins_url( $script_details['file_path'], __FILE__ );
     272            $deps = $script_details['deps'];
     273            $version = filemtime( dirname( __FILE__ ) . '/' . $script_details['file_path'] );
     274
     275            wp_enqueue_script( $handle, $src, $deps, $version );
     276        }
     277
     278        wp_enqueue_style(
     279            'gird-style',
     280            plugin_dir_url( __FILE__ ) . '/block-editor.css',
     281            false,
     282            filemtime( dirname( __FILE__ ) . '/block-editor.css' ),
     283            'all'
     284        );
     285    }
     286
     287    /**
     288     * Add init script to initialize data
     289     *
     290     * @return void
     291     */
     292    public function add_init_scripts() {
     293        wp_enqueue_script(
     294            'outlines-init',
     295            plugins_url( 'init.js', __FILE__ ),
     296            array( 'jquery' ),
     297            filemtime( dirname( __FILE__ ) . '/init.js' )
     298        );
     299
     300        $current_user     = get_current_user_id();
     301        $show_block_name = get_user_meta( $current_user, '_show_block_name', true );
     302        $show_class_name = get_user_meta( $current_user, '_show_class_name', true );
     303        $lock_outline = get_user_meta( $current_user, '_lock_block_outline', true );
     304        $block_data_position = get_user_meta( $current_user, '_block_data_position', true );
     305        $enable_outline_padding = get_user_meta( $current_user, '_enable_outline_padding', true );
     306        $outline_options  = array(
     307            'show_outline'    => get_user_meta( $current_user, '_enable_block_outline', true ),
     308            'show_block_name' => ( $show_block_name ) ? 'true' : 'false',
     309            'show_class_name' => ( $show_class_name ) ? 'true' : 'false',
     310            'lock_block_outline' => ( $lock_outline ) ? 'true' : 'false',
     311            'block_data_position' => ( $block_data_position ) ? $block_data_position : 'outside',
     312            'outline_color'   => get_user_meta( $current_user, '_block_outline_color', true ),
     313            'outline_style'   => get_user_meta( $current_user, '_block_outline_style', true ),
     314            'outline_opacity' => get_user_meta( $current_user, '_block_outline_opacity', true ),
     315            'enable_outline_padding' => ( $enable_outline_padding ) ? 'true' : 'false',
     316            'outline_padding' => get_user_meta( $current_user, '_block_outline_padding', true ),
     317        );
     318        wp_localize_script( 'outlines-init', 'outlineUserOptions', $outline_options );
     319    }
    328320}
    329321
Note: See TracChangeset for help on using the changeset viewer.