PluginProbe
RSFirewall! / trunk
RSFirewall! vtrunk
rsfirewall / libraries / post.php

post.php in RSFirewall! trunk, at libraries/post.php

197 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package RSFirewall!
4 * @copyright (c) 2018 RSJoomla!
5 * @link https://www.rsjoomla.com
6 * @license GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html
7 */
8
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 class RSFirewall_Post extends RSFirewall_Model
14 {
15 /**
16 * Loads the necessary css/scripts for the specific post model/view, because here we do not use a controller
17 */
18 protected $messages_types = array(
19 'error', 'info', 'success', 'warning'
20 );
21
22 public function load_scripts(){
23 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
24 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles') );
25 }
26
27 /**
28 * Must be declared in case we do not use them in a specific controller so that warnings would not show up
29 */
30 public function enqueue_scripts($pagenow){}
31 public function enqueue_styles($pagenow){}
32
33 /**
34 * Register a custom post type.
35 */
36 public function register()
37 {
38 $this->init();
39 add_action( 'save_post_' . $this->prefix.$this->filename, array($this, 'save'), 10, 2);
40 add_action( 'manage_' . $this->prefix.$this->filename . '_posts_custom_column', array($this, 'column'), 10, 2 );
41
42 add_filter( 'manage_edit-' . $this->prefix.$this->filename . '_columns', array($this, 'columns') );
43 add_filter( 'manage_edit-' . $this->prefix.$this->filename . '_sortable_columns', array($this, 'sortable_columns') );
44
45 // Filters for the bulk actions
46 add_filter( 'bulk_actions-edit-'.$this->prefix.$this->filename, array($this, 'bulk_actions') );
47 add_filter( 'handle_bulk_actions-edit-'.$this->prefix.$this->filename, array($this, 'handle_bulk_actions'), 10, 3 );
48
49 add_action('admin_notices', array($this, 'show_messages'));
50 }
51
52 /**
53 * Initialize a custom post type
54 */
55 public function init()
56 {
57 /* @override this in subclass */
58 }
59
60 /**
61 * Save post.
62 *
63 * @param $post_id
64 * @param $post
65 *
66 * @return int|void
67 */
68 public function save($post_id, $post)
69 {
70 if ($post->post_type == 'revision')
71 {
72 return;
73 }
74
75 $data = isset($_POST['data']) ? $_POST['data'] : array();
76
77 foreach ($this->form->section->field as $field)
78 {
79 $name = (string) $field->attributes()->name;
80 $value = isset($data[$name]) ? sanitize_text_field($data[$name]) : '';
81 if ( get_post_meta( $post->ID, $name, false ) ) {
82 update_post_meta( $post->ID, $name, $value );
83 } else {
84 add_post_meta( $post->ID, $name, $value );
85 }
86 }
87 }
88
89 /**
90 * Display custom column info.
91 *
92 * @param $column
93 * @param $post_id
94 */
95 public function column($column, $post_id)
96 {
97 /* @override this in subclass */
98 }
99
100 /**
101 * Display columns.
102 *
103 * @param $columns
104 *
105 * @return array
106 */
107 public function columns($columns)
108 {
109 return $columns;
110 }
111
112 /**
113 * Make columns sortable.
114 *
115 * @param $columns
116 *
117 * @return mixed
118 */
119 public function sortable_columns($columns)
120 {
121 return $columns;
122 }
123
124 /**
125 * Display bulk actions.
126 *
127 * @param $actions
128 *
129 * @return array
130 */
131 public function bulk_actions($actions)
132 {
133 return $actions;
134 }
135
136 /**
137 * Handle the bulk actions
138 *
139 * @param $redirect_to - where to redirect after the action has been done
140 * @param $action - the action that is called upon
141 * @param $post_ids - the array containing the selected ids
142 *
143 * @return string - the proper redirect
144 */
145 public function handle_bulk_actions($redirect_to, $action, $post_ids) {
146 return $redirect_to;
147 }
148
149 public function show_messages()
150 {
151 foreach ($this->messages_types as $type) {
152 if ($errors = $this->get_messages($type)) {
153 ?>
154 <div class="notice notice-<?php echo $type; ?> is-dismissible">
155 <?php foreach ($errors as $error) { ?>
156 <p><?php echo esc_html($error); ?></p>
157 <?php } ?>
158 <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php echo __('Dismiss this notice.', 'rsfirewall');?></span></button>
159 </div>
160 <?php
161 }
162
163 $this->clear_messages($type);
164 }
165 }
166
167 protected function clear_messages($type = 'error')
168 {
169 delete_transient($this->get_transient_name($type));
170 }
171
172 public function get_messages($type = 'error')
173 {
174 return get_transient($this->get_transient_name($type));
175 }
176
177 protected function set_message($message, $type = 'error') {
178 $messages = $this->get_messages($type);
179 // if no message is set this is a boolean value (false)
180 if (is_bool($messages))
181 {
182 $messages = array();
183 }
184
185 $messages[] = $message;
186
187 // Remove empty values
188 $messages = array_filter($messages);
189
190 set_transient($this->get_transient_name($type), $messages, 15);
191 }
192
193 protected function get_transient_name($type = 'error')
194 {
195 return 'save_'.$this->prefix.$this->filename.'_'.$type.'_' . get_current_user_id();
196 }
197 }