WordPressを読む 11-6 /blog/wp-includes/option.php 6
2014/11/29
目次
/blog/wp-includes/option.php 6
読込元 : /blog/wp-includes/functions.php 1
関数
add_site_option()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /** * Add a new site option. * * Existing options will not be updated. Note that prior to 3.3 this wasn't the case. * * @since 2.8.0 * * @see add_option() * * @param string $option Name of option to add. Expected to not be SQL-escaped. * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. * @return bool False if option was not added and true if option was added. */ function add_site_option( $option, $value ) { global $wpdb; wp_protect_special_option( $option ); /** * Filter the value of a specific site option before it is added. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 2.9.0 As 'pre_add_site_option_' . $key * @since 3.0.0 * * @param mixed $value Value of site option. */ $value = apply_filters( 'pre_add_site_option_' . $option, $value ); $notoptions_key = "{$wpdb->siteid}:notoptions"; if ( !is_multisite() ) { $result = add_option( $option, $value ); } else { $cache_key = "{$wpdb->siteid}:$option"; // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); if ( ! is_array( $notoptions ) || ! isset( $notoptions[$option] ) ) if ( false !== get_site_option( $option ) ) return false; $value = sanitize_option( $option, $value ); $serialized_value = maybe_serialize( $value ); $result = $wpdb->insert( $wpdb->sitemeta, array('site_id' => $wpdb->siteid, 'meta_key' => $option, 'meta_value' => $serialized_value ) ); if ( ! $result ) return false; wp_cache_set( $cache_key, $value, 'site-options' ); // This option exists now $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { unset( $notoptions[$option] ); wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); } } if ( $result ) { /** * Fires after a specific site option has been successfully added. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 2.9.0 As "add_site_option_{$key}" * @since 3.0.0 * * @param string $option Name of site option. * @param mixed $value Value of site option. */ do_action( "add_site_option_{$option}", $option, $value ); /** * Fires after a site option has been successfully added. * * @since 3.0.0 * * @param string $option Name of site option. * @param mixed $value Value of site option. */ do_action( "add_site_option", $option, $value ); return true; } return false; } |
関数
delete_site_option()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /** * Removes site option by name. * * @since 2.8.0 * * @see delete_option() * * @param string $option Name of option to remove. Expected to not be SQL-escaped. * @return bool True, if succeed. False, if failure. */ function delete_site_option( $option ) { global $wpdb; // ms_protect_special_option( $option ); @todo /** * Fires immediately before a specific site option is deleted. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 3.0.0 */ do_action( 'pre_delete_site_option_' . $option ); if ( !is_multisite() ) { $result = delete_option( $option ); } else { $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) ); if ( is_null( $row ) || !$row->meta_id ) return false; $cache_key = "{$wpdb->siteid}:$option"; wp_cache_delete( $cache_key, 'site-options' ); $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $wpdb->siteid ) ); } if ( $result ) { /** * Fires after a specific site option has been deleted. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 2.9.0 As "delete_site_option_{$key}" * @since 3.0.0 * * @param string $option Name of the site option. */ do_action( "delete_site_option_{$option}", $option ); /** * Fires after a site option has been deleted. * * @since 3.0.0 * * @param string $option Name of the site option. */ do_action( "delete_site_option", $option ); return true; } return false; } |
関数
update_site_option()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /** * Update the value of a site option that was already added. * * @since 2.8.0 * * @see update_option() * * @param string $option Name of option. Expected to not be SQL-escaped. * @param mixed $value Option value. Expected to not be SQL-escaped. * @return bool False if value was not updated and true if value was updated. */ function update_site_option( $option, $value ) { global $wpdb; wp_protect_special_option( $option ); $old_value = get_site_option( $option ); /** * Filter a specific site option before its value is updated. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 2.9.0 As 'pre_update_site_option_' . $key * @since 3.0.0 * * @param mixed $value New value of site option. * @param mixed $old_value Old value of site option. */ $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value ); if ( $value === $old_value ) return false; if ( false === $old_value ) return add_site_option( $option, $value ); $notoptions_key = "{$wpdb->siteid}:notoptions"; $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) { unset( $notoptions[$option] ); wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); } if ( !is_multisite() ) { $result = update_option( $option, $value ); } else { $value = sanitize_option( $option, $value ); $serialized_value = maybe_serialize( $value ); $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $wpdb->siteid, 'meta_key' => $option ) ); if ( $result ) { $cache_key = "{$wpdb->siteid}:$option"; wp_cache_set( $cache_key, $value, 'site-options' ); } } if ( $result ) { /** * Fires after the value of a specific site option has been successfully updated. * * The dynamic portion of the hook name, $option, refers to the option name. * * @since 2.9.0 As "update_site_option_{$key}" * @since 3.0.0 * * @param string $option Name of site option. * @param mixed $value Current value of site option. * @param mixed $old_value Old value of site option. */ do_action( "update_site_option_{$option}", $option, $value, $old_value ); /** * Fires after the value of a site option has been successfully updated. * * @since 3.0.0 * * @param string $option Name of site option. * @param mixed $value Current value of site option. * @param mixed $old_value Old value of site option. */ do_action( "update_site_option", $option, $value, $old_value ); return true; } return false; } |