WordPress.org

Make WordPress Core

Ticket #12414: delete-postmeta-tester.php

File delete-postmeta-tester.php, 4.8 KB (added by studiograsshopper, 6 years ago)

Test plugin to demonstarte issue

Line 
1<?php
2/*
3Plugin Name: Delete postmeta test plugin
4Plugin URI: http://www.studiograsshopper.ch/dynamic-content-gallery/
5Version: 0.1
6Author: Ade Walker, Studiograsshopper
7Author URI: http://www.studiograsshopper.ch
8Description: To demonstrate problems with WP 3.0 and handling of postmeta deletion via Post editor metaboxes hooked to save_post
9*/
10
11/*  Copyright 2010  Ade WALKER  (email : info@studiograsshopper.ch) */
12
13/*      License information
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License 2 as published by
16the Free Software Foundation.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21GNU General Public License for more details.
22
23The license for this software can be found here:
24http://www.gnu.org/licenses/gpl-2.0.html
25*/
26
27/* Prevent direct access to this file */
28if (!defined('ABSPATH')) {
29        exit("Sorry, you are not allowed to access this file directly.");
30}
31
32define( 'SGR_FILE_HOOK', 'postmeta_test_plugin' );
33
34/* Admin - Adds Metaboxes to Post/Page Editor */
35// Function defined in dfcg-admin-metaboxes.php
36add_action('admin_menu', 'sgr_add_metabox');
37
38/* Admin - Saves Metabox data in Post/Page Editor */
39// Function defined in dfcg-admin-metaboxes.php
40add_action('save_post', 'sgr_save_metabox_data', 1, 2);
41
42
43/**
44* Adds metaboxes to Post and Page screen
45*
46* Hooked to 'admin_menu'
47*
48*/
49function sgr_add_metabox() {
50
51                add_meta_box( SGR_FILE_HOOK . '_box', __( 'Test metabox' ), 'sgr_meta_box', 'page', 'side', 'low' );
52       
53                add_meta_box( SGR_FILE_HOOK . '_box', __( 'Test metabox' ), 'sgr_meta_box', 'post', 'side', 'low' );
54}
55
56
57/**
58* Populates metaboxes in Post and Page screen
59*
60* Called by add_meta_box() in dfcg_add_metabox() function
61*
62* Note: Markup follows WP standards for Post/Page Editor sidebar
63*
64*/
65function sgr_meta_box($post) {
66
67        // Use nonce for verification
68        echo '<input type="hidden" name="sgr_metabox_noncename" id="sgr_metabox_noncename" value="' . 
69        wp_create_nonce( SGR_FILE_HOOK ) . '" />';
70       
71        ?>
72       
73<?php /* IMAGE BLOCK */ ?>
74        <div class="sgr-form">
75                <h5><?php _e('Enter a URL to something'); ?>:</h5>
76                <label class="screen-reader-text" for="_sgr-image"><?php _e('Image URL', DFCG_DOMAIN); ?></label>
77                <textarea id="_sgr-image" name="_sgr-image" style="font-size:11px;" cols="38" rows="2"><?php echo get_post_meta($post->ID, '_sgr-image', true); ?></textarea>
78               
79        </div>
80       
81<?php /* DESC BLOCK */ ?>
82       
83        <div class="sgr-form">
84                <h5><?php _e('Enter some text'); ?>:</h5>
85                <label class="screen-reader-text" for="_sgr-desc"><?php _e('Text', DFCG_DOMAIN); ?></label>
86                <textarea id="_sgr-desc" name="_sgr-desc" style="font-size:11px;" cols="38" rows="4"><?php echo get_post_meta($post->ID, '_sgr-desc', true); ?></textarea>
87        </div>
88<?php
89}
90
91
92/**
93* Saves data added/edited in metaboxes in Post and Page screen
94*
95* Hooked to 'save_post'
96*
97* Adapted from Write Panel plugin by Nathan Rice
98*
99*/
100function sgr_save_metabox_data($post_id, $post) {
101       
102        // Check referrer is from DCG metabox
103        if ( !wp_verify_nonce( $_POST['sgr_metabox_noncename'], SGR_FILE_HOOK )) {
104        return $post->ID;
105        }
106
107        // Is the user allowed to edit the post or page?
108        if ( 'page' == $_POST['post_type'] ) {
109                if ( !current_user_can( 'edit_page', $post->ID ))
110                return $post->ID;
111        } else {
112                if ( !current_user_can( 'edit_post', $post->ID ))
113                return $post->ID;
114        }
115
116        // Build array from $_POST data
117        $newdata['_sgr-image'] = $_POST['_sgr-image'];
118        $newdata['_sgr-desc'] = $_POST['_sgr-desc'];
119       
120        /* Sanitise data */
121       
122        // trim whitespace - all options
123        foreach( $newdata as $key => $value ) {
124                $input[$key] = trim($value);
125        }
126       
127        // Deal with Image
128        $newdata['_sgr-image'] = esc_url_raw( $newdata['_sgr-image'] );
129       
130        // Deal with Text
131        $allowed_html = array( 'a' => array('href' => array(),'title' => array() ), 'br' => array(), 'em' => array(), 'strong' => array() );
132        $allowed_protocols = array( 'http', 'https', 'mailto', 'feed' );
133       
134        $newdata['_sgr-desc'] = wp_kses( $newdata['_sgr-desc'], $allowed_html, $allowed_protocols );
135       
136       
137       
138        // Add values of $newdata as custom fields
139       
140        foreach ($newdata as $key => $value) {
141               
142                if( $post->post_type == 'revision' ) return; //don't store custom data twice
143               
144                $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
145               
146                if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value
147                        update_post_meta($post->ID, $key, $value);
148                } else { //if the custom field doesn't have a value
149                        add_post_meta($post->ID, $key, $value);
150                }
151               
152                if(!$value) delete_post_meta($post->ID, $key); //delete if any are blank, eg _dfcg-exclude is NULL
153        }
154}