Plugin Directory

Changeset 2097633


Ignore:
Timestamp:
05/30/2019 03:47:55 AM (7 years ago)
Author:
saroyaboy
Message:

Update 1.2

Location:
color-your-bar/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • color-your-bar/trunk/color-your-bar.php

    r2039374 r2097633  
    66 * Author: Darshan Saroya
    77 * Author URI: http://darshansaroya.com
    8  * Version: 1.01
     8 * Version: 1.2
    99 * Text Domain: color-your-bar
    1010 * Domain Path: languages
     
    2424 * @package Color Your Bar
    2525 * @author Darshan Saroya
    26  * @version 1.0
     26 * @version 1.2
    2727 */
     28
     29register_activation_hook( __FILE__, 'cyb_active_func' );
     30
     31function cyb_active_func(){
     32    add_option( 'cyb-switch', 0 );
     33    add_option( 'cyb-post-type', array('post', 'page') );
     34}
    2835
    2936add_action( 'admin_enqueue_scripts', 'cyb_assets' );
     
    3643
    3744function cyb_add_head_tag(){
     45    global $post;
     46    $cyb_post = get_post_type($post->ID);
     47    $cyb_post_type = get_option( 'cyb-post-type');
    3848    $enable_cyb= get_option('cyb-switch');
    39     $cyb_color= get_option('cyb-color');
     49    $cyb_color= get_option('cyb-color');
     50    if(null !== $cyb_post_type && is_array($cyb_post_type)){
     51        foreach ($cyb_post_type as $post_type ) {
     52            if($cyb_post == $post_type){
     53                $color = get_post_meta( $post->ID, 'cyb-color', true );
     54                if(null !== $color){
     55                    $cyb_color = $color;
     56                }
     57            }
     58        }
     59    }
    4060    if($enable_cyb!='0'){ ?>
    4161    <meta name="theme-color" content="<?php echo esc_html( $cyb_color ); ?>">
     
    102122    register_setting("cyb_setting_section", 'cyb-switch', $cyb_switch_args);
    103123
     124    add_settings_field('cyb-post-type', esc_html__( 'Show Meta Box on Post Type', 'color-your-bar' ), "cyb_post_type_element", "color-your-bar", "cyb_setting_section");
     125
     126    $cyb_post_type_args = array(
     127        'type' => 'array',
     128        'sanitize_callback' => 'cyb_sanitize_array',
     129        'default' => 0,
     130    );
     131    register_setting("cyb_setting_section", 'cyb-post-type', $cyb_post_type_args);
     132
    104133    add_settings_field('cyb-color', esc_html__( 'Choose Color', 'color-your-bar' ), "cyb_color_field_element", "color-your-bar", "cyb_setting_section");
    105134
     
    123152}
    124153
     154function cyb_post_type_callback(){
     155    esc_html_e( 'Settings to change Google Chrome(Mobile) address bar color', 'color-your-bar' );
     156}
     157
    125158function cyb_color_switch_element()
    126159{
     
    132165}
    133166
     167function cyb_post_type_element()
     168{
     169    $val= get_post_types ( array('public'   => true,));
     170    //id and name of form element should be same as the setting name.
     171    $post_selected = get_option('cyb-post-type');
     172    foreach ($val as $post_type) {
     173    ?>
     174        <input type="checkbox" name='cyb-post-type[]' value="<?php echo esc_attr($post_type); ?>" <?php 
     175        if(is_array($post_selected)){
     176            foreach($post_selected as $type){
     177                if($type == $post_type){
     178                    echo esc_attr( 'checked' );
     179                }
     180            }
     181        } ?>> <?php echo esc_html($post_type); ?><br>
     182    <?php }
     183}
     184
    134185function cyb_sanitize_checkbox($input){
    135186        if ( $input != 1 ) {
     
    140191}
    141192
     193function cyb_sanitize_array($input){
     194        if ( !is_array( $input ) ) {
     195            return '';
     196        } else {
     197            return $input;
     198        }
     199}
     200
    142201add_action("admin_init", "cyb_setting_display");
     202
     203function cyb_add_color_metaboxes() {
     204    $cyb_post_type = get_option( 'cyb-post-type' );
     205    foreach ($cyb_post_type as $post_type ) {
     206        add_meta_box(
     207            'cyb_color_meta',
     208            __('Chrome Bar Color', 'color-your-bar'),
     209            'cyb_color_meta',
     210            $post_type,
     211            'side',
     212            'default'
     213        );
     214    }
     215}
     216
     217if( get_option( 'cyb-switch') != 0){
     218    add_action( 'add_meta_boxes', 'cyb_add_color_metaboxes' );
     219}
     220
     221function cyb_color_meta() {
     222    global $post;
     223    // Nonce field to validate form request came from current site
     224    wp_nonce_field( basename( __FILE__ ), 'cyb-nonce' );
     225    // Get the cyb_color data if it's already been entered
     226    $cyb_color = get_option('cyb-color');
     227    if( null !== get_post_meta( $post->ID, 'cyb-color', true )  && get_post_meta( $post->ID, 'cyb-color', true )!=''){
     228        $cyb_color = get_post_meta( $post->ID, 'cyb-color', true );
     229    }
     230    // Output the field ?>
     231    <input type="text" name='cyb-color' id='cyb-color' value="<?php echo esc_html($cyb_color); ?>" />
     232<?php }
     233
     234/**
     235 * Save the metabox data
     236 */
     237function cyb_save_meta( $post_id, $post ) {
     238    // Return if the user doesn't have edit permissions.
     239    if ( ! current_user_can( 'edit_post', $post_id ) ) {
     240        return $post_id;
     241    }
     242    // Verify this came from the our screen and with proper authorization,
     243    // because save_post can be triggered at other times.
     244    if ( ! isset( $_POST['cyb-color'] ) || ! wp_verify_nonce( $_POST['cyb-nonce'], basename(__FILE__) ) ) {
     245        return $post_id;
     246    }
     247    // Now that we're authenticated, time to save the data.
     248    // This sanitizes the data from the field and saves it into an array $cyb_meta.
     249    $cyb_meta['cyb-color'] = esc_textarea( $_POST['cyb-color'] );
     250    // Cycle through the $cyb_meta array.
     251    // Note, in this example we just have one item, but this is helpful if you have multiple.
     252    foreach ( $cyb_meta as $key => $value ) :
     253        // Don't store custom data twice
     254        if ( 'revision' === $post->post_type ) {
     255            return;
     256        }
     257        if ( get_post_meta( $post_id, $key, false ) ) {
     258            // If the custom field already has a value, update it.
     259            update_post_meta( $post_id, $key, $value );
     260        } else {
     261            // If the custom field doesn't have a value, add it.
     262            add_post_meta( $post_id, $key, $value);
     263        }
     264        if ( ! $value ) {
     265            // Delete the meta key if there's no value
     266            delete_post_meta( $post_id, $key );
     267        }
     268    endforeach;
     269}
     270add_action( 'save_post', 'cyb_save_meta', 1, 2 );
     271
    143272?>
  • color-your-bar/trunk/readme.txt

    r2039374 r2097633  
    44Donate link: http://www.paypal.me/darshansaroya
    55Requires at least: 3.5
    6 Tested up to: 5.1
     6Tested up to: 5.2.1
    77License: GPL2
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3434
    3535== Changelog ==
    36 = 1.0 =
    37 Initial Release.
     36= 1.2 =
     37Meta Box Added: Now you can color every single page/post chrome bar.
    3838
    3939= 1.01 =
    4040Readme Updated
    4141
     42= 1.0 =
     43Initial Release.
     44
    4245== Upgrade Notice ==
    43461.0 Initial Release.
    44471.01 Readme Updated
     481.2 Meta Box Added
Note: See TracChangeset for help on using the changeset viewer.