type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) { } /** * Get users of an object * * @since 2.6.0 bbPress (r6722) * * @param int $object_id The object id * @param string $meta_key The key used to index this relationship * @param string $meta_type The type of meta to look in * * @return array Returns ids of users */ public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { } /** * Get the part of the query responsible for JOINing objects to relationships. * * @since 2.6.0 bbPress (r6737) * * @param array $args * @param string $meta_key * @param string $meta_type * * @return array */ public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) { } } /** * Meta strategy for interfacing with User Engagements * * @since 2.6.0 bbPress (r6722) */ class BBP_User_Engagements_Meta extends BBP_User_Engagements_Base { /** * Type of strategy being used. * * @since 2.6.0 bbPress (r6737) * * @var string */ public $type = 'meta'; /** * Add a user id to an object * * @since 2.6.0 bbPress (r6722) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * @param bool $unique Whether meta key should be unique to the object * * @return bool Returns true on success, false on failure */ public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) { return add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique ); } /** * Remove a user id from an object * * @since 2.6.0 bbPress (r6722) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { return delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false ); } /** * Remove a user id from all objects * * @since 2.6.0 bbPress (r6722) * * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) { return delete_metadata( $meta_type, null, $meta_key, $user_id, true ); } /** * Remove an object from all users * * @since 2.6.0 bbPress (r6722) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { return delete_metadata( $meta_type, $object_id, $meta_key, null, false ); } /** * Remove all users from all objects * * @since 2.6.0 bbPress (r6722) * * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) { return delete_metadata( $meta_type, null, $meta_key, null, true ); } /** * Get users of an object * * @since 2.6.0 bbPress (r6722) * * @param int $object_id The object id * @param string $meta_key The key used to index this relationship * @param string $meta_type The type of meta to look in option keys. * * @since 2.6.0 bbPress (r6844) * * @param string $meta_key * @param int $object_id * @param bool $prefix * * @return string */ private function get_user_option_key( $meta_key = '', $object_id = 0, $prefix = false ) { switch ( $meta_key ) { // Favorites case '_bbp_favorite' : $key = '_bbp_favorites'; break; // Subscriptions case '_bbp_subscription' : // Maybe guess at post type $post_type = ! empty( $object_id ) ? get_post_type( $object_id ) : bbp_get_topic_post_type(); // Forums & Topics used different keys :/ $key = ( bbp_get_forum_post_type() === $post_type ) ? '_bbp_forum_subscriptions' : '_bbp_subscriptions'; break; // Unknown, so pluralize default : $key = "{$meta_key}s"; break; } // Maybe prefix the key (for use in raw database queries) if ( true === $prefix ) { $key = bbp_db()->get_blog_prefix() . $key; } // Return the old (pluralized) user option key return $key; } /** * Private function to get a 2.5 compatible cache key. * * This method exists to provide backwards compatibility with bbPress 2.5, * which had caching surrounding the FIND_IN_SET usermeta queries. * * @since 2.6.3 bbPress (r6991) * * @param string $meta_key * @param int $object_id * * @return string */ private function get_cache_key( $meta_key = '', $object_id = 0 ) { // No negative numbers in cache keys (zero is weird, but not disallowed) $object_id = absint( $object_id ); // Maybe guess at post type $post_type = ! empty( $object_id ) ? get_post_type( $object_id ) : bbp_get_topic_post_type(); switch ( $meta_key ) { // Favorites case '_bbp_favorite' : $key = 'bbp_get_topic_favoriters_'; break; // Subscriptions case '_bbp_subscription' : // Forums & Topics used different keys :/ $key = ( bbp_get_forum_post_type() === $post_type ) ? 'bbp_get_forum_subscribers_' : 'bbp_get_topic_subscribers_'; break; // Unknown, so pluralize default : $nounize = rtrim( $meta_key, 'e' ); $key = "bbp_get_{$post_type}_{$nounize}ers_"; break; } // Return the old (pluralized) user option key with object ID appended return "{$key}{$object_id}"; } /** * Get the user engagement cache for a given meta key and object ID. * * This method exists to provide backwards compatibility with bbPress 2.5, * which had caching surrounding the FIND_IN_SET queries in usermeta. * * @since 2.6.3 bbPress (r6991) * * @param string $meta_key * @param int $object_id * * @return mixed Results from cache get */ private function cache_get( $meta_key = '', $object_id = 0 ) { $cache_key = $this->get_cache_key( $meta_key, $object_id ); return wp_cache_get( $cache_key, 'bbpress_engagements' ); } /** * Set the user engagement cache for a given meta key and object ID. * * This method exists to provide backwards compatibility with bbPress 2.5, * which had caching surrounding the FIND_IN_SET queries in usermeta. * * @since 2.6.3 bbPress (r6991) * * @param string $meta_key * @param int $object_id * * @return mixed Results from cache set */ private function cache_set( $meta_key = '', $object_id = 0, $user_ids = array() ) { $cache_key = $this->get_cache_key( $meta_key, $object_id ); $user_ids = $this->parse_comma_list( $user_ids ); return wp_cache_set( $cache_key, $user_ids, 'bbpress_engagements' ); } /** * Delete the user engagement cache for a given meta key and object ID. * * This method exists to provide backwards compatibility with bbPress 2.5, * which had caching surrounding the FIND_IN_SET queries in usermeta. * * @since 2.6.3 bbPress (r6991) * * @param string $meta_key * @param int $object_id * * @return mixed Results from cache delete */ private function cache_delete( $meta_key = '', $object_id = 0 ) { $cache_key = $this->get_cache_key( $meta_key, $object_id ); return wp_cache_delete( $cache_key, 'bbpress_engagements' ); } /** * Turn a comma-separated string into an array of integers * * @since 2.6.0 bbPress (r6844) * * @param string $results * @return array */ private function parse_comma_list( $results = '' ) { return array_filter( wp_parse_id_list( $results ) ); } /** * Add a user id to an object * * @since 2.6.0 bbPress (r6844) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * @param bool $unique Whether meta key should be unique to the object * * @return bool Returns true on success, false on failure */ public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) { $retval = false; $option_key = $this->get_user_option_key( $meta_key, $object_id ); $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) ); $exists = array_search( $object_id, $object_ids ); // Not already added, so add it if ( false === $exists ) { $object_ids[] = $object_id; $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) ); $retval = update_user_option( $user_id, $option_key, $object_ids ); // Delete cache if successful (accounts for int & true) if ( false !== $retval ) { $this->cache_delete( $meta_key, $object_id ); } } // Return true if added, or false if not return $retval; } /** * Remove a user id from an object * * @since 2.6.0 bbPress (r6844) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { $retval = false; $option_key = $this->get_user_option_key( $meta_key, $object_id ); $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) ); $exists = array_search( $object_id, $object_ids ); // Exists, so remove it if ( false !== $exists ) { unset( $object_ids[ $exists ] ); $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) ); $retval = ! empty( $object_ids ) ? update_user_option( $user_id, $option_key, $object_ids ) : delete_user_option( $user_id, $option_key ); // Delete cache if successful (accounts for int & true) if ( false !== $retval ) { $this->cache_delete( $meta_key, $object_id ); } } // Return true if removed, or false if not return $retval; } /** * Remove a user id from all objects * * @since 2.6.0 bbPress (r6844) * * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) { // Get the key $option_key = $this->get_user_option_key( $meta_key ); // Get the option $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) ); // Attempt to delete the user option $retval = delete_user_option( $user_id, $option_key ); // Try to delete caches, but only if everything else succeeded if ( ! empty( $retval ) && ! empty( $object_ids ) ) { foreach ( $object_ids as $object_id ) { $this->cache_delete( $meta_key, $object_id ); } } // Return true if user was removed, or false if not return $retval; } /** * Remove an object from all users * * @since 2.6.0 bbPress (r6844) * * @param int $object_id The object id * @param int $user_id The user id * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { // Query for users $user_ids = $this->get_users_for_object( $object_id, $meta_key, $meta_type ); $u_count = count( $user_ids ); // Count number of removals $removed = array(); $r_count = 0; // Users have engaged, so remove them if ( ! empty( $u_count ) ) { // Loop through users and remove them from the object foreach ( $user_ids as $user_id ) { $removed[] = $this->remove_user_from_object( $object_id, $user_id, $meta_key, $meta_type ); } // Count the removed users $r_count = count( $removed ); } // Return true if successfully removed from all users return ( $r_count === $u_count ); } /** * Remove all users from all objects * * @since 2.6.0 bbPress (r6844) * * @param string $meta_key The relationship key * @param string $meta_type The relationship type (usually 'post') * * @return bool Returns true on success, false on failure */ public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) { // Query for users $option_key = $this->get_user_option_key( $meta_key, 0, true ); $bbp_db = bbp_db(); $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}'" ); $u_count = count( $user_ids ); // Count number of removals $removed = array(); $r_count = 0; // Users have engaged, so remove them if ( ! empty( $u_count ) ) { // Loop through users and remove their user options foreach ( $user_ids as $user_id ) { $removed[] = $this->remove_user_from_all_objects( $user_id, $meta_key ); } // Count the removed users $r_count = count( $removed ); } // Return true if successfully removed from all users return ( $r_count === $u_count ); } /** * Get users of an object * * The database queries in this function were cached in bbPress versions * older than 2.6, but no longer are to avoid cache pollution. * * @since 2.6.0 bbPress (r6844) * * @param int $object_id The object id * @param string $meta_key The key used to index this relationship * @param string $meta_type The type of meta to look in * * @return array Returns ids of users */ public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { // Try to get user IDs from cache $user_ids = $this->cache_get( $meta_key, $object_id ); // Cache is empty, so hit the database if ( false === $user_ids ) { $option_key = $this->get_user_option_key( $meta_key, $object_id, true ); $bbp_db = bbp_db(); $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}' and FIND_IN_SET('{$object_id}', meta_value) > 0" ); // Always cache results (even if empty, to prevent multiple misses) $this->cache_set( $meta_key, $object_id, $user_ids ); } // Return parsed IDs return $this->parse_comma_list( $user_ids ); } /** * Get the part of the query responsible for JOINing objects to relationships. * * @since 2.6.0 bbPress (r6844) * * @param array $args * @param string $meta_key * @param string $meta_type * * @return array */ public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) { $user_id = bbp_get_user_id( $args, true, true ); $option_key = $this->get_user_option_key( $meta_key ); $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) ); // Maybe trick WP_Query into ".ID IN (0)" to return no results if ( empty( $object_ids ) ) { $object_ids = array( 0 ); } // Maybe include these post IDs $args = array( 'post__in' => $object_ids ); // Parse arguments return bbp_parse_args( $args, array(), $context_key ); } } 6.0 bbPress (r5309) * * @param int $user_id * @param mixed $count * @return boolean */ function bbp_update_user_topic_count( $user_id = 0, $count = false ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } // Just in time filtering of the user's topic count $count = apply_filters( 'bbp_update_user_topic_count', $count, $user_id ); // Bail if no count was passed if ( false === $count ) { return false; } // Return the updated user option return update_user_option( $user_id, '_bbp_topic_count', $count ); } /** * Update the reply count for a user * * @since 2.6.0 bbPress (r5309) * * @param int $user_id * @param mixed $count * @return boolean */ function bbp_update_user_reply_count( $user_id = 0, $count = false ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } // Just in time filtering of the user's reply count $count = apply_filters( 'bbp_update_user_reply_count', $count, $user_id ); // Bail if no count was passed if ( false === $count ) { return false; } // Return the updated user option return update_user_option( $user_id, '_bbp_reply_count', $count ); } /** * Output a users topic count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_user_topic_count( $user_id = 0, $integer = false ) { echo esc_html( bbp_get_user_topic_count( $user_id, $integer ) ); } /** * Return a users reply count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_get_user_topic_count( $user_id = 0, $integer = false ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } $count = get_user_option( '_bbp_topic_count', $user_id ); $filter = ( true === $integer ) ? 'bbp_get_user_topic_count_int' : 'bbp_get_user_topic_count'; // Filter & return return apply_filters( $filter, $count, $user_id ); } /** * Output a users reply count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_user_reply_count( $user_id = 0, $integer = false ) { echo esc_html( bbp_get_user_reply_count( $user_id, $integer ) ); } /** * Return a users reply count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_get_user_reply_count( $user_id = 0, $integer = false ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } $count = get_user_option( '_bbp_reply_count', $user_id ); $filter = ( true === $integer ) ? 'bbp_get_user_reply_count_int' : 'bbp_get_user_reply_count'; return apply_filters( $filter, $count, $user_id ); } /** * Output a users total post count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_user_post_count( $user_id = 0, $integer = false ) { echo esc_html( bbp_get_user_post_count( $user_id, $integer ) ); } /** * Return a users total post count * * @since 2.1.0 bbPress (r3632) * * @param int $user_id * @param boolean $integer Optional. Whether or not to format the result * * @return string */ function bbp_get_user_post_count( $user_id = 0, $integer = false ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } $topics = bbp_get_user_topic_count( $user_id, true ); $replies = bbp_get_user_reply_count( $user_id, true ); $count = $topics + $replies; $filter = ( true === $integer ) ? 'bbp_get_user_post_count_int' : 'bbp_get_user_post_count'; return apply_filters( $filter, $count, $user_id ); } /** Last Posted ***************************************************************/ /** * Update a users last posted time, for use with post throttling * * @since 2.1.0 bbPress (r3910) * * @param int $user_id User ID to update * @param int $time Time in time() format * @return bool False if no user or failure, true if successful */ function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } // Set time to now if nothing is passed if ( empty( $time ) ) { $time = time(); } return update_user_option( $user_id, '_bbp_last_posted', $time ); } /** * Output the raw value of the last posted time. * * @since 2.1.0 bbPress (r3910) * * @param int $user_id User ID to retrieve value for */ function bbp_user_last_posted( $user_id = 0 ) { echo esc_html( bbp_get_user_last_posted( $user_id ) ); } /** * Return the raw value of the last posted time. * * @since 2.1.0 bbPress (r3910) * * @param int $user_id User ID to retrieve value for * @return mixed False if no user, time() format if exists */ function bbp_get_user_last_posted( $user_id = 0 ) { // Validate user id $user_id = bbp_get_user_id( $user_id ); if ( empty( $user_id ) ) { return false; } $time = get_user_option( '_bbp_last_posted', $user_id ); // Filter & return return apply_filters( 'bbp_get_user_last_posted', $time, $user_id ); } */ $pee = preg_replace( '!
\n(\[' . $block_hidden_form_tags . '[^]]*\])!', "\n$1", $pee ); } $pee = preg_replace( '!(]*>)\s*
!', "$1", $pee ); $pee = preg_replace( '!
(\s*]*>)!', '$1', $pee ); if ( strpos( $pee, ']*>)(.*?)!is', 'clean_pre', $pee ); } $pee = preg_replace( "|\n

$|", '

', $pee ); return $pee; } function wpcf7_autop_preserve_newline_callback( $matches ) { return str_replace( "\n", '', $matches[0] ); } function wpcf7_sanitize_query_var( $text ) { $text = wp_unslash( $text ); $text = wp_check_invalid_utf8( $text ); if ( false !== strpos( $text, '<' ) ) { $text = wp_pre_kses_less_than( $text ); $text = wp_strip_all_tags( $text ); } $text = preg_replace( '/%[a-f0-9]{2}/i', '', $text ); $text = preg_replace( '/ +/', ' ', $text ); $text = trim( $text, ' ' ); return $text; } function wpcf7_strip_quote( $text ) { $text = trim( $text ); if ( preg_match( '/^"(.*)"$/s', $text, $matches ) ) { $text = $matches[1]; } elseif ( preg_match( "/^'(.*)'$/s", $text, $matches ) ) { $text = $matches[1]; } return $text; } function wpcf7_strip_quote_deep( $arr ) { if ( is_string( $arr ) ) { return wpcf7_strip_quote( $arr ); } if ( is_array( $arr ) ) { $result = array(); foreach ( $arr as $key => $text ) { $result[$key] = wpcf7_strip_quote_deep( $text ); } return $result; } } function wpcf7_normalize_newline( $text, $to = "\n" ) { if ( ! is_string( $text ) ) { return $text; } $nls = array( "\r\n", "\r", "\n" ); if ( ! in_array( $to, $nls ) ) { return $text; } return str_replace( $nls, $to, $text ); } function wpcf7_normalize_newline_deep( $arr, $to = "\n" ) { if ( is_array( $arr ) ) { $result = array(); foreach ( $arr as $key => $text ) { $result[$key] = wpcf7_normalize_newline_deep( $text, $to ); } return $result; } return wpcf7_normalize_newline( $arr, $to ); } function wpcf7_strip_newline( $str ) { $str = (string) $str; $str = str_replace( array( "\r", "\n" ), '', $str ); return trim( $str ); } function wpcf7_canonicalize( $text, $strto = 'lower' ) { if ( function_exists( 'mb_convert_kana' ) and 'UTF-8' == get_option( 'blog_charset' ) ) { $text = mb_convert_kana( $text, 'asKV', 'UTF-8' ); } if ( 'lower' == $strto ) { $text = strtolower( $text ); } elseif ( 'upper' == $strto ) { $text = strtoupper( $text ); } $text = trim( $text ); return $text; } /** * Check whether a string is a valid NAME token. * * ID and NAME tokens must begin with a letter ([A-Za-z]) * and may be followed by any number of letters, digits ([0-9]), * hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). * * @see http://www.w3.org/TR/html401/types.html#h-6.2 * * @return bool True if it is a valid name, false if not. */ function wpcf7_is_name( $string ) { return preg_match( '/^[A-Za-z][-A-Za-z0-9_:.]*$/', $string ); } function wpcf7_sanitize_unit_tag( $tag ) { $tag = preg_replace( '/[^A-Za-z0-9_-]/', '', $tag ); return $tag; } function wpcf7_is_email( $email ) { $result = is_email( $email ); return apply_filters( 'wpcf7_is_email', $result, $email ); } function wpcf7_is_url( $url ) { $result = ( false !== filter_var( $url, FILTER_VALIDATE_URL ) ); return apply_filters( 'wpcf7_is_url', $result, $url ); } function wpcf7_is_tel( $tel ) { $pattern = '%^[+]?' // + sign . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234 . '(?:[/ -]*' // delimiter . '(?:\([0-9]+\)|[0-9]+)' // (1234) or 1234 . ')*$%'; $result = preg_match( $pattern, trim( $tel ) ); return apply_filters( 'wpcf7_is_tel', $result, $tel ); } function wpcf7_is_number( $number ) { $result = is_numeric( $number ); return apply_filters( 'wpcf7_is_number', $result, $number ); } function wpcf7_is_date( $date ) { $result = preg_match( '/^([0-9]{4,})-([0-9]{2})-([0-9]{2})$/', $date, $matches ); if ( $result ) { $result = checkdate( $matches[2], $matches[3], $matches[1] ); } return apply_filters( 'wpcf7_is_date', $result, $date ); } function wpcf7_is_mailbox_list( $mailbox_list ) { if ( ! is_array( $mailbox_list ) ) { $mailbox_text = (string) $mailbox_list; $mailbox_text = wp_unslash( $mailbox_text ); $mailbox_text = preg_replace( '/\\\\(?:\"|\')/', 'esc-quote', $mailbox_text ); $mailbox_text = preg_replace( '/(?:\".*?\"|\'.*?\')/', 'quoted-string', $mailbox_text ); $mailbox_list = explode( ',', $mailbox_text ); } $addresses = array(); foreach ( $mailbox_list as $mailbox ) { if ( ! is_string( $mailbox ) ) { return false; } $mailbox = trim( $mailbox ); if ( preg_match( '/<(.+)>$/', $mailbox, $matches ) ) { $addr_spec = $matches[1]; } else { $addr_spec = $mailbox; } if ( ! wpcf7_is_email( $addr_spec ) ) { return false; } $addresses[] = $addr_spec; } return $addresses; } function wpcf7_is_email_in_domain( $email, $domain ) { $email_list = wpcf7_is_mailbox_list( $email ); $domain = strtolower( $domain ); foreach ( $email_list as $email ) { $email_domain = substr( $email, strrpos( $email, '@' ) + 1 ); $email_domain = strtolower( $email_domain ); $domain_parts = explode( '.', $domain ); do { $site_domain = implode( '.', $domain_parts ); if ( $site_domain == $email_domain ) { continue 2; } array_shift( $domain_parts ); } while ( $domain_parts ); return false; } return true; } function wpcf7_is_email_in_site_domain( $email ) { if ( wpcf7_is_localhost() ) { return true; } $site_domain = strtolower( $_SERVER['SERVER_NAME'] ); if ( preg_match( '/^[0-9.]+$/', $site_domain ) ) { // 123.456.789.012 return true; } if ( wpcf7_is_email_in_domain( $email, $site_domain ) ) { return true; } $home_url = home_url(); // for interoperability with WordPress MU Domain Mapping plugin if ( is_multisite() and function_exists( 'domain_mapping_siteurl' ) ) { $domain_mapping_siteurl = domain_mapping_siteurl( false ); if ( $domain_mapping_siteurl ) { $home_url = $domain_mapping_siteurl; } } if ( preg_match( '%^https?://([^/]+)%', $home_url, $matches ) ) { $site_domain = strtolower( $matches[1] ); if ( $site_domain != strtolower( $_SERVER['SERVER_NAME'] ) and wpcf7_is_email_in_domain( $email, $site_domain ) ) { return true; } } return false; } function wpcf7_antiscript_file_name( $filename ) { $filename = wp_basename( $filename ); $parts = explode( '.', $filename ); if ( count( $parts ) < 2 ) { return $filename; } $script_pattern = '/^(php|phtml|pl|py|rb|cgi|asp|aspx)\d?$/i'; $filename = array_shift( $parts ); $extension = array_pop( $parts ); foreach ( (array) $parts as $part ) { if ( preg_match( $script_pattern, $part ) ) { $filename .= '.' . $part . '_'; } else { $filename .= '.' . $part; } } if ( preg_match( $script_pattern, $extension ) ) { $filename .= '.' . $extension . '_.txt'; } else { $filename .= '.' . $extension; } return $filename; } function wpcf7_mask_password( $text, $length_unmasked = 0 ) { $length = strlen( $text ); $length_unmasked = absint( $length_unmasked ); if ( 0 == $length_unmasked ) { if ( 9 < $length ) { $length_unmasked = 4; } elseif ( 3 < $length ) { $length_unmasked = 2; } else { $length_unmasked = $length; } } $text = substr( $text, 0 - $length_unmasked ); $text = str_pad( $text, $length, '*', STR_PAD_LEFT ); return $text; } efill', 'permission_callback' => '__return_true', ), ) ); } function wpcf7_rest_get_contact_forms( WP_REST_Request $request ) { $args = array(); $per_page = $request->get_param( 'per_page' ); if ( null !== $per_page ) { $args['posts_per_page'] = (int) $per_page; } $offset = $request->get_param( 'offset' ); if ( null !== $offset ) { $args['offset'] = (int) $offset; } $order = $request->get_param( 'order' ); if ( null !== $order ) { $args['order'] = (string) $order; } $orderby = $request->get_param( 'orderby' ); if ( null !== $orderby ) { $args['orderby'] = (string) $orderby; } $search = $request->get_param( 'search' ); if ( null !== $search ) { $args['s'] = (string) $search; } $items = WPCF7_ContactForm::find( $args ); $response = array(); foreach ( $items as $item ) { $response[] = array( 'id' => $item->id(), 'slug' => $item->name(), 'title' => $item->title(), 'locale' => $item->locale(), ); } return rest_ensure_response( $response ); } function wpcf7_rest_create_contact_form( WP_REST_Request $request ) { $id = (int) $request->get_param( 'id' ); if ( $id ) { return new WP_Error( 'wpcf7_post_exists', __( "Cannot create existing contact form.", 'contact-form-7' ), array( 'status' => 400 ) ); } $args = $request->get_params(); $args['id'] = -1; // Create $context = $request->get_param( 'context' ); $item = wpcf7_save_contact_form( $args, $context ); if ( ! $item ) { return new WP_Error( 'wpcf7_cannot_save', __( "There was an error saving the contact form.", 'contact-form-7' ), array( 'status' => 500 ) ); } $response = array( 'id' => $item->id(), 'slug' => $item->name(), 'title' => $item->title(), 'locale' => $item->locale(), 'properties' => wpcf7_get_properties_for_api( $item ), 'config_errors' => array(), ); if ( wpcf7_validate_configuration() ) { $config_validator = new WPCF7_ConfigValidator( $item ); $config_validator->validate(); $response['config_errors'] = $config_validator->collect_error_messages(); if ( 'save' == $context ) { $config_validator->save(); } } return rest_ensure_response( $response ); } function wpcf7_rest_get_contact_form( WP_REST_Request $request ) { $id = (int) $request->get_param( 'id' ); $item = wpcf7_contact_form( $id ); if ( ! $item ) { return new WP_Error( 'wpcf7_not_found', __( "The requested contact form was not found.", 'contact-form-7' ), array( 'status' => 404 ) ); } $response = array( 'id' => $item->id(), 'slug' => $item->name(), 'title' => $item->title(), 'locale' => $item->locale(), 'properties' => wpcf7_get_properties_for_api( $item ), ); return rest_ensure_response( $response ); } function wpcf7_rest_update_contact_form( WP_REST_Request $request ) { $id = (int) $request->get_param( 'id' ); $item = wpcf7_contact_form( $id ); if ( ! $item ) { return new WP_Error( 'wpcf7_not_found', __( "The requested contact form was not found.", 'contact-form-7' ), array( 'status' => 404 ) ); } $args = $request->get_params(); $context = $request->get_param( 'context' ); $item = wpcf7_save_contact_form( $args, $context ); if ( ! $item ) { return new WP_Error( 'wpcf7_cannot_save', __( "There was an error saving the contact form.", 'contact-form-7' ), array( 'status' => 500 ) ); } $response = array( 'id' => $item->id(), 'slug' => $item->name(), 'title' => $item->title(), 'locale' => $item->locale(), 'properties' => wpcf7_get_properties_for_api( $item ), 'config_errors' => array(), ); if ( wpcf7_validate_configuration() ) { $config_validator = new WPCF7_ConfigValidator( $item ); $config_validator->validate(); $response['config_errors'] = $config_validator->collect_error_messages(); if ( 'save' == $context ) { $config_validator->save(); } } return rest_ensure_response( $response ); } function wpcf7_rest_delete_contact_form( WP_REST_Request $request ) { $id = (int) $request->get_param( 'id' ); $item = wpcf7_contact_form( $id ); if ( ! $item ) { return new WP_Error( 'wpcf7_not_found', __( "The requested contact form was not found.", 'contact-form-7' ), array( 'status' => 404 ) ); } $result = $item->delete(); if ( ! $result ) { return new WP_Error( 'wpcf7_cannot_delete', __( "There was an error deleting the contact form.", 'contact-form-7' ), array( 'status' => 500 ) ); } $response = array( 'deleted' => true ); return rest_ensure_response( $response ); } function wpcf7_rest_create_feedback( WP_REST_Request $request ) { $url_params = $request->get_url_params(); $item = null; if ( ! empty( $url_params['id'] ) ) { $item = wpcf7_contact_form( $url_params['id'] ); } if ( ! $item ) { return new WP_Error( 'wpcf7_not_found', __( "The requested contact form was not found.", 'contact-form-7' ), array( 'status' => 404 ) ); } $result = $item->submit(); $unit_tag = $request->get_param( '_wpcf7_unit_tag' ); $response = array( 'into' => '#' . wpcf7_sanitize_unit_tag( $unit_tag ), 'status' => $result['status'], 'message' => $result['message'], 'posted_data_hash' => $result['posted_data_hash'], ); if ( 'validation_failed' == $result['status'] ) { $invalid_fields = array(); foreach ( (array) $result['invalid_fields'] as $name => $field ) { $invalid_fields[] = array( 'into' => 'span.wpcf7-form-control-wrap.' . sanitize_html_class( $name ), 'message' => $field['reason'], 'idref' => $field['idref'], ); } $response['invalid_fields'] = $invalid_fields; } $response = wpcf7_apply_filters_deprecated( 'wpcf7_ajax_json_echo', array( $response, $result ), '5.2', 'wpcf7_feedback_response' ); $response = apply_filters( 'wpcf7_feedback_response', $response, $result ); return rest_ensure_response( $response ); } function wpcf7_rest_get_refill( WP_REST_Request $request ) { $id = (int) $request->get_param( 'id' ); $item = wpcf7_contact_form( $id ); if ( ! $item ) { return new WP_Error( 'wpcf7_not_found', __( "The requested contact form was not found.", 'contact-form-7' ), array( 'status' => 404 ) ); } $response = wpcf7_apply_filters_deprecated( 'wpcf7_ajax_onload', array( array() ), '5.2', 'wpcf7_refill_response' ); $response = apply_filters( 'wpcf7_refill_response', array() ); return rest_ensure_response( $response ); } function wpcf7_get_properties_for_api( WPCF7_ContactForm $contact_form ) { $properties = $contact_form->get_properties(); $properties['form'] = array( 'content' => (string) $properties['form'], 'fields' => array_map( function( WPCF7_FormTag $form_tag ) { return array( 'type' => $form_tag->type, 'basetype' => $form_tag->basetype, 'name' => $form_tag->name, 'options' => $form_tag->options, 'raw_values' => $form_tag->raw_values, 'labels' => $form_tag->labels, 'values' => $form_tag->values, 'pipes' => $form_tag->pipes instanceof WPCF7_Pipes ? $form_tag->pipes->to_array() : $form_tag->pipes, 'content' => $form_tag->content, ); }, $contact_form->scan_form_tags() ), ); $properties['additional_settings'] = array( 'content' => (string) $properties['additional_settings'], 'settings' => array_filter( array_map( function( $setting ) { $pattern = '/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/'; if ( preg_match( $pattern, $setting, $matches ) ) { $name = trim( $matches[1] ); $value = trim( $matches[2] ); if ( in_array( $value, array( 'on', 'true' ), true ) ) { $value = true; } elseif ( in_array( $value, array( 'off', 'false' ), true ) ) { $value = false; } return array( $name, $value ); } return false; }, explode( "\n", $properties['additional_settings'] ) ) ), ); return $properties; }