WordPress.org

Plugin Directory

Changeset 1518153


Ignore:
Timestamp:
10/19/16 23:13:33 (14 months ago)
Author:
leocaseiro
Message:

Version 1.8.0 Released - add 2nd param to return collection for get_customs

Location:
custom-options-plus/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • custom-options-plus/trunk/custom-options-plus.php

    r1493670 r1518153  
    66You can for example, register the address and phone numbers of your company to leave in the header of your site. So, if someday relocate, you do not need to change your theme. Just change administratively. 
    77You can also enter the login of your social networks. How to login twitter, Facebook, Youtube, contact email and more. 
    8 Version: 1.7.1 
     8Version: 1.8.0 
    99Author: Leo Caseiro 
    1010Author URI: http://leocaseiro.com.br/ 
     
    4141// Added on 1.5 
    4242define( 'COP_OPTIONS_PREFIX', 'cop_' ); 
    43 define( 'COP_PLUGIN_VERSION', '1.7.0' ); 
     43define( 'COP_PLUGIN_VERSION', '1.8.0' ); 
    4444 
    4545global $wpdb, $COP_TABLE; 
     
    9999 
    100100    $row['label'] = stripslashes_deep( filter_var( $row['label'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
    101     $row['name']  = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
     101    $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
    102102    $row['value'] = stripslashes_deep( filter_var( $row['value'], FILTER_UNSAFE_RAW ) ); 
    103103 
     
    106106        array( 
    107107            'label' => $row['label'], 
    108             'name'  => $row['name'], 
     108            'name' => $row['name'], 
    109109            'value' => stripslashes( $row['value'] ) 
    110110        ), 
     
    117117    global $wpdb; 
    118118 
    119     $row['id']    = filter_var( $row['id'], FILTER_VALIDATE_INT ); 
     119    $row['id'] = filter_var( $row['id'], FILTER_VALIDATE_INT ); 
    120120    $row['label'] = stripslashes_deep( filter_var( $row['label'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
    121     $row['name']  = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
     121    $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); 
    122122    $row['value'] = stripslashes_deep( filter_var( $row['value'], FILTER_UNSAFE_RAW ) ); 
    123123 
     
    127127        array( 
    128128            'label' => $row['label'], 
    129             'name'  => $row['name'], 
     129            'name' => $row['name'], 
    130130            'value' => stripslashes( $row['value'] ) 
    131131        ), 
     
    167167    wp_enqueue_script( 'cop-import-export', COP_PLUGIN_URL . '/js/import-export.js', array( 'jquery', ), null, true ); 
    168168 
    169     $id    = ''; 
     169    $id = ''; 
    170170    $label = ''; 
    171     $name  = ''; 
     171    $name = ''; 
    172172    $value = ''; 
    173173 
     
    197197        $option = cop_get_option( $_GET['id'] ); 
    198198 
    199         $id    = $option->id; 
     199        $id = $option->id; 
    200200        $label = $option->label; 
    201         $name  = $option->name; 
     201        $name = $option->name; 
    202202        $value = $option->value; 
    203203 
     
    336336 
    337337 
    338 // Get your single option 
     338/** 
     339 * @param string $name 
     340 * 
     341 * @return bool|null|string 
     342 */ 
    339343function get_custom( $name ) { 
    340344    global $wpdb, $COP_TABLE; 
     
    347351} 
    348352 
    349 // Get your array options 
    350 function get_customs( $name ) { 
     353/** 
     354 * @param $name 
     355 * @param bool $collection (optional) 
     356 * 
     357 * @return array(string|int, string|int...) or array('label' => string|int, 'value' => string|int); 
     358 */ 
     359function get_customs( $name, $collection = false ) { 
    351360    global $wpdb, $COP_TABLE; 
    352361    if ( '' != $name ) : 
    353         $list  = $wpdb->get_results( $wpdb->prepare( "SELECT value FROM $COP_TABLE WHERE name = %s ", $name ), ARRAY_A ); 
     362        $list = $wpdb->get_results( $wpdb->prepare( "SELECT label, value FROM $COP_TABLE WHERE name = %s ", $name ), ARRAY_A ); 
    354363        $array = array(); 
    355364        foreach ( $list as $key => $name ) : 
    356             $array[] = $name['value']; 
     365 
     366            if ( $collection ) : 
     367                $array[] = array( 'label' => $name['label'], 'value' => $name['value'] ); 
     368            else : 
     369                $array[] = $name['value']; 
     370            endif; 
     371 
    357372        endforeach; 
    358373 
     
    363378} 
    364379 
    365  
    366380// Tutorial on Help Button 
    367381function cop_plugin_help( $contextual_help, $screen_id, $screen ) { 
     
    370384    if ( $screen_id == $my_plugin_hook ) { 
    371385 
    372         $contextual_help = '<br>Use <br /><code>' . htmlentities( '<?php echo get_custom(\'yourkey\') ; ?>' ) . '</code><br /><br /> or <br><code>' . htmlentities( '<?php foreach ( get_customs(\'yourkey\') as $name ) : ' ) . '<br />    echo $name; <br /> ' . htmlentities( 'endforeach; ?>' ) . '</code> <br /> in your theme.'; 
     386        $contextual_help = '<br>Use <br /><code>' . htmlentities( '<?php echo get_custom(\'yourkey\') ; ?>' ) . '</code><br /><br /> or <br><code>' . htmlentities( '<?php foreach ( get_customs(\'yourkey\') as $name ) : ' ) . '<br /> or <br><code>' . htmlentities( '<?php foreach ( get_customs(\'yourkey\', true) as $output ) : ' ) . '<br />    echo $output["name"] . " - " . $output["value"]; <br /> ' . htmlentities( 'endforeach; ?>' ) . '</code>ls <br /> in your theme.'; 
    373387    } 
    374388 
     
    382396function cop_export_data() { 
    383397    if ( ! wp_verify_nonce( $_REQUEST['security_cop_ajax_export'], 'cop_ajax_export_nonce' ) ) { 
    384         wp_send_json_error( array('message' => 'Access Denied!')); 
     398        wp_send_json_error( array( 'message' => 'Access Denied!' ) ); 
    385399    } 
    386400 
     
    398412 
    399413    if ( ! wp_verify_nonce( $_POST['security_cop_ajax_import'], 'cop_ajax_import_nonce' ) ) { 
    400         wp_send_json_error(array('message' => 'Access Denied!')); 
     414        wp_send_json_error( array( 'message' => 'Access Denied!' ) ); 
    401415    } 
    402416 
     
    411425    } 
    412426 
    413     $file_obj     = $_FILES['cop_file_import']; 
     427    $file_obj = $_FILES['cop_file_import']; 
    414428    $file_content = file_get_contents( $file_obj['tmp_name'] ); 
    415429 
     
    418432    foreach ( $file_data as $row ) { 
    419433        if ( ! isset( $row['label'] ) || ! isset( $row['name'] ) || ! isset( $row['value'] ) ) { 
    420             wp_send_json_error(array('message' => 'The JSON file is invalid')); 
     434            wp_send_json_error( array( 'message' => 'The JSON file is invalid' ) ); 
    421435        } 
    422436        cop_insert( $row ); 
  • custom-options-plus/trunk/readme.txt

    r1493670 r1518153  
    55Requires at least: 2.7 
    66Tested up to: 4.6 
    7 Stable tag: 1.7.1 
     7Stable tag: 1.8.0 
    88License: GPLv2 or later 
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    3838` 
    3939<?php 
     40    // Default get_customs returns a list(array) 
    4041    $array = get_customs('array_name'); 
    41     foreach ($array as $name) : 
     42    foreach ( $array as $name ) : 
    4243        echo $name; 
     44    endforeach; 
     45 
     46    // Second parameter set to true for get_customs returns a collection(array) with `label` and `value` 
     47    $array = get_customs('array_name', true); 
     48    foreach ( $array as $name ) : 
     49        echo $name['label'] . ' - '. $name['value']; 
    4350    endforeach; 
    4451?> 
     
    4754 
    4855== Changelog == 
     56= 1.8.0 = 
     57* get_customs() returns a collection (optional). Thanks @kas-cor 
     58 
    4959= 1.7.1 = 
    5060* Fixed array bug that generated errors in old PHP versions 
Note: See TracChangeset for help on using the changeset viewer.