Changeset 2263986
- Timestamp:
- 03/19/2020 03:54:16 PM (6 years ago)
- Location:
- sticky-cpt
- Files:
-
- 14 added
- 4 edited
-
tags/2.0.0 (added)
-
tags/2.0.0/gut (added)
-
tags/2.0.0/gut/js (added)
-
tags/2.0.0/gut/js/blocks.js (added)
-
tags/2.0.0/gut/js/info.js (added)
-
tags/2.0.0/includes (added)
-
tags/2.0.0/includes/class-sticky-cpt-loader.php (added)
-
tags/2.0.0/includes/class-sticky-cpt-posts.php (added)
-
tags/2.0.0/readme.txt (added)
-
tags/2.0.0/sticky-cpt.php (added)
-
trunk/gut (added)
-
trunk/gut/js (added)
-
trunk/gut/js/blocks.js (added)
-
trunk/gut/js/info.js (added)
-
trunk/includes/class-sticky-cpt-loader.php (modified) (7 diffs)
-
trunk/includes/class-sticky-cpt-posts.php (modified) (1 diff)
-
trunk/readme.txt (modified) (1 diff)
-
trunk/sticky-cpt.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sticky-cpt/trunk/includes/class-sticky-cpt-loader.php
r1520497 r2263986 11 11 12 12 public function __construct() { 13 13 14 add_action( 'admin_init', array( $this, 'init' ) ); 15 14 16 add_action( 'admin_footer-post.php', array( $this , 'add_sticky' ) ); 15 17 add_action( 'admin_footer-post-new.php', array( $this , 'add_sticky' ) ); 16 18 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 17 145 } 18 146 … … 28 156 } 29 157 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 30 182 private function add_prefix( &$n ) { 31 183 $n = 'edit-'.$n; … … 40 192 private function get_all_cpt() { 41 193 $args = array( 42 'public' => true,43 '_builtin' => false194 'public' => true, 195 '_builtin' => false 44 196 ); 45 197 $post_types = get_post_types( $args ); … … 62 214 $NoChange = __( "— No Change —" ); 63 215 64 $script = <<<HTML216 $script = <<<HTML 65 217 <script> 66 218 jQuery(function($) { … … 80 232 global $post, $typenow; 81 233 234 $currentScreen = get_current_screen(); 235 if($currentScreen->is_block_editor() == 1) return false; 236 82 237 $post_types = $this->get_all_cpt(); 83 238 … … 92 247 } 93 248 94 $script = <<<HTML249 $script = <<<HTML 95 250 <script> 96 251 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>'); 97 253 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>"; 98 254 $('[for=visibility-radio-public]').append(sticky); … … 119 275 120 276 endif; 121 -
sticky-cpt/trunk/includes/class-sticky-cpt-posts.php
r1520497 r2263986 15 15 $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' ) "); 16 16 $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 } 18 22 19 23 if( count( $result ) == 0 ) return $views; -
sticky-cpt/trunk/readme.txt
r2263259 r2263986 4 4 Requires at least: 3.5 5 5 Tested up to: 5.3.2 6 Stable tag: 1.0.16 Stable tag: 2.0.0 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 9 10 Add the possibility of "sticky" CPT. 10 Add the possibility of "sticky" CPT. (Gutenberg compatibility) 11 11 12 12 == Description == -
sticky-cpt/trunk/sticky-cpt.php
r1690789 r2263986 4 4 * Plugin Name: Sticky CPT 5 5 * Plugin URI: http://www.samy-kantari.fr/ 6 * Description: Add the possibility of "sticky" CPT 7 * Version: 1.0.16 * Description: Add the possibility of "sticky" CPT (Gutenberg compatibility) 7 * Version: 2.0.0 8 8 * Author: Kantari Samy 9 9 * Author URI: http://www.samy-kantari.fr/ … … 18 18 * DEFINES 19 19 */ 20 define( 'STICKY_CPT' , ' 1.0.1' );20 define( 'STICKY_CPT' , '2.0.0' ); 21 21 define( 'STICKY_CPT_FILE' , __FILE__ ); 22 22 define( 'STICKY_CPT_PATH' , realpath( plugin_dir_path( STICKY_CPT_FILE ) ) . '/' );
Note: See TracChangeset
for help on using the changeset viewer.