Plugin Directory

source: featured-image-caption/trunk/classes/MetaBox.php @ 2300545

Last change on this file since 2300545 was 2300545, checked in by cconover, 5 years ago

Version 0.8.9

File size: 5.1 KB
Line 
1<?php
2
3/**
4 * Post meta box.
5 *
6 * @since 0.7.0
7 */
8
9namespace cconover\FeaturedImageCaption;
10
11class MetaBox {
12    /**
13     * Create the meta box.
14     *
15     * @todo Support for custom post types.
16     */
17    public function metabox()
18    {
19        // Specify the screens where the meta box should be available
20        $screens = apply_filters('cc_featured_image_caption_screens', array('post', 'page'));
21
22        // Iterate through the specified screens to add the meta box
23        foreach ($screens as $screen) {
24            add_meta_box(
25                CCFIC_ID, // HTML ID for the meta box
26                CCFIC_NAME, // Title of the meta box displayed to the us
27                array($this, 'metabox_callback'), // Callback function for the meta box to display it to the user
28                $screen, // Locations where the meta box should be shown
29                'side' // Location where the meta box should be shown. This one is placed on the side.
30            );
31        }
32    }
33
34    /**
35     * Featured image caption meta box callback.
36     */
37    public function metabox_callback($post)
38    {
39        // Add a nonce field to verify data submissions came from our site
40        wp_nonce_field(CCFIC_ID, CCFIC_KEY.'_nonce');
41
42        // Retrieve the current caption as a string, if set
43        $caption = get_post_meta($post->ID, '_'.CCFIC_KEY, true);
44
45        // If the data is a string, convert it to an array (legacy data support)
46        if (is_string($caption)) {
47            $caption = array(
48                'caption_text' => $caption,
49            );
50        }
51
52        echo '<label for="'.CCFIC_KEY.'_caption_text">Caption text</label><textarea style="width: 100%; max-width: 100%;" id="'.CCFIC_KEY.'_caption_text" name="'.CCFIC_KEY.'_caption_text">'.(! empty($caption['caption_text']) ? esc_attr($caption['caption_text']) : null).'</textarea>';
53        echo '<br><br>';
54        echo '<strong>Source Attribution</strong><br>';
55        echo '<label for="'.CCFIC_KEY.'_source_text">Text</label><input type="text" style="width: 100%;" id="'.CCFIC_KEY.'_source_text" name="'.CCFIC_KEY.'_source_text" value="'.(! empty($caption['source_text']) ? esc_attr($caption['source_text']) : null).'">';
56        echo '<label for="'.CCFIC_KEY.'_source_url">URL</label><input type="text" style="width: 100%;" id="'.CCFIC_KEY.'_source_url" name="'.CCFIC_KEY.'_source_url" value="'.(! empty($caption['source_url']) ? $caption['source_url'] : null).'">';
57        echo '<input type="checkbox" name="'.CCFIC_KEY.'_new_window" value="1"'.($this->new_window_checked($caption) ? ' checked' : null).'><label for="'.CCFIC_KEY.'_new_window">Open in new window</label>';
58    }
59
60    /**
61     * Save the meta box data.
62     */
63    public function save_metabox($post_id)
64    {
65        /*
66        Verify using the nonce that the data was submitted from our meta box on our site.
67        If it wasn't, return the post ID and be on our way.
68        */
69        // If no nonce was provided or the nonce does not match
70        if (! isset($_POST[CCFIC_KEY.'_nonce']) || ! wp_verify_nonce($_POST[CCFIC_KEY.'_nonce'], CCFIC_ID)) {
71            return $post_id;
72        }
73
74        // Make sure the user has valid permissions
75        // If we're editing a page and the user isn't allowed to do that, return the post ID
76        if ('page' == $_POST['post_type']) {
77            if (! current_user_can('edit_page', $post_id)) {
78                return $post_id;
79            }
80        }
81        // If we're editing any other post type and the user isn't allowed to do that, return the post ID
82        else {
83            if (! current_user_can('edit_post', $post_id)) {
84                return $post_id;
85            }
86        }
87
88        // Now that we've validated nonce and permissions, let's save the caption data
89        // Sanitize the caption
90        $caption = array(
91            'caption_text'    => $_POST[CCFIC_KEY.'_caption_text'],
92            'source_text'    => $_POST[CCFIC_KEY.'_source_text'],
93            'source_url'    => esc_url($_POST[CCFIC_KEY.'_source_url']),
94            'new_window'    => (! empty($_POST[CCFIC_KEY.'_new_window']) ? true : false),
95        );
96
97        // Update the caption meta field
98        update_post_meta($post_id, '_'.CCFIC_KEY, $caption);
99
100        // Update the user default for the "new window" checkbox
101        update_user_option(get_current_user_id(), CCFIC_KEY.'_new_window', $caption['new_window']);
102    }
103
104    /**
105     * Whether the "new window" checkbox should be checked.
106     *
107     * @param array $caption The caption data
108     *
109     * @return bool
110     */
111    public function new_window_checked($caption)
112    {
113        // If "new window" status is set for the caption data
114        if (! empty($caption['new_window'])) {
115            if ($caption['new_window']) {
116                return true;
117            } else {
118                return false;
119            }
120        }
121        // If not set, look for the user option
122        else {
123            $new_window = get_user_option(CCFIC_KEY.'_new_window', get_current_user_id());
124
125            if ($new_window) {
126                return true;
127            } else {
128                return false;
129            }
130        }
131    }
132}
Note: See TracBrowser for help on using the repository browser.