PluginProbe
wpForo Forum / 3.0.7
wpForo Forum v3.0.7
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / modules / revisions / Revisions.php

Revisions.php in wpForo Forum 3.0.7, at modules/revisions/Revisions.php

290 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\modules\revisions;
4
5 use stdClass;
6 use wpforo\modules\revisions\classes\Actions;
7 use wpforo\modules\revisions\classes\Template;
8
9 // Exit if accessed directly
10 if( ! defined( 'ABSPATH' ) ) exit;
11
12 class Revisions {
13 /* @var Template */
14 public $Template;
15 /* @var Actions */
16 public $Actions;
17 public $revision;
18 private $default;
19
20 public function __construct() {
21 $this->init_classes();
22 $this->init();
23 }
24
25 private function init_classes() {
26 $this->Template = new Template();
27 $this->Actions = new Actions();
28 }
29
30 private function init() {
31 $this->init_defaults();
32 $this->revision = $this->default->revision;
33 }
34
35 private function init_defaults() {
36 $this->default = new stdClass();
37 $this->default->revision = [
38 'revisionid' => 0,
39 'userid' => 0,
40 'textareaid' => '',
41 'postid' => 0,
42 'body' => '',
43 'created' => 0,
44 'version' => 0,
45 'email' => '',
46 'url' => '',
47 ];
48 $this->default->revision_format = [
49 'revisionid' => '%d',
50 'userid' => '%d',
51 'textareaid' => '%s',
52 'postid' => '%d',
53 'body' => '%s',
54 'created' => '%d',
55 'version' => '%d',
56 'email' => '%s',
57 'url' => '%s',
58 ];
59 $this->default->sql_select_args = [
60 'include' => [],
61 'exclude' => [],
62 'userids_include' => [],
63 'userids_exclude' => [],
64 'textareaids_include' => [],
65 'textareaids_exclude' => [],
66 'postids_include' => [],
67 'postids_exclude' => [],
68 'urls_include' => [],
69 'urls_exclude' => [],
70 'emails_include' => [],
71 'emails_exclude' => [],
72 'orderby' => 'revisionid',
73 'order' => 'DESC',
74 'offset' => null,
75 'row_count' => null,
76 ];
77 }
78
79 public function get_current_url_query_vars_str() {
80 $url_query_vars_str = wpforo_get_url_query_vars_str();
81 $url_query_vars_str = preg_replace( '#/?\?.*$#isu', '', $url_query_vars_str );
82
83 $wpf_url_parse = array_filter( explode( '/', trim( (string) $url_query_vars_str, '/' ) ) );
84 $wpf_url_parse = array_reverse( $wpf_url_parse );
85 if( in_array( wpforo_settings_get_slug( 'paged' ), $wpf_url_parse ) ) {
86 foreach( $wpf_url_parse as $key => $value ) {
87 unset( $wpf_url_parse[ $key ] );
88 if( $value === wpforo_settings_get_slug( 'paged' ) ) break;
89 }
90 $wpf_url_parse = array_values( $wpf_url_parse );
91 $wpf_url_parse = array_reverse( $wpf_url_parse );
92 $url_query_vars_str = implode( '/', $wpf_url_parse );
93 }
94
95 if( ! $url_query_vars_str ) $url_query_vars_str = 'wpforo_home_url';
96
97 return $url_query_vars_str;
98 }
99
100 public function parse_revision( $revision ) {
101 $revision = array_merge( $this->default->revision, (array) $revision );
102 if( $revision['body'] ) {
103 $revision['body'] = preg_replace( '#</pre>[\r\n\t\s\0]*<pre>#iu', "\r\n", (string) $revision['body'] );
104 $revision['body'] = wpforo_kses( trim( (string) $revision['body'] ) );
105 $revision['body'] = stripslashes( (string) $revision['body'] );
106 }
107
108 return $revision;
109 }
110
111 private function parse_args( $args ) {
112 $args = wpforo_parse_args( $args, $this->default->sql_select_args );
113
114 $args['include'] = wpforo_parse_args( $args['include'] );
115 $args['exclude'] = wpforo_parse_args( $args['exclude'] );
116
117 $args['userids_include'] = wpforo_parse_args( $args['userids_include'] );
118 $args['userids_exclude'] = wpforo_parse_args( $args['userids_exclude'] );
119
120 $args['textareaids_include'] = wpforo_parse_args( $args['textareaids_include'] );
121 $args['textareaids_exclude'] = wpforo_parse_args( $args['textareaids_exclude'] );
122
123 $args['postids_include'] = wpforo_parse_args( $args['postids_include'] );
124 $args['postids_exclude'] = wpforo_parse_args( $args['postids_exclude'] );
125
126 $args['urls_include'] = wpforo_parse_args( $args['urls_include'] );
127 $args['urls_exclude'] = wpforo_parse_args( $args['urls_exclude'] );
128
129 $args['emails_include'] = wpforo_parse_args( $args['emails_include'] );
130 $args['emails_exclude'] = wpforo_parse_args( $args['emails_exclude'] );
131
132 return $args;
133 }
134
135 public function build_sql_where( $args ) {
136 $where = '';
137 $args = $this->parse_args( $args );
138
139 $wheres = [];
140 if( ! empty( $args['include'] ) ) {
141 $wheres[] = "`revisionid` IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['include'] ) ) . ")";
142 }
143 if( ! empty( $args['exclude'] ) ) {
144 $wheres[] = "`revisionid` NOT IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['exclude'] ) ) . ")";
145 }
146
147 if( ! empty( $args['userids_include'] ) ) {
148 $wheres[] = "`userid` IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['userids_include'] ) ) . ")";
149 }
150 if( ! empty( $args['userids_exclude'] ) ) {
151 $wheres[] = "`userid` NOT IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['userids_exclude'] ) ) . ")";
152 }
153
154 if( ! empty( $args['textareaids_include'] ) ) {
155 $wheres[] = "`textareaid` IN('" . implode( "','", array_map( 'esc_sql', $args['textareaids_include'] ) ) . "')";
156 }
157 if( ! empty( $args['textareaids_exclude'] ) ) {
158 $wheres[] = "`textareaid` IN('" . implode( "','", array_map( 'esc_sql', $args['textareaids_exclude'] ) ) . "')";
159 }
160
161 if( ! empty( $args['postids_include'] ) ) {
162 $wheres[] = "`postid` IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['postids_include'] ) ) . ")";
163 }
164 if( ! empty( $args['postids_exclude'] ) ) {
165 $wheres[] = "`postid` NOT IN(" . implode( ',', array_map( 'wpforo_bigintval', $args['postids_exclude'] ) ) . ")";
166 }
167
168 if( ! empty( $args['urls_include'] ) ) {
169 $wheres[] = "`url` IN('" . implode( "','", array_map( 'esc_sql', $args['urls_include'] ) ) . "')";
170 }
171 if( ! empty( $args['urls_exclude'] ) ) {
172 $wheres[] = "`url` IN('" . implode( "','", array_map( 'esc_sql', $args['urls_exclude'] ) ) . "')";
173 }
174
175 if( ! empty( $args['emails_include'] ) ) {
176 $wheres[] = "`email` IN('" . implode( "','", array_map( 'esc_sql', $args['emails_include'] ) ) . "')";
177 }
178 if( ! empty( $args['emails_exclude'] ) ) {
179 $wheres[] = "`email` IN('" . implode( "','", array_map( 'esc_sql', $args['emails_exclude'] ) ) . "')";
180 }
181
182 if( $wheres ) {
183 $where = " WHERE " . implode( " AND ", $wheres );
184 }
185
186 return $where;
187 }
188
189 private function build_sql_select( $args ) {
190 $args = $this->parse_args( $args );
191 $sql = "SELECT * FROM " . WPF()->tables->post_revisions;
192 $sql .= $this->build_sql_where( $args );
193 $allowed_orderby = array_keys( $this->default->revision_format );
194 $orderby = in_array( $args['orderby'], $allowed_orderby, true ) ? $args['orderby'] : 'revisionid';
195 $order = strtoupper( $args['order'] ) === 'ASC' ? 'ASC' : 'DESC';
196 $sql .= " ORDER BY `" . $orderby . "` " . $order;
197 if( $args['row_count'] ) $sql .= " LIMIT " . wpforo_bigintval( $args['offset'] ) . "," . wpforo_bigintval( $args['row_count'] );
198
199 return $sql;
200 }
201
202 public function add( $data ) {
203 if( empty( $data ) ) return false;
204 $revision = $this->parse_revision( $data );
205 unset( $revision['revisionid'] );
206
207 if( ! $revision['created'] ) $revision['created'] = time();
208 if( ! $revision['url'] ) $revision['url'] = $this->get_current_url_query_vars_str();
209 if( ! $revision['userid'] ) $revision['userid'] = WPF()->current_userid;
210 if( ! $revision['email'] ) $revision['email'] = WPF()->current_user_email;
211 if( ! $revision['textareaid'] || ! $revision['url'] || ! $revision['body'] || ! ( $revision['userid'] || $revision['email'] ) ) return false;
212
213 $revision = wpforo_array_ordered_intersect_key( $revision, $this->default->revision_format );
214 if( WPF()->db->insert(
215 WPF()->tables->post_revisions,
216 $revision,
217 wpforo_array_ordered_intersect_key( $this->default->revision_format, $revision )
218 ) ) {
219 return WPF()->db->insert_id;
220 }
221
222 return false;
223 }
224
225 public function edit( $data, $where ) {
226 if( empty( $data ) || empty( $where ) ) return false;
227 if( is_numeric( $where ) ) $where = [ 'revisionid' => $where ];
228 $data = (array) $data;
229 $where = (array) $where;
230
231 $data = wpforo_array_ordered_intersect_key( $data, $this->default->revision_format );
232 $where = wpforo_array_ordered_intersect_key( $where, $this->default->revision_format );
233 if( false !== WPF()->db->update(
234 WPF()->tables->post_revisions,
235 $data,
236 $where,
237 wpforo_array_ordered_intersect_key( $this->default->revision_format, $data ),
238 wpforo_array_ordered_intersect_key( $this->default->revision_format, $where )
239 ) ) {
240 return true;
241 }
242
243 return false;
244 }
245
246 public function delete( $where ) {
247 if( empty( $where ) ) return false;
248 if( is_numeric( $where ) ) $where = [ 'revisionid' => $where ];
249 $where = (array) $where;
250
251 $where = wpforo_array_ordered_intersect_key( $where, $this->default->revision_format );
252 if( false !== WPF()->db->delete(
253 WPF()->tables->post_revisions,
254 $where,
255 wpforo_array_ordered_intersect_key( $this->default->revision_format, $where )
256 ) ) {
257 return true;
258 }
259
260 return false;
261 }
262
263 public function get_revision( $args ) {
264 if( empty( $args ) ) return false;
265
266 $revision = (array) WPF()->db->get_row( $this->build_sql_select( $args ), ARRAY_A );
267 if( $revision ) $revision = $this->parse_revision( $revision );
268
269 return $revision;
270 }
271
272 public function get_revisions( $args ) {
273 if( empty( $args ) ) return false;
274
275 return array_map( [ $this, 'parse_revision' ], WPF()->db->get_results( $this->build_sql_select( $args ), ARRAY_A ) );
276 }
277
278 /**
279 * @param array $args
280 *
281 * @return int
282 */
283 public function get_count( $args ) {
284 $sql = "SELECT SQL_NO_CACHE COUNT(*) FROM " . WPF()->tables->post_revisions;
285 $sql .= $this->build_sql_where( $args );
286
287 return intval( WPF()->db->get_var( $sql ) );
288 }
289 }
290