- if ( is_object($value) )
- $value = wp_clone($value);
-
- $value = sanitize_option( $option, $value );
-
- // 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', 'options' );
- if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
- if ( false !== get_option( $option ) )
- return;
-
- $_value = $value;
- $value = maybe_serialize( $value );
- $autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
- do_action( 'add_option', $option, $_value );
- if ( ! defined( 'WP_INSTALLING' ) ) {
- if ( 'yes' == $autoload ) {
- $alloptions = wp_load_alloptions();
- $alloptions[$option] = $value;
- wp_cache_set( 'alloptions', $alloptions, 'options' );
- } else {
- wp_cache_set( $option, $value, 'options' );
- }
- }
-
- // This option exists now
- $notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
- if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
- unset( $notoptions[$option] );
- wp_cache_set( 'notoptions', $notoptions, 'options' );
- }
-
- $result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) );
-
- if ( $result ) {
- do_action( "add_option_{$option}", $option, $_value );
- do_action( 'added_option', $option, $_value );
- return true;
- }
- return false;
-}
-
-/**
- * Removes option by name. Prevents removal of protected WordPress options.
- *
- * @package WordPress
- * @subpackage Option
- * @since 1.2.0
- *
- * @uses do_action() Calls 'delete_option' hook before option is deleted.
- * @uses do_action() Calls 'deleted_option' and 'delete_option_$option' hooks on success.
- *
- * @param string $option Name of option to remove. Expected to not be SQL-escaped.
- * @return bool True, if option is successfully deleted. False on failure.
- */
-function delete_option( $option ) {
- global $wpdb;
-
- wp_protect_special_option( $option );
-
- // Get the ID, if no ID then return
- $row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
- if ( is_null( $row ) )
- return false;
- do_action( 'delete_option', $option );
- $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $option) );
- if ( ! defined( 'WP_INSTALLING' ) ) {
- if ( 'yes' == $row->autoload ) {
- $alloptions = wp_load_alloptions();
- if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
- unset( $alloptions[$option] );
- wp_cache_set( 'alloptions', $alloptions, 'options' );
- }
- } else {
- wp_cache_delete( $option, 'options' );
- }
- }
- if ( $result ) {
- do_action( "delete_option_$option", $option );
- do_action( 'deleted_option', $option );
- return true;
- }
- return false;
-}
-
-/**
- * Delete a transient
- *
- * @since 2.8.0
- * @package WordPress
- * @subpackage Transient
- *
- * @uses do_action() Calls 'delete_transient_$transient' hook before transient is deleted.
- * @uses do_action() Calls 'deleted_transient' hook on success.
- *
- * @param string $transient Transient name. Expected to not be SQL-escaped.
- * @return bool true if successful, false otherwise
- */
-function delete_transient( $transient ) {
- global $_wp_using_ext_object_cache;
-
- do_action( 'delete_transient_' . $transient, $transient );
-
- if ( $_wp_using_ext_object_cache ) {
- $result = wp_cache_delete( $transient, 'transient' );
- } else {
- $option_timeout = '_transient_timeout_' . $transient;
- $option = '_transient_' . $transient;
- $result = delete_option( $option );
- if ( $result )
- delete_option( $option_timeout );
- }
-
- if ( $result )
- do_action( 'deleted_transient', $transient );
- return $result;
-}
-
-/**
- * Get the value of a transient
- *
- * If the transient does not exist or does not have a value, then the return value
- * will be false.
- *
- * @uses apply_filters() Calls 'pre_transient_$transient' hook before checking the transient.
- * Any value other than false will "short-circuit" the retrieval of the transient
- * and return the returned value.
- * @uses apply_filters() Calls 'transient_$option' hook, after checking the transient, with
- * the transient value.
- *
- * @since 2.8.0
- * @package WordPress
- * @subpackage Transient
- *
- * @param string $transient Transient name. Expected to not be SQL-escaped
- * @return mixed Value of transient
- */
-function get_transient( $transient ) {
- global $_wp_using_ext_object_cache;
-
- $pre = apply_filters( 'pre_transient_' . $transient, false );
- if ( false !== $pre )
- return $pre;
-
- if ( $_wp_using_ext_object_cache ) {
- $value = wp_cache_get( $transient, 'transient' );
- } else {
- $transient_option = '_transient_' . $transient;
- if ( ! defined( 'WP_INSTALLING' ) ) {
- // If option is not in alloptions, it is not autoloaded and thus has a timeout
- $alloptions = wp_load_alloptions();
- if ( !isset( $alloptions[$transient_option] ) ) {
- $transient_timeout = '_transient_timeout_' . $transient;
- if ( get_option( $transient_timeout ) < time() ) {
- delete_option( $transient_option );
- delete_option( $transient_timeout );
- return false;
- }
- }
- }
-
- $value = get_option( $transient_option );
- }
-
- return apply_filters( 'transient_' . $transient, $value );
-}
-
-/**
- * Set/update the value of a transient
- *
- * You do not need to serialize values. If the value needs to be serialized, then
- * it will be serialized before it is set.
- *
- * @since 2.8.0
- * @package WordPress
- * @subpackage Transient
- *
- * @uses apply_filters() Calls 'pre_set_transient_$transient' hook to allow overwriting the
- * transient value to be stored.
- * @uses do_action() Calls 'set_transient_$transient' and 'setted_transient' hooks on success.
- *
- * @param string $transient Transient name. Expected to not be SQL-escaped.
- * @param mixed $value Transient value. Expected to not be SQL-escaped.
- * @param int $expiration Time until expiration in seconds, default 0
- * @return bool False if value was not set and true if value was set.
- */
-function set_transient( $transient, $value, $expiration = 0 ) {
- global $_wp_using_ext_object_cache;
-
- $value = apply_filters( 'pre_set_transient_' . $transient, $value );
-
- if ( $_wp_using_ext_object_cache ) {
- $result = wp_cache_set( $transient, $value, 'transient', $expiration );
- } else {
- $transient_timeout = '_transient_timeout_' . $transient;
- $transient = '_transient_' . $transient;
- if ( false === get_option( $transient ) ) {
- $autoload = 'yes';
- if ( $expiration ) {
- $autoload = 'no';
- add_option( $transient_timeout, time() + $expiration, '', 'no' );
- }
- $result = add_option( $transient, $value, '', $autoload );
- } else {
- if ( $expiration )
- update_option( $transient_timeout, time() + $expiration );
- $result = update_option( $transient, $value );
- }
- }
- if ( $result ) {
- do_action( 'set_transient_' . $transient );
- do_action( 'setted_transient', $transient );
- }
- return $result;
-}
-
-/**
- * Saves and restores user interface settings stored in a cookie.
- *
- * Checks if the current user-settings cookie is updated and stores it. When no
- * cookie exists (different browser used), adds the last saved cookie restoring
- * the settings.
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.7.0
- */
-function wp_user_settings() {
-
- if ( ! is_admin() )
- return;
-
- if ( defined('DOING_AJAX') )
- return;
-
- if ( ! $user = wp_get_current_user() )
- return;
-
- $settings = get_user_option( 'user-settings', $user->ID );
-
- if ( isset( $_COOKIE['wp-settings-' . $user->ID] ) ) {
- $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
-
- if ( ! empty( $cookie ) && strpos( $cookie, '=' ) ) {
- if ( $cookie == $settings )
- return;
-
- $last_time = (int) get_user_option( 'user-settings-time', $user->ID );
- $saved = isset( $_COOKIE['wp-settings-time-' . $user->ID]) ? preg_replace( '/[^0-9]/', '', $_COOKIE['wp-settings-time-' . $user->ID] ) : 0;
-
- if ( $saved > $last_time ) {
- update_user_option( $user->ID, 'user-settings', $cookie, false );
- update_user_option( $user->ID, 'user-settings-time', time() - 5, false );
- return;
- }
- }
- }
-
- setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
- setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );
- $_COOKIE['wp-settings-' . $user->ID] = $settings;
-}
-
-/**
- * Retrieve user interface setting value based on setting name.
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.7.0
- *
- * @param string $name The name of the setting.
- * @param string $default Optional default value to return when $name is not set.
- * @return mixed the last saved user setting or the default value/false if it doesn't exist.
- */
-function get_user_setting( $name, $default = false ) {
-
- $all = get_all_user_settings();
-
- return isset($all[$name]) ? $all[$name] : $default;
-}
-
-/**
- * Add or update user interface setting.
- *
- * Both $name and $value can contain only ASCII letters, numbers and underscores.
- * This function has to be used before any output has started as it calls setcookie().
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.8.0
- *
- * @param string $name The name of the setting.
- * @param string $value The value for the setting.
- * @return bool true if set successfully/false if not.
- */
-function set_user_setting( $name, $value ) {
-
- if ( headers_sent() )
- return false;
-
- $all = get_all_user_settings();
- $name = preg_replace( '/[^A-Za-z0-9_]+/', '', $name );
-
- if ( empty($name) )
- return false;
-
- $all[$name] = $value;
-
- return wp_set_all_user_settings($all);
-}
-
-/**
- * Delete user interface settings.
- *
- * Deleting settings would reset them to the defaults.
- * This function has to be used before any output has started as it calls setcookie().
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.7.0
- *
- * @param mixed $names The name or array of names of the setting to be deleted.
- * @return bool true if deleted successfully/false if not.
- */
-function delete_user_setting( $names ) {
-
- if ( headers_sent() )
- return false;
-
- $all = get_all_user_settings();
- $names = (array) $names;
-
- foreach ( $names as $name ) {
- if ( isset($all[$name]) ) {
- unset($all[$name]);
- $deleted = true;
- }
- }
-
- if ( isset($deleted) )
- return wp_set_all_user_settings($all);
-
- return false;
-}
-
-/**
- * Retrieve all user interface settings.
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.7.0
- *
- * @return array the last saved user settings or empty array.
- */
-function get_all_user_settings() {
- global $_updated_user_settings;
-
- if ( ! $user = wp_get_current_user() )
- return array();
-
- if ( isset($_updated_user_settings) && is_array($_updated_user_settings) )
- return $_updated_user_settings;
-
- $all = array();
- if ( isset($_COOKIE['wp-settings-' . $user->ID]) ) {
- $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['wp-settings-' . $user->ID] );
-
- if ( $cookie && strpos($cookie, '=') ) // the '=' cannot be 1st char
- parse_str($cookie, $all);
-
- } else {
- $option = get_user_option('user-settings', $user->ID);
- if ( $option && is_string($option) )
- parse_str( $option, $all );
- }
-
- return $all;
-}
-
-/**
- * Private. Set all user interface settings.
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.8.0
- *
- * @param unknown $all
- * @return bool
- */
-function wp_set_all_user_settings($all) {
- global $_updated_user_settings;
-
- if ( ! $user = wp_get_current_user() )
- return false;
-
- $_updated_user_settings = $all;
- $settings = '';
- foreach ( $all as $k => $v ) {
- $v = preg_replace( '/[^A-Za-z0-9_]+/', '', $v );
- $settings .= $k . '=' . $v . '&';
- }
-
- $settings = rtrim($settings, '&');
-
- update_user_option( $user->ID, 'user-settings', $settings, false );
- update_user_option( $user->ID, 'user-settings-time', time(), false );
-
- return true;
-}
-
-/**
- * Delete the user settings of the current user.
- *
- * @package WordPress
- * @subpackage Option
- * @since 2.7.0
- */
-function delete_all_user_settings() {
- if ( ! $user = wp_get_current_user() )
- return;
-
- update_user_option( $user->ID, 'user-settings', '', false );
- setcookie('wp-settings-' . $user->ID, ' ', time() - 31536000, SITECOOKIEPATH);
-}
-
-/**
- * Serialize data, if needed.
- *
- * @since 2.0.5
- *
- * @param mixed $data Data that might be serialized.
- * @return mixed A scalar data
- */
-function maybe_serialize( $data ) {
- if ( is_array( $data ) || is_object( $data ) )
- return serialize( $data );
-
- if ( is_serialized( $data ) )
- return serialize( $data );
-
- return $data;
-}
-
-/**
- * Retrieve post title from XMLRPC XML.
- *
- * If the title element is not part of the XML, then the default post title from
- * the $post_default_title will be used instead.
- *
- * @package WordPress
- * @subpackage XMLRPC
- * @since 0.71
- *
- * @global string $post_default_title Default XMLRPC post title.
- *
- * @param string $content XMLRPC XML Request content
- * @return string Post title
- */
-function xmlrpc_getposttitle( $content ) {
- global $post_default_title;
- if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) {
- $post_title = $matchtitle[1];
- } else {
- $post_title = $post_default_title;
- }
- return $post_title;
-}
-
-/**
- * Retrieve the post category or categories from XMLRPC XML.
- *
- * If the category element is not found, then the default post category will be
- * used. The return type then would be what $post_default_category. If the
- * category is found, then it will always be an array.
- *
- * @package WordPress
- * @subpackage XMLRPC
- * @since 0.71
- *
- * @global string $post_default_category Default XMLRPC post category.
- *
- * @param string $content XMLRPC XML Request content
- * @return string|array List of categories or category name.
- */
-function xmlrpc_getpostcategory( $content ) {
- global $post_default_category;
- if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) {
- $post_category = trim( $matchcat[1], ',' );
- $post_category = explode( ',', $post_category );
- } else {
- $post_category = $post_default_category;
- }
- return $post_category;
-}
-
-/**
- * XMLRPC XML content without title and category elements.
- *
- * @package WordPress
- * @subpackage XMLRPC
- * @since 0.71
- *
- * @param string $content XMLRPC XML Request content
- * @return string XMLRPC XML Request content without title and category elements.
- */
-function xmlrpc_removepostdata( $content ) {
- $content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content );
- $content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content );
- $content = trim( $content );
- return $content;
-}
-
-/**
- * Open the file handle for debugging.
- *
- * This function is used for XMLRPC feature, but it is general purpose enough
- * to be used in anywhere.
- *
- * @see fopen() for mode options.
- * @package WordPress
- * @subpackage Debug
- * @since 0.71
- * @uses $debug Used for whether debugging is enabled.
- *
- * @param string $filename File path to debug file.
- * @param string $mode Same as fopen() mode parameter.
- * @return bool|resource File handle. False on failure.
- */
-function debug_fopen( $filename, $mode ) {
- global $debug;
- if ( 1 == $debug ) {
- $fp = fopen( $filename, $mode );
- return $fp;
- } else {
- return false;
- }
-}
-
-/**
- * Write contents to the file used for debugging.
- *
- * Technically, this can be used to write to any file handle when the global
- * $debug is set to 1 or true.
- *
- * @package WordPress
- * @subpackage Debug
- * @since 0.71
- * @uses $debug Used for whether debugging is enabled.
- *
- * @param resource $fp File handle for debugging file.
- * @param string $string Content to write to debug file.
- */
-function debug_fwrite( $fp, $string ) {
- global $debug;
- if ( 1 == $debug )
- fwrite( $fp, $string );
-}
-
-/**
- * Close the debugging file handle.
- *
- * Technically, this can be used to close any file handle when the global $debug
- * is set to 1 or true.
- *
- * @package WordPress
- * @subpackage Debug
- * @since 0.71
- * @uses $debug Used for whether debugging is enabled.
- *
- * @param resource $fp Debug File handle.
- */
-function debug_fclose( $fp ) {
- global $debug;
- if ( 1 == $debug )
- fclose( $fp );
-}
-
-/**
- * Check content for video and audio links to add as enclosures.
- *
- * Will not add enclosures that have already been added and will
- * remove enclosures that are no longer in the post. This is called as
- * pingbacks and trackbacks.
- *
- * @package WordPress
- * @since 1.5.0
- *
- * @uses $wpdb
- *
- * @param string $content Post Content
- * @param int $post_ID Post ID
- */
-function do_enclose( $content, $post_ID ) {
- global $wpdb;
- include_once( ABSPATH . WPINC . '/class-IXR.php' );
-
- $log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
- $post_links = array();
- debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" );
-
- $pung = get_enclosed( $post_ID );
-
- $ltrs = '\w';
- $gunk = '/#~:.?+=&%@!\-';
- $punc = '.:?\-';
- $any = $ltrs . $gunk . $punc;
-
- preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
-
- debug_fwrite( $log, 'Post contents:' );
- debug_fwrite( $log, $content . "\n" );
-
- foreach ( $pung as $link_test ) {
- if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
- $mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
- do_action( 'delete_postmeta', $mid );
- $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
- do_action( 'deleted_postmeta', $mid );
- }
- }
-
- foreach ( (array) $post_links_temp[0] as $link_test ) {
- if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
- $test = @parse_url( $link_test );
- if ( false === $test )
- continue;
- if ( isset( $test['query'] ) )
- $post_links[] = $link_test;
- elseif ( $test['path'] != '/' && $test['path'] != '' )
- $post_links[] = $link_test;
- }
- }
-
- foreach ( (array) $post_links as $url ) {
- if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) {
-
- if ( $headers = wp_get_http_headers( $url) ) {
- $len = (int) $headers['content-length'];
- $type = $headers['content-type'];
- $allowed_types = array( 'video', 'audio' );
-
- // Check to see if we can figure out the mime type from
- // the extension
- $url_parts = @parse_url( $url );
- if ( false !== $url_parts ) {
- $extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
- if ( !empty( $extension ) ) {
- foreach ( get_allowed_mime_types( ) as $exts => $mime ) {
- if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
- $type = $mime;
- break;
- }
- }
- }
- }
-
- if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
- $meta_value = "$url\n$len\n$type\n";
- $wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
- do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
- }
- }
- }
- }
-}