PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / includes / public / class-wpupg-meta-box.php

class-wpupg-meta-box.php in WP Ultimate Post Grid 4.0.1, at includes/public/class-wpupg-meta-box.php

139 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for the grid meta box.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 3.0.0
7 *
8 * @package WP_Ultimate_Post_Grid
9 * @subpackage WP_Ultimate_Post_Grid/includes/public
10 */
11
12 /**
13 * Responsible for the grid meta box.
14 *
15 * @since 3.0.0
16 * @package WP_Ultimate_Post_Grid
17 * @subpackage WP_Ultimate_Post_Grid/includes/public
18 * @author Brecht Vandersmissen <[email protected]>
19 */
20 class WPUPG_Meta_Box {
21
22 /**
23 * Register actions and filters.
24 *
25 * @since 3.0.0
26 */
27 public static function init() {
28 add_action( 'init', array( __CLASS__, 'meta_fields_in_rest' ) );
29 add_action( 'admin_init', array( __CLASS__, 'add_meta_box' ) );
30
31 add_action( 'edit_attachment', array( __CLASS__, 'save_attachment' ) );
32 add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 2 );
33 }
34
35 /**
36 * Register meta fields for the REST API.
37 *
38 * @since 3.0.0
39 */
40 public static function meta_fields_in_rest() {
41 $post_types = WPUPG_Settings::get( 'meta_box_post_types' );
42 $fields = array(
43 'wpupg_custom_link' => 'string',
44 'wpupg_custom_link_behaviour' => 'string',
45 'wpupg_custom_link_nofollow' => 'string',
46 'wpupg_custom_image' => 'string',
47 'wpupg_custom_image_id' => 'integer',
48 );
49
50 foreach( $post_types as $post_type ) {
51 foreach ( $fields as $field => $type ) {
52 register_meta( $post_type, $field, array( 'show_in_rest' => true, 'type' => $type ) );
53 }
54 }
55 }
56
57 /**
58 * Add the meta box.
59 *
60 * @since 3.0.0
61 */
62 public static function add_meta_box() {
63 $post_types = WPUPG_Settings::get( 'meta_box_post_types' );
64
65 foreach( $post_types as $post_type ) {
66 add_meta_box(
67 'wpupg_meta_box_post',
68 'WP Ultimate Post Grid',
69 array( __CLASS__, 'meta_box' ),
70 $post_type,
71 'normal',
72 'high',
73 array(
74 '__back_compat_meta_box' => true,
75 )
76 );
77 }
78 }
79
80 /**
81 * Meta box content.
82 *
83 * @since 3.0.0
84 */
85 public static function meta_box() {
86 $post = get_post();
87 include( WPUPG_DIR . 'templates/admin/meta-box.php' );
88 }
89
90 /**
91 * Save meta box fields when saving attachment.
92 *
93 * @since 3.0.0
94 * @param int $post_id ID of the attachment getting saved.
95 */
96 public static function save_attachment( $post_id ) {
97 $post = get_post( $post_id );
98 self::save_post( $post_id, $post );
99 }
100
101 /**
102 * Save meta box fields when saving post.
103 *
104 * @since 3.0.0
105 * @param int $post_id ID of the post getting saved.
106 * @param mixed $post Post object getting saved.
107 */
108 public static function save_post( $post_id, $post ) {
109 if ( $post->post_type !== WPUPG_POST_TYPE ) {
110 if ( ! isset( $_POST['wpupg_nonce'] ) || ! wp_verify_nonce( $_POST['wpupg_nonce'], 'grid' ) ) {
111 return;
112 }
113
114 // Meta fields.
115 $fields = array(
116 'wpupg_custom_link',
117 'wpupg_custom_link_behaviour',
118 'wpupg_custom_link_nofollow',
119 'wpupg_custom_image',
120 'wpupg_custom_image_id',
121 );
122
123 foreach ( $fields as $field ) {
124 $old = get_post_meta( $post_id, $field, true );
125 $new = isset( $_POST[$field] ) ? $_POST[$field] : null;
126
127 // Update or delete meta data if changed.
128 if ( isset( $new ) && $new !== $old ) {
129 update_post_meta( $post_id, $field, $new );
130 } elseif ( $new == '' && $old ) {
131 delete_post_meta( $post_id, $field, $old );
132 }
133 }
134 }
135 }
136 }
137
138 WPUPG_Meta_Box::init();
139