Plugin Directory

Changeset 1806936


Ignore:
Timestamp:
01/22/2018 08:57:34 AM (8 years ago)
Author:
themespond
Message:
  • Fix metabox in front_page is not display
  • Add: Display metabox in page by Template Name of the page
Location:
tp-framework
Files:
174 added
5 edited

Legend:

Unmodified
Added
Removed
  • tp-framework/trunk/includes/admin-fields/field_default.php

    r1785963 r1806936  
    212212 */
    213213function tpfw_form_select( $settings, $value = '' ) {
    214 //  p($settings);
    215 //  v($value);
    216 //  die();
     214   
    217215    $multiple = isset( $settings['multiple'] ) && $settings['multiple'] ? 'multiple' : '';
    218216
     
    260258                $selected = in_array( $option_key, $value ) ? 'selected' : '';
    261259            } else {
    262                 $selected = $option_key === $value ? 'selected' : '';
     260                $selected = $option_key == $value ? 'selected' : '';
    263261            }
    264262
  • tp-framework/trunk/includes/class-tpfw-metabox.php

    r1800926 r1806936  
    6161
    6262                $this->settings = wp_parse_args( $args, $defaults );
    63                
     63
    6464                add_action( 'add_meta_boxes', array( $this, 'register' ) );
    65                
    66                 foreach ( $this->settings['screens'] as $screen => $value ) {
    67 
    68                     if ( $value == 'front_page' || $value == 'posts_page' ) {
    69                         $screen = 'page';
    70                     } else if ( !is_string( $screen ) ) {
    71                         $screen = $value;
    72                     } else {
    73                         $screen = $value;
    74                     }
    75 
    76                     add_action( 'save_post_' . $screen, array( $this, 'save' ), 1, 2 );
    77                 }
     65                add_action( 'save_post', array( $this, 'save' ), 1, 2 );
    7866            }
    7967        }
     
    8674
    8775            global $post;
    88            
     76
    8977            if ( empty( $post ) ) {
    9078                return;
    9179            }
    9280
     81            if ( $screen = $this->getScreen( $post ) ) {
     82
     83                $this->field_wrapper = '<div class="tpfw_form_row" %3$s><div class="col-label">%1$s</div><div class="col-field">%2$s</div></div>';
     84
     85                $this->output = $this->pre_output( $post->ID );
     86
     87                add_meta_box( $this->settings['id'], $this->settings['heading'], array( $this, 'output' ), $screen, $this->settings['context'], $this->settings['priority'], $this->settings['fields'] );
     88            }
     89        }
     90
     91        /**
     92         * Get screen to display in metabox
     93         * @since 1.0.11
     94         *
     95         * @param object|WP_Post $post
     96         * @return string Or false
     97         */
     98        protected function getScreen( $post ) {
     99
    93100            $screen = false;
    94101
    95102            if ( in_array( $post->post_type, $this->settings['screens'] ) || in_array( $post->ID, $this->settings['screens'] ) ) {
    96103                $screen = $post->post_type;
     104            } else if ( !empty( $post->page_template ) ) {
     105
     106                $templates = wp_get_theme()->get_page_templates( $post, $post->post_type );
     107
     108                if ( isset( $templates[$post->page_template] ) ) {
     109                    $_screen = $templates[$post->page_template];
     110                    if ( in_array( $_screen, $this->settings['screens'] ) ) {
     111                        $screen = $post->post_type;
     112                    }
     113                }
     114               
    97115            } else {
    98                
     116
    99117                $blogPage = in_array( 'posts_page', $this->settings['screens'] ) && $post->ID == get_option( 'page_for_posts' );
    100                 $frontPage = in_array( 'front_page', $this->settings['screens'] ) && $post->ID == get_option( 'page_for_posts' );
     118                $frontPage = in_array( 'front_page', $this->settings['screens'] ) && $post->ID == get_option( 'page_on_front' );
     119
    101120                if ( $blogPage || $frontPage ) {
    102121                    $screen = 'page';
    103122                }
    104                
    105             }
    106            
    107             if ( $screen ) {
    108 
    109                 $this->field_wrapper = '<div class="tpfw_form_row" %3$s><div class="col-label">%1$s</div><div class="col-field">%2$s</div></div>';
    110 
    111                 $this->output = $this->pre_output( $post->ID );
    112 
    113                 add_meta_box( $this->settings['id'], $this->settings['heading'], array( $this, 'output' ), $screen, $this->settings['context'], $this->settings['priority'], $this->settings['fields'] );
    114             }
     123            }
     124
     125            return $screen;
    115126        }
    116127
     
    263274        /**
    264275         * Save post meta
     276         *
    265277         * @param int $post_id
    266278         * @param object|WP_Post $post
     
    268280        public function save( $post_id, $post ) {
    269281
     282            /* Don't save if $_POST is empty */
     283            if ( empty( $_POST ) ) {
     284                return $post_id;
     285            }
     286
     287            /* Don't save during autosave */
     288            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
     289                return $post_id;
     290
    270291            $metabox = $this->settings;
    271292
    272             /* don't save if $_POST is empty */
    273             if ( empty( $_POST ) )
    274                 return $post_id;
    275 
    276             /* don't save during autosave */
    277             if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    278                 return $post_id;
    279 
    280             /* verify nonce */
     293            /* Verify nonce */
    281294            if ( !isset( $_POST[$metabox['id'] . '_nonce'] ) || !wp_verify_nonce( $_POST[$metabox['id'] . '_nonce'], $metabox['id'] ) )
    282295                return $post_id;
    283296
    284             /* check permissions */
    285             if ( isset( $_POST['post_type'] ) && 'page' == sanitize_text_field( $_POST['post_type'] ) ) {
    286                 if ( !current_user_can( 'edit_page', $post_id ) )
    287                     return $post_id;
    288             } else {
    289                 if ( !current_user_can( 'edit_post', $post_id ) )
    290                     return $post_id;
    291             }
    292 
     297            /* Check permissions */
     298            $screen = $this->getScreen( $post );
     299
     300            if ( 'page' == $screen && !current_user_can( 'edit_page', $post_id ) ) {
     301                return $post_id;
     302            } else if ( !current_user_can( 'edit_post', $post_id ) ) {
     303                return $post_id;
     304            }
     305
     306            /**
     307             * Do save
     308             */
    293309            $allow_save = true;
    294310
     
    321337                            $value = call_user_func( $field['sanitize_callback'], $input_value );
    322338                        } else {
     339
    323340                            if ( $field['type'] == 'upload' ) {
    324341                                $value = tpfw_sanitize_upload( $input_value );
     
    328345                                $value = !empty( $input_value ) ? 1 : 0;
    329346                            } elseif ( $field['type'] == 'link' ) {
    330 
    331347                                $value = strip_tags( $input_value );
    332348                            } elseif ( $field['type'] == 'textarea' ) {
    333 
    334349                                $value = wp_kses( trim( wp_unslash( $input_value ) ), wp_kses_allowed_html( 'post' ) );
    335350                            } elseif ( $field['type'] == 'repeater' && !empty( $input_value ) ) {
     
    344359                         */
    345360                        $value = apply_filters( "tpfw_sanitize_field_{$field['type']}", $value, $input_value );
    346 
    347361                        update_post_meta( $post_id, $field['name'], $value );
    348362                    } else {
     
    351365                }
    352366            }
     367
    353368            do_action( sprintf( 'tpfw_%s_updated', $metabox['id'] ), $post_id, $post, $allow_save );
    354369        }
  • tp-framework/trunk/readme.txt

    r1800926 r1806936  
    55Requires at least: 4.5   
    66Tested up to: 4.9
    7 Stable tag: 1.0.10
     7Stable tag: 1.0.11
    88License: GPLv3   
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html   
     
    8383== Changelog ==
    8484
     85= 1.0.11 (January 22, 2018): =
     86* Fix metabox in front_page is not display
     87* Add: Display metabox in page by Template Name of the page
     88
    8589= 1.0.10 (January 11, 2018): =
    8690 * Fix dependency fields js in edit form taxonomy
  • tp-framework/trunk/sample/post-meta.php

    r1785963 r1806936  
    4848        ),
    4949        array(
    50            
    5150            'name' => 'tpfw_textfield',
    5251            'type' => 'textfield',
     
    223222    $box1 = new Tpfw_Metabox( array(
    224223        'id' => 'tpfw_metabox',
    225         'screens' => array( 'page' ), //Display in post, page, front_page, posts_page
     224        'screens' => array( 'post', 2, 'Contact Page', 'About Page' ), //Display in post, page, front_page, posts_page or page_template name
    226225        'heading' => __( 'Metabox', 'tp-framework' ),
    227226        'context' => 'advanced', //side
     
    292291                )
    293292            ),
    294            
    295293            array(
    296294                'name' => 'tpfw_textfield_multi',
  • tp-framework/trunk/tp-framework.php

    r1800926 r1806936  
    66  Description: Create Admin fields, metabox, widget, taxonomy, menu meta, customizer fields quickly and friendly.
    77  Author: themespond
    8   Version: 1.0.10
     8  Version: 1.0.11
    99  Author URI: https://themespond.com
    1010  Text Domain: tp-framework
     
    2222     * @var string
    2323     */
    24     public $version = '1.0.10';
     24    public $version = '1.0.11';
    2525
    2626    /**
Note: See TracChangeset for help on using the changeset viewer.