PluginProbe
WebberZone Top 10 — Popular Posts / trunk
WebberZone Top 10 — Popular Posts vtrunk
4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 1.6.2 All 116 releases
top-10 / includes / admin / class-metabox.php

class-metabox.php in WebberZone Top 10 — Popular Posts trunk, at includes/admin/class-metabox.php

331 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Metabox class.
4 *
5 * @package WebberZone\Top_Ten\Admin
6 */
7
8 namespace WebberZone\Top_Ten\Admin;
9
10 use WebberZone\Top_Ten\Counter;
11 use WebberZone\Top_Ten\Util\Hook_Registry;
12
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Admin Metabox Class.
19 *
20 * @since 3.3.0
21 */
22 class Metabox {
23
24 /**
25 * Constructor class.
26 *
27 * @since 3.3.0
28 */
29 public function __construct() {
30 Hook_Registry::add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
31 Hook_Registry::add_action( 'save_post', array( $this, 'save_meta_box' ) );
32 Hook_Registry::add_action( 'edit_attachment', array( $this, 'save_meta_box' ) );
33 Hook_Registry::add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 11 );
34 }
35
36 /**
37 * Function to add meta box in Write screens.
38 *
39 * @since 1.9.10
40 *
41 * @param string $post_type Post type.
42 */
43 public static function add_meta_box( $post_type ) {
44
45 // If metaboxes are disabled, then exit.
46 if ( ! \tptn_get_option( 'show_metabox' ) ) {
47 return;
48 }
49
50 // If current user isn't an admin and we're restricting metaboxes to admins only, then exit.
51 if ( ! current_user_can( 'manage_options' ) && \tptn_get_option( 'show_metabox_admins' ) ) {
52 return;
53 }
54
55 /**
56 * Filters whether to show the Top 10 meta box.
57 *
58 * @since 3.1.0
59 *
60 * @param bool $show_meta_box Whether the Top 10 meta box should be shown. Default true.
61 */
62 $show_meta_box = apply_filters( 'tptn_show_meta_box', true );
63
64 if ( ! $show_meta_box ) {
65 return;
66 }
67
68 $args = array(
69 'public' => true,
70 );
71 $post_types = get_post_types( $args );
72
73 /**
74 * Filter post types on which the meta box is displayed
75 *
76 * @since 2.2.0
77 *
78 * @param array $post_types Array of post types
79 */
80 $post_types = apply_filters( 'tptn_meta_box_post_types', $post_types );
81
82 if ( in_array( $post_type, $post_types, true ) ) {
83
84 add_meta_box(
85 'tptn_metabox',
86 'Top 10',
87 array( __CLASS__, 'call_meta_box' ),
88 $post_type,
89 'advanced',
90 'default'
91 );
92 }
93 }
94
95 /**
96 * Function to call the meta box.
97 *
98 * @since 1.9.10
99 */
100 public static function call_meta_box() {
101 global $post;
102
103 // Add an nonce field so we can check for it later.
104 wp_nonce_field( 'tptn_meta_box', 'tptn_meta_box_nonce' );
105
106 // Get the number of visits for the post being editted.
107 $total_count = get_tptn_total_count( $post->ID, get_current_blog_id() );
108
109 // Get the post meta.
110 $post_meta = get_post_meta( $post->ID, 'tptn_post_meta', true );
111
112 $disable_here = isset( $post_meta['disable_here'] ) ? $post_meta['disable_here'] : 0;
113 $exclude_this_post = isset( $post_meta['exclude_this_post'] ) ? $post_meta['exclude_this_post'] : 0;
114
115 $thumb_meta = get_post_meta( $post->ID, \tptn_get_option( 'thumb_meta' ), true );
116 $thumb_meta = ( $thumb_meta ) ? $thumb_meta : '';
117
118 ?>
119 <p>
120 <label for="total_count"><strong><?php esc_html_e( 'Visit count:', 'top-10' ); ?></strong></label>
121 <input type="text" id="total_count" name="total_count" value="<?php echo esc_attr( (string) $total_count ); ?>" style="width:100%" />
122 <em><?php esc_html_e( 'Enter a number above to update the visit count. Leaving the above box blank will set the count to zero', 'top-10' ); ?></em>
123 <input type="hidden" id="total_count_original" name="total_count_original" value="<?php echo esc_attr( (string) $total_count ); ?>">
124 </p>
125
126 <p>
127 <label for="disable_here"><strong><?php esc_html_e( 'Disable Popular Posts display:', 'top-10' ); ?></strong></label>
128 <input type="checkbox" id="disable_here" name="disable_here" <?php checked( 1, $disable_here, true ); ?> />
129 <br />
130 <em><?php esc_html_e( 'If this is checked, then Top 10 will not display the popular posts widgets when viewing this post.', 'top-10' ); ?></em>
131 </p>
132
133 <p>
134 <label for="exclude_this_post"><strong><?php esc_html_e( 'Exclude this post from the popular posts list:', 'top-10' ); ?></strong></label>
135 <input type="checkbox" id="exclude_this_post" name="exclude_this_post" <?php checked( 1, $exclude_this_post, true ); ?> />
136 <br />
137 <em><?php esc_html_e( 'If this is checked, then this post will be excluded from the popular posts list.', 'top-10' ); ?></em>
138 </p>
139
140 <p>
141 <label for="thumb_meta"><strong><?php esc_html_e( 'Location of thumbnail:', 'top-10' ); ?></strong></label>
142 <input type="text" id="thumb_meta" name="thumb_meta" value="<?php echo esc_url( $thumb_meta ); ?>" style="width:100%" />
143 <em><?php esc_html_e( "Enter the full URL to the image (JPG, PNG or GIF) you'd like to use. This image will be used for the post. It will be resized to the thumbnail size set under Top 10 Settings &raquo; Thumbnail options.", 'top-10' ); ?></em>
144 <em><?php esc_html_e( 'The URL above is saved in the meta field:', 'top-10' ); ?></em><strong><?php echo esc_attr( \tptn_get_option( 'thumb_meta' ) ); ?></strong>
145 </p>
146
147 <p>
148 <?php if ( function_exists( 'crp_get_settings' ) ) { ?>
149 <em style="color:red">
150 <?php
151 /* translators: 1: Plugin name */
152 printf( __( 'You have %1$s installed. If you are trying to modify the thumbnail, then you will need to make the same change in the %1$s meta box on this page.', 'top-10' ), 'Contextual Related Posts' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
153 ?>
154 </em>
155 <?php } ?>
156 </p>
157
158 <?php
159 if ( $thumb_meta ) {
160 echo '<img src="' . esc_url( $thumb_meta ) . '" style="max-width:100%" />';
161 }
162 ?>
163
164 <?php
165 /**
166 * Action triggered when displaying Top 10 meta box
167 *
168 * @since 4.0.0
169 *
170 * @param object $post Post object
171 */
172 do_action( 'tptn_call_meta_box', $post );
173 }
174
175
176 /**
177 * Function to save the meta box.
178 *
179 * @since 1.9.10
180 *
181 * @param int $post_id Post ID.
182 */
183 public static function save_meta_box( $post_id ) {
184 global $wpdb;
185
186 $post_meta = array();
187
188 // Bail if we're doing an auto save.
189 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
190 return;
191 }
192
193 // If our nonce isn't there, or we can't verify it, bail.
194 if ( ! isset( $_POST['tptn_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['tptn_meta_box_nonce'] ), 'tptn_meta_box' ) ) {
195 return;
196 }
197
198 // If our current user can't edit this post, bail.
199 if ( ! current_user_can( 'edit_post', $post_id ) ) {
200 return;
201 }
202
203 // Update the posts view count.
204 if ( isset( $_POST['total_count'] ) && isset( $_POST['total_count_original'] ) ) {
205 $total_count = intval( $_POST['total_count'] );
206 $total_count_original = intval( $_POST['total_count_original'] );
207 $blog_id = get_current_blog_id();
208
209 if ( 0 === $total_count ) {
210 Counter::delete_count( $post_id, $blog_id );
211 } elseif ( $total_count_original !== $total_count ) {
212 Counter::edit_count( $post_id, $blog_id, $total_count );
213 }
214 }
215
216 // Update the thumbnail URL.
217 if ( isset( $_POST['thumb_meta'] ) ) {
218 $thumb_meta = empty( $_POST['thumb_meta'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['thumb_meta'] ) );
219 }
220
221 if ( ! empty( $thumb_meta ) ) {
222 update_post_meta( $post_id, \tptn_get_option( 'thumb_meta' ), $thumb_meta );
223 } else {
224 delete_post_meta( $post_id, \tptn_get_option( 'thumb_meta' ) );
225 }
226
227 $post_meta['disable_here'] = isset( $_POST['disable_here'] ) ? 1 : 0;
228 $post_meta['exclude_this_post'] = isset( $_POST['exclude_this_post'] ) ? 1 : 0;
229
230 /**
231 * Filter the Top 10 Post meta variable which contains post-specific settings
232 *
233 * @since 2.2.0
234 *
235 * @param array $post_meta Top 10 post-specific settings
236 * @param int $post_id Post ID
237 */
238 $post_meta = apply_filters( 'tptn_post_meta', $post_meta, $post_id );
239
240 $post_meta_filtered = array_filter( $post_meta );
241
242 /**** Now we can start saving */
243 if ( empty( $post_meta_filtered ) ) { // Checks if all the array items are 0 or empty.
244 delete_post_meta( $post_id, 'tptn_post_meta' ); // Delete the post meta if no options are set.
245 } else {
246 update_post_meta( $post_id, 'tptn_post_meta', $post_meta );
247 }
248
249 /**
250 * Action triggered when saving Contextual Related Posts meta box settings
251 *
252 * @since 2.2
253 *
254 * @param int $post_id Post ID
255 */
256 do_action( 'tptn_save_meta_box', $post_id );
257 }
258
259 /**
260 * Enqueue scripts and styles for the meta box.
261 *
262 * @since 4.0.0
263 */
264 public function admin_enqueue_scripts() {
265
266 // If metaboxes are disabled, then exit.
267 if ( ! \tptn_get_option( 'show_metabox' ) ) {
268 return;
269 }
270
271 // If current user isn't an admin and we're restricting metaboxes to admins only, then exit.
272 if ( ! current_user_can( 'manage_options' ) && \tptn_get_option( 'show_metabox_admins' ) ) {
273 return;
274 }
275
276 $screen = get_current_screen();
277 if ( null === $screen ) {
278 return;
279 }
280
281 if ( 'post' === $screen->base || 'page' === $screen->base ) {
282 $file_prefix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
283
284 if ( ! wp_style_is( 'wz-tptn-tom-select', 'registered' ) ) {
285 wp_register_style(
286 'wz-tptn-tom-select',
287 TOP_TEN_PLUGIN_URL . 'includes/admin/settings/css/tom-select.min.css',
288 array(),
289 \WebberZone\Top_Ten\Admin\Settings\Settings_API::VERSION
290 );
291 }
292
293 if ( ! wp_script_is( 'wz-tptn-tom-select', 'registered' ) ) {
294 wp_register_script(
295 'wz-tptn-tom-select',
296 TOP_TEN_PLUGIN_URL . 'includes/admin/settings/js/tom-select.complete.min.js',
297 array( 'jquery' ),
298 \WebberZone\Top_Ten\Admin\Settings\Settings_API::VERSION,
299 true
300 );
301 }
302
303 if ( ! wp_script_is( 'wz-tptn-tom-select-init', 'registered' ) ) {
304 wp_register_script(
305 'wz-tptn-tom-select-init',
306 TOP_TEN_PLUGIN_URL . "includes/admin/settings/js/tom-select-init{$file_prefix}.js",
307 array( 'jquery', 'wz-tptn-tom-select' ),
308 \WebberZone\Top_Ten\Admin\Settings\Settings_API::VERSION,
309 true
310 );
311 }
312
313 wp_localize_script(
314 'wz-tptn-tom-select-init',
315 'WZTomSelectSettings',
316 array(
317 'endpoint' => 'category',
318 'strings' => array(
319 /* translators: %s: search term */
320 'no_results' => esc_html__( 'No results found for "%s"', 'top-10' ),
321 ),
322 )
323 );
324
325 wp_enqueue_style( 'wz-tptn-tom-select' );
326 wp_enqueue_script( 'wz-tptn-tom-select' );
327 wp_enqueue_script( 'wz-tptn-tom-select-init' );
328 }
329 }
330 }
331