From 9d71a461c6693e6a1e6e08fa73cacef6a573b68e Mon Sep 17 00:00:00 2001 From: Leo Caseiro Date: Thu, 20 Oct 2016 10:09:40 +1100 Subject: [PATCH] get_customs outputs array whit keys 'label' and 'value' from @kas-cor (#20) * Update README.md * Update custom-options-plus.php * Update README.md * Update custom-options-plus.php * Update custom-options-plus.php * feature(get_customs): return collection as optional - Release 1.8.0 --- README.md | 13 ++++++++- custom-options-plus.php | 58 +++++++++++++++++++++++++---------------- readme.txt | 14 ++++++++-- 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 978f387..d02feab 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,17 @@ FAQ (Frequently Asked Questions) **Used to multiples options** ```php ``` @@ -52,6 +59,10 @@ FAQ (Frequently Asked Questions) Changelog -------------- +**1.8.0** + +* get_customs() returns a collection (optional). Thanks @kas-cor + **1.7.1** * Fixed array bug that generated errors in old PHP versions. Thanks @AdvancedStyle diff --git a/custom-options-plus.php b/custom-options-plus.php index 4c375a0..b4f8a00 100644 --- a/custom-options-plus.php +++ b/custom-options-plus.php @@ -5,7 +5,7 @@ Description: The easiest way to add your custom variables as a Settings Page for your Theme. Even with no expertise in PHP. You 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. You can also enter the login of your social networks. How to login twitter, Facebook, Youtube, contact email and more. -Version: 1.7.1 +Version: 1.8.0 Author: Leo Caseiro Author URI: http://leocaseiro.com.br/ */ @@ -40,7 +40,7 @@ // Added on 1.5 define( 'COP_OPTIONS_PREFIX', 'cop_' ); -define( 'COP_PLUGIN_VERSION', '1.7.0' ); +define( 'COP_PLUGIN_VERSION', '1.8.0' ); global $wpdb, $COP_TABLE; define( 'COP_TABLE', $wpdb->prefix . 'custom_options_plus' ); @@ -98,14 +98,14 @@ function cop_insert( $row ) { global $wpdb; $row['label'] = stripslashes_deep( filter_var( $row['label'], FILTER_SANITIZE_SPECIAL_CHARS ) ); - $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); + $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); $row['value'] = stripslashes_deep( filter_var( $row['value'], FILTER_UNSAFE_RAW ) ); return $wpdb->insert( COP_TABLE, array( 'label' => $row['label'], - 'name' => $row['name'], + 'name' => $row['name'], 'value' => stripslashes( $row['value'] ) ), array( '%s', '%s', '%s' ) @@ -116,9 +116,9 @@ function cop_insert( $row ) { function cop_update( $row ) { global $wpdb; - $row['id'] = filter_var( $row['id'], FILTER_VALIDATE_INT ); + $row['id'] = filter_var( $row['id'], FILTER_VALIDATE_INT ); $row['label'] = stripslashes_deep( filter_var( $row['label'], FILTER_SANITIZE_SPECIAL_CHARS ) ); - $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); + $row['name'] = stripslashes_deep( filter_var( $row['name'], FILTER_SANITIZE_SPECIAL_CHARS ) ); $row['value'] = stripslashes_deep( filter_var( $row['value'], FILTER_UNSAFE_RAW ) ); @@ -126,7 +126,7 @@ function cop_update( $row ) { COP_TABLE, array( 'label' => $row['label'], - 'name' => $row['name'], + 'name' => $row['name'], 'value' => stripslashes( $row['value'] ) ), array( 'id' => $row['id'] ), @@ -166,9 +166,9 @@ function custom_options_plus_adm() { wp_enqueue_script( 'copFunctions', COP_PLUGIN_URL . '/js/functions.js', array( 'stringToSlug' ) ); wp_enqueue_script( 'cop-import-export', COP_PLUGIN_URL . '/js/import-export.js', array( 'jquery', ), null, true ); - $id = ''; + $id = ''; $label = ''; - $name = ''; + $name = ''; $value = ''; $message = ''; @@ -196,9 +196,9 @@ function custom_options_plus_adm() { $option = cop_get_option( $_GET['id'] ); - $id = $option->id; + $id = $option->id; $label = $option->label; - $name = $option->name; + $name = $option->name; $value = $option->value; endif; @@ -335,7 +335,11 @@ class="button-primary hidden" value=""/> } -// Get your single option +/** + * @param string $name + * + * @return bool|null|string + */ function get_custom( $name ) { global $wpdb, $COP_TABLE; @@ -346,14 +350,25 @@ function get_custom( $name ) { endif; } -// Get your array options -function get_customs( $name ) { +/** + * @param $name + * @param bool $collection (optional) + * + * @return array(string|int, string|int...) or array('label' => string|int, 'value' => string|int); + */ +function get_customs( $name, $collection = false ) { global $wpdb, $COP_TABLE; if ( '' != $name ) : - $list = $wpdb->get_results( $wpdb->prepare( "SELECT value FROM $COP_TABLE WHERE name = %s ", $name ), ARRAY_A ); + $list = $wpdb->get_results( $wpdb->prepare( "SELECT label, value FROM $COP_TABLE WHERE name = %s ", $name ), ARRAY_A ); $array = array(); foreach ( $list as $key => $name ) : - $array[] = $name['value']; + + if ( $collection ) : + $array[] = array( 'label' => $name['label'], 'value' => $name['value'] ); + else : + $array[] = $name['value']; + endif; + endforeach; return $array; @@ -362,14 +377,13 @@ function get_customs( $name ) { endif; } - // Tutorial on Help Button function cop_plugin_help( $contextual_help, $screen_id, $screen ) { global $my_plugin_hook; if ( $screen_id == $my_plugin_hook ) { - $contextual_help = '
Use
' . htmlentities( '' ) . '

or
' . htmlentities( ' echo $name;
' . htmlentities( 'endforeach; ?>' ) . '

in your theme.'; + $contextual_help = '
Use
' . htmlentities( '' ) . '

or
' . htmlentities( ' or
' . htmlentities( ' echo $output["name"] . " - " . $output["value"];
' . htmlentities( 'endforeach; ?>' ) . '

in your theme.'; } return $contextual_help; @@ -381,7 +395,7 @@ function cop_plugin_help( $contextual_help, $screen_id, $screen ) { // Ajax Export Data (Added on 1.7) function cop_export_data() { if ( ! wp_verify_nonce( $_REQUEST['security_cop_ajax_export'], 'cop_ajax_export_nonce' ) ) { - wp_send_json_error( array('message' => 'Access Denied!')); + wp_send_json_error( array( 'message' => 'Access Denied!' ) ); } header( 'Content-type: application/json' ); @@ -397,7 +411,7 @@ function cop_import_data() { global $wpdb, $COP_TABLE; if ( ! wp_verify_nonce( $_POST['security_cop_ajax_import'], 'cop_ajax_import_nonce' ) ) { - wp_send_json_error(array('message' => 'Access Denied!')); + wp_send_json_error( array( 'message' => 'Access Denied!' ) ); } $truncate_table = filter_var( $_POST['clear-table'], FILTER_VALIDATE_BOOLEAN ); @@ -410,14 +424,14 @@ function cop_import_data() { $wpdb->query( "TRUNCATE TABLE $COP_TABLE" ); } - $file_obj = $_FILES['cop_file_import']; + $file_obj = $_FILES['cop_file_import']; $file_content = file_get_contents( $file_obj['tmp_name'] ); $file_data = json_decode( $file_content, true ); foreach ( $file_data as $row ) { if ( ! isset( $row['label'] ) || ! isset( $row['name'] ) || ! isset( $row['value'] ) ) { - wp_send_json_error(array('message' => 'The JSON file is invalid')); + wp_send_json_error( array( 'message' => 'The JSON file is invalid' ) ); } cop_insert( $row ); } diff --git a/readme.txt b/readme.txt index 0536ec9..c7c2e58 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: http://leocaseiro.com.br/contato/ Tags: configs, custom, custom configs, custom options, custom options plus, custom settings, leocaseiro, options, settings, wp_options Requires at least: 2.7 Tested up to: 4.6 -Stable tag: 1.7.1 +Stable tag: 1.8.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -37,15 +37,25 @@ This Plugin was Based on Custom Settings (Custom Configs) which has been removal = Used to multiples options = ` ` == Changelog == += 1.8.0 = +* get_customs() returns a collection (optional). Thanks @kas-cor + = 1.7.1 = * Fixed array bug that generated errors in old PHP versions