Changeset 2045814
- Timestamp:
- 03/07/2019 09:21:20 AM (7 years ago)
- Location:
- wp-post-expires/trunk
- Files:
-
- 2 edited
-
assets/js/plugin-scripts.js (modified) (1 diff)
-
wp-post-expires.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-post-expires/trunk/assets/js/plugin-scripts.js
r2045296 r2045814 29 29 }); 30 30 31 /* FUTURE 31 32 ( function( wp ) { 32 33 let el = wp.element.createElement; 34 function Component() { 35 let meta = wp.data.select('core/editor').getEditedPostAttribute('meta'); 36 if(meta['xn-wppe-expiration'].length > 0) { 37 return el(wp.element.Fragment, {}, 38 el(wp.editPost.PluginPostStatusInfo, {}, 39 wp.i18n.__('Expires', 'wp-post-expires') + ' ' + meta['xn-wppe-expiration'], 40 ) 41 ); 42 } 33 43 34 function Component() { 35 return el(wp.element.Fragment, 36 {}, 37 el(wp.editPost.PluginPostStatusInfo, 38 {}, 39 'Истекает: 21.07.2019 08:00', 40 ) 41 ); 44 return ''; 42 45 } 43 46 44 wp.plugins.registerPlugin(' plugin-name', {47 wp.plugins.registerPlugin('xn-wppe', { 45 48 render: Component 46 49 }); 47 50 48 51 } )( window.wp ); 52 */ -
wp-post-expires/trunk/wp-post-expires.php
r2045313 r2045814 28 28 29 29 class XNPostExpires { 30 private $plugin_version = '1.2.1'; 31 private $url_assets; 32 public $settings = []; 33 34 public static function init() { 35 return new self; 36 } 37 38 public function __construct() { 39 load_plugin_textdomain('wp-post-expires', false, dirname(plugin_basename( __FILE__ ) ).'/languages'); 40 41 $this->settings = $this->getSettings(); 42 $this->url_assets = plugin_dir_url( __FILE__ ).'assets/'; 43 44 add_action('the_post', [$this, 'expiredPost']); 45 46 if(current_user_can('manage_options')) { 47 add_action('admin_init', [$this, 'registerSettings']); 48 } 49 50 if(current_user_can('edit_posts')) { 51 add_action('enqueue_block_editor_assets', [$this, 'loadScripts']); 30 private $plugin_version = '1.2.1'; 31 private $url_assets; 32 public $settings = []; 33 34 public static function init() { 35 return new self; 36 } 37 38 public function __construct() { 39 40 load_plugin_textdomain('wp-post-expires', false, dirname(plugin_basename( __FILE__ )) . '/languages'); 41 42 $this->settings = $this->getSettings(); 43 $this->url_assets = plugin_dir_url( __FILE__ ).'assets/'; 44 45 add_action('the_post', [$this, 'expiredPost']); 46 47 if(current_user_can('manage_options')) { 48 add_action('admin_init', [$this, 'registerSettings']); 49 } 50 51 if(current_user_can('edit_posts')) { 52 add_action('enqueue_block_editor_assets', [$this, 'loadScripts']); 52 53 add_action('add_meta_boxes', [$this, 'addMetaBox']); 53 add_action('save_post', [$this, 'saveBoxFields']); 54 } 55 } 56 57 private function getSettings() { 58 $settings_load = get_option('xn_wppe_settings'); 59 60 if(empty($settings_load) || !is_array($settings_load)) { 61 $settings_load = []; 62 } 63 64 if(!isset($settings_load['post_types'])) { 65 $settings_load['post_types']['post'] = 1; 66 $settings_load['post_types']['page'] = 0; 67 } 68 69 if(!isset($settings_load['action'])) { 70 $settings_load['action'] = 'add_prefix'; 71 } 72 73 if(!isset($settings_load['prefix'])) { 74 $settings_load['prefix'] = __('Expired:', 'wp-post-expires'); 75 }else{ 76 $settings_load['prefix'] = esc_attr($settings_load['prefix']); 77 } 78 79 return $settings_load; 80 } 81 82 public function loadScripts() { 54 add_action('save_post', [$this, 'saveBoxFields']); 55 } 56 57 register_meta('post', 'xn-wppe-expiration', ['show_in_rest' => true]); 58 register_meta('post', 'xn-wppe-expiration-action', ['show_in_rest' => true]); 59 register_meta('post', 'xn-wppe-expiration-prefix', ['show_in_rest' => true]); 60 } 61 62 private function getSettings() { 63 $settings_load = get_option('xn_wppe_settings'); 64 65 if(empty($settings_load) || !is_array($settings_load)) { 66 $settings_load = []; 67 } 68 69 if(!isset($settings_load['post_types'])) { 70 $settings_load['post_types']['post'] = 1; 71 $settings_load['post_types']['page'] = 0; 72 } 73 74 if(!isset($settings_load['action'])) { 75 $settings_load['action'] = 'add_prefix'; 76 } 77 78 if(!isset($settings_load['prefix'])) { 79 $settings_load['prefix'] = __('Expired:', 'wp-post-expires'); 80 }else{ 81 $settings_load['prefix'] = esc_attr($settings_load['prefix']); 82 } 83 84 return $settings_load; 85 } 86 87 public function loadScripts() { 83 88 wp_enqueue_script('jquery-ui-core', ['jquery']); 84 89 wp_enqueue_script('jquery-ui-datepicker', ['jquery-ui-core', 'jquery']); 85 90 wp_enqueue_style('jquery-ui', 'https://cdn.jsdelivr.net/npm/jquery-ui-dist@1.12.1/jquery-ui.min.css', [], $this->plugin_version); 86 91 wp_enqueue_style('xn-dtpicker-skin', $this->url_assets.'css/latoja.datepicker.css', ['jquery-ui'], $this->plugin_version); 87 wp_enqueue_script('xn-plugin-js', $this->url_assets.'js/plugin-scripts.js', [92 wp_enqueue_script('xn-plugin-js', $this->url_assets.'js/plugin-scripts.js', [ 88 93 'jquery-ui-datepicker', 89 'wp-plugins',90 94 'wp-edit-post', 91 95 'wp-element', 92 'wp-components' 96 'wp-components', 97 'wp-i18n' 93 98 ], $this->plugin_version); 94 } 99 100 // TODO: i18n js 101 //wp_set_script_translations('xn-plugin-js', 'xn-wppe-expiration', dirname(plugin_basename( __FILE__ )) . '/languages'); 102 } 95 103 96 104 public function addMetaBox() { … … 107 115 } 108 116 109 public function addBoxFields($post) {110 if(!empty($post->ID)) {111 $expires = get_post_meta($post->ID, 'xn-wppe-expiration', true);112 $expires_select = get_post_meta($post->ID, 'xn-wppe-expiration-action', true);113 $expires_prefix = get_post_meta($post->ID, 'xn-wppe-expiration-prefix', false);114 } else {117 public function addBoxFields($post) { 118 if(!empty($post->ID)) { 119 $expires = get_post_meta($post->ID, 'xn-wppe-expiration', true); 120 $expires_select = get_post_meta($post->ID, 'xn-wppe-expiration-action', true); 121 $expires_prefix = get_post_meta($post->ID, 'xn-wppe-expiration-prefix', false); 122 } else { 115 123 $expires = ''; 116 124 $expires_select = ''; … … 118 126 } 119 127 120 //$label = !empty($expires)? date_i18n('M j, Y @ G:i', strtotime($expires)) : __('never', 'wp-post-expires');121 $date = !empty($expires)? date_i18n('Y-m-d H:i', strtotime($expires)) : '';122 $select = !empty($expires_select)? $expires_select : $this->settings['action'];123 //Allow empty value124 $prefix = isset($expires_prefix[0])? esc_attr($expires_prefix[0]) : $this->settings['prefix'];125 ?>128 //$label = !empty($expires)? date_i18n('M j, Y @ G:i', strtotime($expires)) : __('never', 'wp-post-expires'); 129 $date = !empty($expires)? date_i18n('Y-m-d H:i', strtotime($expires)) : ''; 130 $select = !empty($expires_select)? $expires_select : $this->settings['action']; 131 //Allow empty value 132 $prefix = isset($expires_prefix[0])? esc_attr($expires_prefix[0]) : $this->settings['prefix']; 133 ?> 126 134 127 135 <div class="components-panel__row"> … … 147 155 </div> 148 156 </div> 149 <?php150 }151 152 public function saveBoxFields($post_id = 0) {153 154 if( defined('DOING_AUTOSAVE') || defined('DOING_AJAX') || wp_is_post_revision($post_id) ) {155 return false;156 }157 158 $expiration = !empty($_POST['xn-wppe-expiration'])? sanitize_text_field($_POST['xn-wppe-expiration']) : false;159 $action_type = !empty($_POST['xn-wppe-expiration-action'])? sanitize_text_field($_POST['xn-wppe-expiration-action']) : false;160 $add_prefix = isset($_POST['xn-wppe-expiration-prefix'])? sanitize_text_field($_POST['xn-wppe-expiration-prefix']) : false;161 162 if($expiration !== false && $action_type !== false) {163 update_post_meta($post_id, 'xn-wppe-expiration', $expiration);164 update_post_meta($post_id, 'xn-wppe-expiration-action', $action_type);165 if($add_prefix !== false) {166 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $add_prefix);167 }168 }else{169 delete_post_meta($post_id, 'xn-wppe-expiration');170 delete_post_meta($post_id, 'xn-wppe-expiration-action');171 delete_post_meta($post_id, 'xn-wppe-expiration-prefix');172 }173 }174 175 public function registerSettings() {176 177 register_setting('reading', 'xn_wppe_settings');178 179 add_settings_section("xn_wppe_section", __('Settings posts expires', 'wp-post-expires'), null, 'reading');180 181 add_settings_field('xn_wppe_settings_posttype', __('Supported post types', 'wp-post-expires'), [$this, 'settingsFieldPosttype'], 'reading', "xn_wppe_section");182 add_settings_field('xn_wppe_settings_action', __('Action by default', 'wp-post-expires'), [$this, 'settingsFieldAction'], 'reading', "xn_wppe_section");183 add_settings_field('xn_wppe_settings_prefix', __('Default Expired Item Prefix', 'wp-post-expires'), [$this, 'settingsFieldPrefix'], 'reading', "xn_wppe_section");184 }185 186 public function settingsFieldPosttype() {187 $all_post_types = get_post_types(false, 'objects');188 unset($all_post_types['nav_menu_item']);189 unset($all_post_types['revision']);190 unset($all_post_types['attachment']);191 192 foreach($all_post_types as $post_type => $post_type_obj) {193 echo '<label>';194 echo '<input type="checkbox" name="xn_wppe_settings[post_types]['.$post_type.']" value="1"'.(isset($this->settings['post_types'][$post_type])?' checked':'').'>';195 echo $post_type_obj->labels->name;196 echo '</label> ';197 }198 }199 200 public function settingsFieldAction() {201 echo '<select name="xn_wppe_settings[action]" id="xn_wppe_settings_action">';202 echo '<option '.($this->settings['action']=='add_prefix'?'selected':'').' value="add_prefix">'.__('Add Prefix', 'wp-post-expires').'</option>';203 echo '<option '.($this->settings['action']=='to_drafts'?'selected':'').' value="to_drafts">'.__('Move to drafts', 'wp-post-expires').'</option>';204 echo '<option '.($this->settings['action']=='to_trash'?'selected':'').' value="to_trash">'.__('Move to trash', 'wp-post-expires').'</option>';205 echo '</select>';206 }207 208 public function settingsFieldPrefix() {209 echo '<input id="xn_wppe_settings_prefix" type="text" name="xn_wppe_settings[prefix]" value="'.$this->settings['prefix'].'" class="regular-text">';210 echo '<p class="description">'.__('Enter the text you would like prepended to expired items.', 'wp-post-expires').'</p>';211 }212 213 public function textTitleFilter($title = '', $post_id = 0) {214 $expires_prefix = get_post_meta($post_id, 'xn-wppe-expiration-prefix', true);215 $prefix = !empty($expires_prefix)? esc_attr($expires_prefix).' ' : '';216 217 return $prefix.$title;218 }219 220 public function cssClassFilter($classes) {221 $classes[] = 'post-expired';222 return $classes;223 }224 225 public function expiredPost($post) {226 $post_id = $post->ID;227 228 if(self::isExpired($post_id)) {229 230 $expires_action = get_post_meta($post_id, 'xn-wppe-expiration-action', true);231 $action = !empty($expires_action)? $expires_action : $this->settings['action'];232 233 switch($action) {234 case 'add_prefix':235 add_filter('the_title', [$this, 'textTitleFilter'], 10, 2);236 add_filter('post_class', [$this, 'cssClassFilter']);237 break;238 case 'to_drafts':239 remove_action('save_post', [$this, 'saveBoxFields']);240 wp_update_post(['ID' => $post_id, 'post_status' => 'draft']);241 update_post_meta($post_id, 'xn-wppe-expiration-action', 'add_prefix');242 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $this->settings['prefix']);243 add_action('save_post', [$this, 'saveBoxFields']);244 break;245 case 'to_trash':246 remove_action('save_post', [$this, 'saveBoxFields']);247 $del_post = wp_trash_post($post_id);248 //check post to trash, or deleted249 if($del_post !== false) {250 update_post_meta($post_id, 'xn-wppe-expiration-action', 'add_prefix');251 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $this->settings['prefix']);252 }253 add_action('save_post', [$this, 'saveBoxFields']);254 break;255 }256 }257 }157 <?php 158 } 159 160 public function saveBoxFields($post_id = 0) { 161 162 if( defined('DOING_AUTOSAVE') || defined('DOING_AJAX') || wp_is_post_revision($post_id) ) { 163 return false; 164 } 165 166 $expiration = !empty($_POST['xn-wppe-expiration'])? sanitize_text_field($_POST['xn-wppe-expiration']) : false; 167 $action_type = !empty($_POST['xn-wppe-expiration-action'])? sanitize_text_field($_POST['xn-wppe-expiration-action']) : false; 168 $add_prefix = isset($_POST['xn-wppe-expiration-prefix'])? sanitize_text_field($_POST['xn-wppe-expiration-prefix']) : false; 169 170 if($expiration !== false && $action_type !== false) { 171 update_post_meta($post_id, 'xn-wppe-expiration', $expiration); 172 update_post_meta($post_id, 'xn-wppe-expiration-action', $action_type); 173 if($add_prefix !== false) { 174 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $add_prefix); 175 } 176 } else { 177 delete_post_meta($post_id, 'xn-wppe-expiration'); 178 delete_post_meta($post_id, 'xn-wppe-expiration-action'); 179 delete_post_meta($post_id, 'xn-wppe-expiration-prefix'); 180 } 181 } 182 183 public function registerSettings() { 184 185 register_setting('reading', 'xn_wppe_settings'); 186 187 add_settings_section("xn_wppe_section", __('Settings posts expires', 'wp-post-expires'), null, 'reading'); 188 189 add_settings_field('xn_wppe_settings_posttype', __('Supported post types', 'wp-post-expires'), [$this, 'settingsFieldPosttype'], 'reading', "xn_wppe_section"); 190 add_settings_field('xn_wppe_settings_action', __('Action by default', 'wp-post-expires'), [$this, 'settingsFieldAction'], 'reading', "xn_wppe_section"); 191 add_settings_field('xn_wppe_settings_prefix', __('Default Expired Item Prefix', 'wp-post-expires'), [$this, 'settingsFieldPrefix'], 'reading', "xn_wppe_section"); 192 } 193 194 public function settingsFieldPosttype() { 195 $all_post_types = get_post_types(false, 'objects'); 196 unset($all_post_types['nav_menu_item']); 197 unset($all_post_types['revision']); 198 unset($all_post_types['attachment']); 199 200 foreach($all_post_types as $post_type => $post_type_obj) { 201 echo '<label>'; 202 echo '<input type="checkbox" name="xn_wppe_settings[post_types]['.$post_type.']" value="1"'.(isset($this->settings['post_types'][$post_type])?' checked':'').'>'; 203 echo $post_type_obj->labels->name; 204 echo '</label> '; 205 } 206 } 207 208 public function settingsFieldAction() { 209 echo '<select name="xn_wppe_settings[action]" id="xn_wppe_settings_action">'; 210 echo '<option '.($this->settings['action']=='add_prefix'?'selected':'').' value="add_prefix">'.__('Add Prefix', 'wp-post-expires').'</option>'; 211 echo '<option '.($this->settings['action']=='to_drafts'?'selected':'').' value="to_drafts">'.__('Move to drafts', 'wp-post-expires').'</option>'; 212 echo '<option '.($this->settings['action']=='to_trash'?'selected':'').' value="to_trash">'.__('Move to trash', 'wp-post-expires').'</option>'; 213 echo '</select>'; 214 } 215 216 public function settingsFieldPrefix() { 217 echo '<input id="xn_wppe_settings_prefix" type="text" name="xn_wppe_settings[prefix]" value="'.$this->settings['prefix'].'" class="regular-text">'; 218 echo '<p class="description">'.__('Enter the text you would like prepended to expired items.', 'wp-post-expires').'</p>'; 219 } 220 221 public function textTitleFilter($title = '', $post_id = 0) { 222 $expires_prefix = get_post_meta($post_id, 'xn-wppe-expiration-prefix', true); 223 $prefix = !empty($expires_prefix)? esc_attr($expires_prefix).' ' : ''; 224 225 return $prefix.$title; 226 } 227 228 public function cssClassFilter($classes) { 229 $classes[] = 'post-expired'; 230 return $classes; 231 } 232 233 public function expiredPost($post) { 234 $post_id = $post->ID; 235 236 if(self::isExpired($post_id)) { 237 238 $expires_action = get_post_meta($post_id, 'xn-wppe-expiration-action', true); 239 $action = !empty($expires_action)? $expires_action : $this->settings['action']; 240 241 switch($action) { 242 case 'add_prefix': 243 add_filter('the_title', [$this, 'textTitleFilter'], 10, 2); 244 add_filter('post_class', [$this, 'cssClassFilter']); 245 break; 246 case 'to_drafts': 247 remove_action('save_post', [$this, 'saveBoxFields']); 248 wp_update_post(['ID' => $post_id, 'post_status' => 'draft']); 249 update_post_meta($post_id, 'xn-wppe-expiration-action', 'add_prefix'); 250 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $this->settings['prefix']); 251 add_action('save_post', [$this, 'saveBoxFields']); 252 break; 253 case 'to_trash': 254 remove_action('save_post', [$this, 'saveBoxFields']); 255 $del_post = wp_trash_post($post_id); 256 //check post to trash, or deleted 257 if($del_post !== false) { 258 update_post_meta($post_id, 'xn-wppe-expiration-action', 'add_prefix'); 259 update_post_meta($post_id, 'xn-wppe-expiration-prefix', $this->settings['prefix']); 260 } 261 add_action('save_post', [$this, 'saveBoxFields']); 262 break; 263 } 264 } 265 } 258 266 259 267 public static function dateExpiration($post_id = 0, $format = false) {
Note: See TracChangeset
for help on using the changeset viewer.