Plugin Directory

Changeset 2263986


Ignore:
Timestamp:
03/19/2020 03:54:16 PM (6 years ago)
Author:
leprincenoir
Message:

add Gutenberg compatibility

Location:
sticky-cpt
Files:
14 added
4 edited

Legend:

Unmodified
Added
Removed
  • sticky-cpt/trunk/includes/class-sticky-cpt-loader.php

    r1520497 r2263986  
    1111
    1212        public function __construct() {
     13
    1314            add_action( 'admin_init', array( $this, 'init' ) );
     15
    1416            add_action( 'admin_footer-post.php', array( $this , 'add_sticky' ) );
    1517            add_action( 'admin_footer-post-new.php', array( $this , 'add_sticky' ) );
    1618            add_action( 'admin_footer-edit.php', array( $this, 'add_sticky_quick_edit' ) );
     19
     20            add_action( 'enqueue_block_editor_assets', array($this, 'block_enqueue'), 20 );
     21
     22            add_action('init', array($this, 'register_meta_sticky_cpt'));
     23
     24            add_action( 'added_post_meta', array($this, 'added_post_meta_sticky'), 10, 4 );
     25            add_action( 'updated_post_meta', array($this, 'added_post_meta_sticky'), 10, 4 );
     26            add_action( 'deleted_post_meta', array($this, 'deleted_post_meta_sticky'), 10, 4 );
     27
     28            add_action('add_option_sticky_posts', array($this, 'add_option_sticky_posts'), 10, 2);
     29            add_action('update_option_sticky_posts', array($this, 'update_option_sticky_posts'), 15, 2);
     30
     31
     32            add_action('admin_notices', array($this, 'general_admin_notice'));
     33        }
     34
     35
     36        function general_admin_notice(){
     37
     38            $currentScreen = get_current_screen();
     39
     40            if(in_array($currentScreen->post_type, $this->get_all_cpt())){
     41                if($currentScreen->base == 'post' && $currentScreen->is_block_editor == 1) {
     42                    if ( !post_type_supports( $currentScreen->post_type, 'custom-fields' ) ) {
     43                        echo '<div class="notice notice-warning is-dismissible"><p>'.__("Please add custom-fields support on your Custom Post Type to work with Gutenberg.").' <a href="https://developer.wordpress.org/reference/functions/add_post_type_support/" target="_blank">add_post_type_support</a></p></div>';
     44                    }
     45                }
     46            }
     47
     48
     49
     50        }
     51
     52
     53        public function add_option_sticky_posts($option_name, $new_value) {
     54            remove_action('added_post_meta', array($this, 'added_post_meta_sticky'), 10, 4);
     55            foreach ($new_value as $item) {
     56                if (!add_post_meta($item, 'sticky_value_cpt', 1, true)) {
     57                    update_post_meta($item, 'sticky_value_cpt', 1);
     58                }
     59            }
     60
     61
     62        }
     63
     64        public function update_option_sticky_posts( $old_value, $new_value ) {
     65            remove_action('updated_post_meta', array($this, 'added_post_meta_sticky'), 10, 4);
     66            $remove = array_diff($old_value, $new_value);
     67
     68            if(count($remove) > 0) {
     69                $remove = array_shift($remove);
     70                if ( ! add_post_meta( $remove, 'sticky_value_cpt', 0, true ) ) {
     71                    update_post_meta ( $remove, 'sticky_value_cpt', 0 );
     72                }
     73            }else {
     74                $add = array_diff($new_value, $old_value);
     75
     76                if(count($add) > 0) {
     77                    $add = array_shift($add);
     78                    if (!add_post_meta($add, 'sticky_value_cpt', 1, true)) {
     79                        update_post_meta($add, 'sticky_value_cpt', 1);
     80                    }
     81
     82                }
     83            }
     84
     85
     86
     87        }
     88
     89
     90        private function update_sticky_option($new_value) {
     91            $option_name = 'sticky_posts';
     92            if ( get_option( $option_name ) !== false ) {
     93                update_option( $option_name, $new_value );
     94            } else {
     95                add_option( $option_name, $new_value );
     96            }
     97        }
     98
     99        function deleted_post_meta_sticky( $deleted_meta_ids, $post_id, $meta_key, $only_delete_these_meta_values )
     100        {
     101            if ( 'sticky_value_cpt' == $meta_key ) {
     102                $stickies = get_option( 'sticky_posts' );
     103
     104                if (($key = array_search($post_id, $stickies)) !== false) {
     105                    unset($stickies[$key]);
     106                    $this->update_sticky_option( $stickies );
     107                }
     108            }
     109        }
     110        function added_post_meta_sticky( $meta_id, $post_id, $meta_key, $meta_value )
     111        {
     112            if ( 'sticky_value_cpt' == $meta_key ) {
     113                $stickies = get_option( 'sticky_posts' );
     114
     115                if($meta_value == 1) {
     116                    if(is_array($stickies) && count($stickies) > 0) {
     117                        $stickies[] = $post_id;
     118                    } else {
     119                        $stickies = array($post_id);
     120                    }
     121
     122
     123                } else {
     124                    if (($key = array_search($post_id, $stickies)) !== false) {
     125                        unset($stickies[$key]);
     126                    }
     127                }
     128
     129                $this->update_sticky_option( $stickies );
     130            }
     131
     132        }
     133        function register_meta_sticky_cpt() {
     134
     135            register_post_meta('', 'sticky_value_cpt', array(
     136                'show_in_rest' => true,
     137                'type' => 'boolean',
     138                'single' => true,
     139                'sanitize_callback' => 'sanitize_text_field',
     140                'auth_callback' => function() {
     141                    return current_user_can('edit_posts');
     142                }
     143            ));
     144
    17145        }
    18146
     
    28156        }
    29157
     158
     159        public function block_enqueue() {
     160            $screen = get_current_screen();
     161
     162
     163            if(!in_array($screen->post_type, $this->get_all_cpt())) return false;
     164
     165            if ( post_type_supports( $screen->post_type, 'custom-fields' ) ) {
     166                wp_enqueue_script(
     167                    'who-sticky-cpt', // Unique handle.
     168                    plugin_dir_url(__DIR__) . 'gut/js/blocks.js',
     169                    array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'wp-plugins', 'wp-edit-post'), // Dependencies, defined above.
     170                    null
     171                );
     172            } else {
     173                wp_enqueue_script(
     174                    'who-sticky-cpt-info', // Unique handle.
     175                    plugin_dir_url(__DIR__) . 'gut/js/info.js',
     176                    array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'wp-plugins', 'wp-edit-post'), // Dependencies, defined above.
     177                    null
     178                );
     179            }
     180        }
     181
    30182        private function add_prefix( &$n ) {
    31183            $n = 'edit-'.$n;
     
    40192        private function get_all_cpt() {
    41193            $args = array(
    42                'public'   => true,
    43                '_builtin' => false
     194                'public'   => true,
     195                '_builtin' => false
    44196            );
    45197            $post_types = get_post_types( $args );
     
    62214            $NoChange      = __( "&mdash; No Change &mdash;" );
    63215
    64 $script = <<<HTML
     216            $script = <<<HTML
    65217<script>
    66218    jQuery(function($) {
     
    80232            global $post, $typenow;
    81233
     234            $currentScreen = get_current_screen();
     235            if($currentScreen->is_block_editor() == 1) return false;
     236
    82237            $post_types = $this->get_all_cpt();
    83238
     
    92247            }
    93248
    94 $script = <<<HTML
     249            $script = <<<HTML
    95250<script>
    96251    jQuery(function($) {
     252        $('.edit-post-post-status .components-panel__row').last().append('<div class="components-panel__row"><div class="components-base-control"><div class="components-base-control__field"><span class="components-checkbox-control__input-container"><input id="inspector-checkbox-control-1" class="components-checkbox-control__input" type="checkbox" value="1"></span><label class="components-checkbox-control__label" for="inspector-checkbox-control-1">Épingler en haut du blog</label></div></div></div>');
    97253        var sticky = "<br/><span id='sticky-span'><input id='sticky' name='sticky' type='checkbox' value='sticky' $checked /> <label for='sticky' class='selectit'>$label</label><br /></span>";
    98254        $('[for=visibility-radio-public]').append(sticky);
     
    119275
    120276endif;
    121 
  • sticky-cpt/trunk/includes/class-sticky-cpt-posts.php

    r1520497 r2263986  
    1515            $sticky     = $wpdb->get_col("SELECT `ID` FROM $wpdb->posts WHERE (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending') AND ( post_type = '$post_type' ) ");
    1616            $all_sticky = get_option( 'sticky_posts' );
    17             $result     = array_intersect( $all_sticky, $sticky );
     17
     18            $result = array();
     19            if(isset($all_sticky) && is_array($all_sticky)) {
     20                $result = array_intersect($all_sticky, $sticky);
     21            }
    1822
    1923            if( count( $result ) == 0 ) return $views;
  • sticky-cpt/trunk/readme.txt

    r2263259 r2263986  
    44Requires at least: 3.5
    55Tested up to: 5.3.2
    6 Stable tag: 1.0.1
     6Stable tag: 2.0.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    99
    10 Add the possibility of "sticky" CPT.
     10Add the possibility of "sticky" CPT. (Gutenberg compatibility)
    1111
    1212== Description ==
  • sticky-cpt/trunk/sticky-cpt.php

    r1690789 r2263986  
    44 * Plugin Name:       Sticky CPT
    55 * Plugin URI:        http://www.samy-kantari.fr/
    6  * Description:       Add the possibility of "sticky" CPT
    7  * Version:           1.0.1
     6 * Description:       Add the possibility of "sticky" CPT (Gutenberg compatibility)
     7 * Version:           2.0.0
    88 * Author:            Kantari Samy
    99 * Author URI:        http://www.samy-kantari.fr/
     
    1818 * DEFINES
    1919 */
    20 define( 'STICKY_CPT'          , '1.0.1' );
     20define( 'STICKY_CPT'          , '2.0.0' );
    2121define( 'STICKY_CPT_FILE'     , __FILE__ );
    2222define( 'STICKY_CPT_PATH'     , realpath( plugin_dir_path( STICKY_CPT_FILE ) ) . '/' );
Note: See TracChangeset for help on using the changeset viewer.