1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | $lsx_projects_scporder = new LSX_Projects_SCPO_Engine(); |
||
4 | |||
5 | /** |
||
6 | * SCPO Engine |
||
7 | * |
||
8 | * @package LSX Projects |
||
9 | * @author LightSpeed |
||
10 | * @license GPL3 |
||
11 | * @link |
||
12 | * @copyright 2016 LightSpeed |
||
13 | */ |
||
14 | class LSX_Projects_SCPO_Engine { |
||
15 | |||
16 | function __construct() { |
||
0 ignored issues
–
show
|
|||
17 | if ( ! get_option( 'lsx_projects_scporder_install' ) ) { |
||
0 ignored issues
–
show
|
|||
18 | $this->lsx_projects_scporder_install(); |
||
19 | } |
||
20 | |||
21 | add_action( 'admin_init', array( $this, 'refresh' ) ); |
||
22 | add_action( 'admin_init', array( $this, 'load_script_css' ) ); |
||
23 | |||
24 | add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) ); |
||
25 | |||
26 | add_action( 'pre_get_posts', array( $this, 'lsx_projects_scporder_pre_get_posts' ) ); |
||
27 | |||
28 | add_filter( 'get_previous_post_where', array( $this, 'lsx_projects_scporder_previous_post_where' ) ); |
||
29 | add_filter( 'get_previous_post_sort', array( $this, 'lsx_projects_scporder_previous_post_sort' ) ); |
||
30 | add_filter( 'get_next_post_where', array( $this, 'lsx_projects_scporder_next_post_where' ) ); |
||
31 | add_filter( 'get_next_post_sort', array( $this, 'lsx_projects_scporder_next_post_sort' ) ); |
||
32 | } |
||
0 ignored issues
–
show
|
|||
33 | |||
34 | function lsx_projects_scporder_install() { |
||
0 ignored issues
–
show
|
|||
35 | update_option( 'lsx_projects_scporder_install', 1 ); |
||
36 | } |
||
0 ignored issues
–
show
|
|||
37 | |||
38 | function _check_load_script_css() { |
||
0 ignored issues
–
show
|
|||
39 | $active = false; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
40 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
41 | |||
42 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
43 | return false; |
||
44 | } |
||
45 | |||
46 | if ( isset( $_GET['orderby'] ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'action=edit' ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/post-new.php' ) ) { |
||
0 ignored issues
–
show
|
|||
47 | return false; |
||
48 | } |
||
49 | |||
50 | if ( ! empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
51 | if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), $objects ) ) { // if page or custom post types |
||
0 ignored issues
–
show
|
|||
52 | $active = true; |
||
53 | } |
||
54 | |||
55 | if ( ! isset( $_GET['post_type'] ) && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/edit.php' ) && array_key_exists( 'post', $objects ) ) { // if post |
||
0 ignored issues
–
show
|
|||
56 | $active = true; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return $active; |
||
61 | } |
||
0 ignored issues
–
show
|
|||
62 | |||
63 | function load_script_css() { |
||
0 ignored issues
–
show
|
|||
64 | if ( $this->_check_load_script_css() ) { |
||
0 ignored issues
–
show
|
|||
65 | wp_enqueue_script( 'scporderjs', LSX_PROJECTS_URL . 'assets/js/scporder.min.js', array( 'jquery', 'jquery-ui-sortable' ), null, true ); |
||
66 | |||
67 | $scporderjs_params = array( |
||
68 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
69 | 'ajax_nonce' => wp_create_nonce( 'scporder' ), |
||
70 | ); |
||
71 | |||
72 | wp_localize_script( 'scporderjs', 'scporderjs_params', $scporderjs_params ); |
||
73 | |||
74 | wp_enqueue_style( 'scporder', LSX_PROJECTS_URL . 'assets/css/scporder.css', array(), null ); |
||
75 | } |
||
76 | } |
||
0 ignored issues
–
show
|
|||
77 | |||
78 | function refresh() { |
||
0 ignored issues
–
show
|
|||
79 | global $wpdb; |
||
80 | |||
81 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
82 | |||
83 | if ( ! empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
84 | foreach ( $objects as $object => $object_data ) { |
||
0 ignored issues
–
show
|
|||
85 | $result = $wpdb->get_results($wpdb->prepare(" |
||
0 ignored issues
–
show
|
|||
86 | SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min |
||
87 | FROM $wpdb->posts |
||
88 | WHERE post_type = '%s' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') |
||
89 | ", $object)); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
90 | |||
91 | if ( 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max ) { |
||
0 ignored issues
–
show
|
|||
92 | continue; |
||
93 | } |
||
94 | |||
95 | $results = $wpdb->get_results( $wpdb->prepare(" |
||
0 ignored issues
–
show
|
|||
96 | SELECT ID |
||
97 | FROM $wpdb->posts |
||
98 | WHERE post_type = '%s' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') |
||
99 | ORDER BY menu_order ASC |
||
100 | ", $object ) ); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
101 | |||
102 | foreach ( $results as $key => $result ) { |
||
0 ignored issues
–
show
|
|||
103 | $wpdb->update( |
||
104 | $wpdb->posts, |
||
105 | array( |
||
106 | 'menu_order' => $key + 1, |
||
107 | ), |
||
108 | array( |
||
109 | 'ID' => $result->ID, |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
0 ignored issues
–
show
|
|||
116 | |||
117 | function update_menu_order() { |
||
0 ignored issues
–
show
|
|||
118 | check_ajax_referer( 'scporder', 'security' ); |
||
119 | |||
120 | global $wpdb; |
||
121 | |||
122 | parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data ); |
||
123 | |||
124 | if ( ! is_array( $data ) ) { |
||
0 ignored issues
–
show
|
|||
125 | return false; |
||
126 | } |
||
127 | |||
128 | $id_arr = array(); |
||
129 | |||
130 | foreach ( $data as $key => $values ) { |
||
0 ignored issues
–
show
|
|||
131 | foreach ( $values as $position => $id ) { |
||
0 ignored issues
–
show
|
|||
132 | $id_arr[] = $id; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | $menu_order_arr = array(); |
||
137 | |||
138 | foreach ( $id_arr as $key => $id ) { |
||
0 ignored issues
–
show
|
|||
139 | $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) ); |
||
140 | |||
141 | foreach ( $results as $result ) { |
||
0 ignored issues
–
show
|
|||
142 | $menu_order_arr[] = $result->menu_order; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | sort( $menu_order_arr ); |
||
147 | |||
148 | foreach ( $data as $key => $values ) { |
||
0 ignored issues
–
show
|
|||
149 | foreach ( $values as $position => $id ) { |
||
0 ignored issues
–
show
|
|||
150 | $wpdb->update( |
||
151 | $wpdb->posts, |
||
152 | array( |
||
153 | 'menu_order' => $menu_order_arr[ $position ], |
||
154 | ), |
||
155 | array( |
||
156 | 'ID' => intval( $id ), |
||
157 | ) |
||
158 | ); |
||
159 | } |
||
160 | } |
||
161 | } |
||
0 ignored issues
–
show
|
|||
162 | |||
163 | function lsx_projects_scporder_previous_post_where( $where ) { |
||
0 ignored issues
–
show
|
|||
164 | global $post; |
||
165 | |||
166 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
167 | |||
168 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
169 | return $where; |
||
170 | } |
||
171 | |||
172 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
0 ignored issues
–
show
|
|||
173 | $current_menu_order = $post->menu_order; |
||
174 | $where = "WHERE p.menu_order > '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'"; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
175 | } |
||
176 | |||
177 | return $where; |
||
178 | } |
||
0 ignored issues
–
show
|
|||
179 | |||
180 | function lsx_projects_scporder_previous_post_sort( $orderby ) { |
||
0 ignored issues
–
show
|
|||
181 | global $post; |
||
182 | |||
183 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
184 | |||
185 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
186 | return $orderby; |
||
187 | } |
||
188 | |||
189 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
0 ignored issues
–
show
|
|||
190 | $orderby = 'ORDER BY p.menu_order ASC LIMIT 1'; |
||
191 | } |
||
192 | |||
193 | return $orderby; |
||
194 | } |
||
0 ignored issues
–
show
|
|||
195 | |||
196 | function lsx_projects_scporder_next_post_where( $where ) { |
||
0 ignored issues
–
show
|
|||
197 | global $post; |
||
198 | |||
199 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
200 | |||
201 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
202 | return $where; |
||
203 | } |
||
204 | |||
205 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
0 ignored issues
–
show
|
|||
206 | $current_menu_order = $post->menu_order; |
||
207 | $where = "WHERE p.menu_order < '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'"; |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
208 | } |
||
209 | |||
210 | return $where; |
||
211 | } |
||
0 ignored issues
–
show
|
|||
212 | |||
213 | function lsx_projects_scporder_next_post_sort( $orderby ) { |
||
0 ignored issues
–
show
|
|||
214 | global $post; |
||
215 | |||
216 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
217 | |||
218 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
219 | return $orderby; |
||
220 | } |
||
221 | |||
222 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
0 ignored issues
–
show
|
|||
223 | $orderby = 'ORDER BY p.menu_order DESC LIMIT 1'; |
||
224 | } |
||
225 | |||
226 | return $orderby; |
||
227 | } |
||
0 ignored issues
–
show
|
|||
228 | |||
229 | function lsx_projects_scporder_pre_get_posts( $wp_query ) { |
||
0 ignored issues
–
show
|
|||
230 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
231 | |||
232 | if ( empty( $objects ) ) { |
||
0 ignored issues
–
show
|
|||
233 | return false; |
||
234 | } |
||
235 | |||
236 | if ( is_admin() ) { |
||
0 ignored issues
–
show
|
|||
237 | if ( isset( $wp_query->query['post_type'] ) && ! isset( $_GET['orderby'] ) ) { |
||
0 ignored issues
–
show
|
|||
238 | if ( array_key_exists( $wp_query->query['post_type'], $objects ) ) { |
||
0 ignored issues
–
show
|
|||
239 | $wp_query->set( 'orderby', 'menu_order' ); |
||
240 | $wp_query->set( 'order', 'ASC' ); |
||
241 | } |
||
242 | } |
||
243 | } else { |
||
244 | $active = false; |
||
245 | |||
246 | if ( isset( $wp_query->query['post_type'] ) ) { |
||
0 ignored issues
–
show
|
|||
247 | if ( ! is_array( $wp_query->query['post_type'] ) ) { |
||
0 ignored issues
–
show
|
|||
248 | if ( array_key_exists( $wp_query->query['post_type'], $objects ) ) { |
||
0 ignored issues
–
show
|
|||
249 | $active = true; |
||
250 | } |
||
251 | } |
||
252 | } else { |
||
253 | if ( array_key_exists( 'post', $objects ) ) { |
||
0 ignored issues
–
show
|
|||
254 | $active = true; |
||
255 | } |
||
256 | } |
||
257 | |||
258 | if ( ! $active ) { |
||
0 ignored issues
–
show
|
|||
259 | return false; |
||
260 | } |
||
261 | |||
262 | if ( isset( $wp_query->query['suppress_filters'] ) ) { |
||
0 ignored issues
–
show
|
|||
263 | if ( $wp_query->get( 'orderby' ) == 'date' ) { |
||
0 ignored issues
–
show
|
|||
264 | $wp_query->set( 'orderby', 'menu_order' ); |
||
265 | } |
||
266 | |||
267 | if ( $wp_query->get( 'order' ) == 'DESC' ) { |
||
0 ignored issues
–
show
|
|||
268 | $wp_query->set( 'order', 'ASC' ); |
||
269 | } |
||
270 | } else { |
||
271 | if ( ! $wp_query->get( 'orderby' ) ) { |
||
0 ignored issues
–
show
|
|||
272 | $wp_query->set( 'orderby', 'menu_order' ); |
||
273 | } |
||
274 | |||
275 | if ( ! $wp_query->get( 'order' ) ) { |
||
0 ignored issues
–
show
|
|||
276 | $wp_query->set( 'order', 'ASC' ); |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | } |
||
0 ignored issues
–
show
|
|||
281 | |||
282 | function get_lsx_projects_scporder_options_objects() { |
||
0 ignored issues
–
show
|
|||
283 | return array( |
||
284 | 'project' => esc_html_x( 'Project', 'post type singular name', 'lsx-project' ), |
||
285 | ); |
||
286 | } |
||
0 ignored issues
–
show
|
|||
287 | |||
288 | } |
||
289 | |||
290 | /** |
||
291 | * SCP Order Uninstall hook |
||
292 | */ |
||
293 | register_uninstall_hook( __FILE__, 'lsx_projects_scporder_uninstall' ); |
||
294 | |||
295 | function lsx_projects_scporder_uninstall() { |
||
0 ignored issues
–
show
|
|||
296 | global $wpdb; |
||
297 | |||
298 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
||
0 ignored issues
–
show
|
|||
299 | $curr_blog = $wpdb->blogid; |
||
300 | $blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
301 | |||
302 | foreach ( $blogids as $blog_id ) { |
||
0 ignored issues
–
show
|
|||
303 | switch_to_blog( $blog_id ); |
||
304 | lsx_projects_scporder_uninstall_db(); |
||
305 | } |
||
306 | |||
307 | switch_to_blog( $curr_blog ); |
||
308 | } else { |
||
309 | lsx_projects_scporder_uninstall_db(); |
||
310 | } |
||
311 | } |
||
0 ignored issues
–
show
|
|||
312 | |||
313 | function lsx_projects_scporder_uninstall_db() { |
||
0 ignored issues
–
show
|
|||
314 | delete_option( 'lsx_projects_scporder_install' ); |
||
315 | } |
||
0 ignored issues
–
show
|
|||
316 |