├── README.md ├── composer.json └── wp-large-options.php /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/voceconnect/wp-large-options/d00531e665490a6fdb8a14916a790d062f39470c/README.md -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "voceconnect/wp-large-options", 3 | "description": "You may wish to store a larger option value than is recommended on WordPress.com. If your option data will exceed 400K, or is of an unpredictable size (such as an HTML fragment etc.) you should use the wp_large_options plugin to store the option in a cache-safe manner. Failure to do this could result in the option not being cached, and instead fetched repeatedly from the DB, which could cause performance problems.", 4 | "license": "GPL-2.0+", 5 | "type": "library", 6 | "authors": [ 7 | { 8 | "name": "Mike Pretty", 9 | "email": "mpretty@voceconnect.com" 10 | } 11 | ], 12 | "require": { 13 | "composer/installers": "~1.0" 14 | }, 15 | "autoload": { 16 | "files": ["wp-large-options.php"] 17 | } 18 | } -------------------------------------------------------------------------------- /wp-large-options.php: -------------------------------------------------------------------------------- 1 | 13 | 14 | This program is free software; you can redistribute it and/or modify 15 | it under the terms of the GNU General Public License as published by 16 | the Free Software Foundation; either version 2 of the License, or 17 | (at your option) any later version. 18 | 19 | This program is distributed in the hope that it will be useful, 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | GNU General Public License for more details. 23 | 24 | You should have received a copy of the GNU General Public License 25 | along with this program; if not, write to the Free Software 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 27 | 28 | */ 29 | 30 | define( 'WLO_POST_TYPE', 'wlo_option' ); 31 | define( 'WLO_META_KEY', 'wp-large-option-value'); 32 | 33 | /** 34 | * Add a new option. 35 | * @param string $option 36 | * @param mixed $value 37 | * @return boolean 38 | */ 39 | function wlo_add_option( $option, $value ) { 40 | $option = wlo_get_option_name( $option ); 41 | if ( empty( $option ) ) 42 | return false; 43 | 44 | if ( false !== wlo_get_option( $option ) ) { 45 | return false; 46 | } 47 | 48 | $post = array( 49 | 'post_type' => WLO_POST_TYPE, 50 | 'post_name' => $option, 51 | 'post_title' => $option, 52 | 'post_status' => 'publish', 53 | ); 54 | 55 | $post_id = wp_insert_post( $post ); 56 | 57 | if ( !is_wp_error( $post_id ) && update_post_meta($post_id, WLO_META_KEY, $value) ) { 58 | wp_cache_set( 'wlo_option_id_' . $option, $post_id ); 59 | do_action( "add_wlo_option_{$option}", $option, $value ); 60 | do_action( 'added_wlo_option', $option, $value ); 61 | return true; 62 | } 63 | return false; 64 | } 65 | 66 | /** 67 | * Update or add an option 68 | * @param string $option 69 | * @param mixed $newvalue 70 | * @return boolean 71 | */ 72 | function wlo_update_option( $option, $newvalue ) { 73 | $option = wlo_get_option_name( $option ); 74 | if ( empty( $option ) ) 75 | return false; 76 | 77 | $oldvalue = wlo_get_option( $option ); 78 | 79 | // If the new and old values are the same, no need to update. 80 | if ( $newvalue === $oldvalue ) 81 | return false; 82 | 83 | if ( false === $oldvalue ) 84 | return wlo_add_option( $option, $newvalue ); 85 | 86 | $post_id = wlo_get_option_post_id( $option ); 87 | 88 | if ( update_post_meta($post_id, WLO_META_KEY, $newvalue) ) { 89 | do_action( "update_wlo_option_{$option}", $oldvalue, $newvalue ); 90 | do_action( 'updated_wlo_option', $option, $oldvalue, $newvalue ); 91 | return true; 92 | } 93 | return false; 94 | } 95 | 96 | /** 97 | * Deletes the option 98 | * @param string $option 99 | * @return boolean 100 | */ 101 | function wlo_delete_option( $option ) { 102 | $option = wlo_get_option_name( $option ); 103 | if ( empty( $option ) ) 104 | return false; 105 | 106 | if ( $post_id = wlo_get_option_post_id( $option ) ) { 107 | return wp_delete_post( $post_id, true ); 108 | } 109 | return false; 110 | } 111 | 112 | /** 113 | * Returns the option 114 | * @param string $option 115 | * @param mixed $default 116 | * @return mixed 117 | */ 118 | function wlo_get_option( $option, $default = false ) { 119 | $option = wlo_get_option_name( $option ); 120 | if ( empty( $option ) ) 121 | return false; 122 | 123 | if ( defined( 'WP_SETUP_CONFIG' ) ) 124 | return false; 125 | 126 | if ( !( $post_id = wlo_get_option_post_id( $option ) ) ) 127 | return $default; 128 | 129 | if ( false === ($value = get_post_meta( $post_id, WLO_META_KEY, true )) ) { 130 | return $default; 131 | } 132 | return $value; 133 | } 134 | 135 | /** 136 | * Returns the post that is storing the specific option 137 | * @param string $option 138 | * @return bool|object 139 | */ 140 | function wlo_get_option_post_id( $option ) { 141 | $option = wlo_get_option_name( $option ); 142 | if ( false === ($post_id = wp_cache_get( 'wlo_option_id_' . $option ) ) ) { 143 | $posts = get_posts( array( 144 | 'post_type' => WLO_POST_TYPE, 145 | 'posts_per_page' => 1, 146 | 'name' => $option, 147 | 'fields' => 'ids' 148 | ) ); 149 | 150 | if ( count( $posts ) === 1 ) { 151 | $post_id = $posts[0]; 152 | wp_cache_set('wlo_option_id_' . $option, $post_id); 153 | } 154 | } 155 | 156 | return $post_id; 157 | } 158 | 159 | function wlo_get_option_name( $option ) { 160 | $option = trim( $option ); 161 | 162 | $option = sanitize_title( $option ); 163 | 164 | if ( empty( $option ) ) 165 | return false; 166 | 167 | return $option; 168 | } 169 | 170 | 171 | add_action( 'init', function() { 172 | register_post_type( WLO_POST_TYPE, array( 173 | 'labels' => array( 174 | 'name' => 'Large Options', 175 | 'singular_name' => 'Large Option' 176 | ), 177 | 'publicly_queryable' => false, 178 | 'capability_type' => 'wlo_debug', 179 | 'public' => false, 180 | 'exclude_from_search' => true, 181 | 'rewrite' => false, 182 | 'has_archive' => false, 183 | 'query_var' => false, 184 | 'taxonomies' => array( ), 185 | 'show_ui' => false, 186 | 'can_export' => true, 187 | 'show_in_nav_menus' => false, 188 | 'show_in_menu' => false, 189 | 'show_in_admin_bar' => false, 190 | 'delete_with_user' => false, 191 | ) ); 192 | }, 1 ); 193 | --------------------------------------------------------------------------------