, $user_id = false ) { $author_meta = get_the_author_meta( $field, $user_id ); /** * The value of the requested user metadata. * * The filter name is dynamic and depends on the $field parameter of the function. * * @since 2.8.0 * * @param string $author_meta The value of the metadata. * @param int $user_id The user ID. */ echo apply_filters( "the_author_{$field}", $author_meta, $user_id ); } /** * Retrieve either author's link or author's name. * * If the author has a home page set, return an HTML link, otherwise just return the * author's name. * * @since 3.0.0 * * @return string|null An HTML link if the author's url exist in user meta, * else the result of get_the_author(). */ function get_the_author_link() { if ( get_the_author_meta('url') ) { return sprintf( '%3$s', esc_url( get_the_author_meta('url') ), /* translators: %s: author's display name */ esc_attr( sprintf( __( 'Visit %s’s website' ), get_the_author() ) ), get_the_author() ); } else { return get_the_author(); } } /** * Display either author's link or author's name. * * If the author has a home page set, echo an HTML link, otherwise just echo the * author's name. * * @link https://codex.wordpress.org/Template_Tags/the_author_link * * @since 2.1.0 */ function the_author_link() { echo get_the_author_link(); } /** * Retrieve the number of posts by the author of the current post. * * @since 1.5.0 * * @return int The number of posts by the author. */ function get_the_author_posts() { $post = get_post(); if ( ! $post ) { return 0; } return count_user_posts( $post->post_author, $post->post_type ); } /** * Display the number of posts by the author of the current post. * * @link https://codex.wordpress.org/Template_Tags/the_author_posts * @since 0.71 */ function the_author_posts() { echo get_the_author_posts(); } /** * Retrieves an HTML link to the author page of the current post's author. * * Returns an HTML-formatted link using get_author_posts_url(). * * @since 4.4.0 * * @global object $authordata The current author's DB object. * * @return string An HTML link to the author page. */ function get_the_author_posts_link() { global $authordata; if ( ! is_object( $authordata ) ) { return; } $link = sprintf( '', esc_url( get_author_posts_url( $authordata->ID, $authordata->user_nicename ) ), /* translators: %s: author's display name */ esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ), get_the_author() ); /** * Filters the link to the author page of the author of the current post. * * @since 2.9.0 * * @param string $link HTML link. */ return apply_filters( 'the_author_posts_link', $link ); } /** * Displays an HTML link to the author page of the current post's author. * * @since 1.2.0 * @since 4.4.0 Converted into a wrapper for get_the_author_posts_link() * * @param string $deprecated Unused. */ function the_author_posts_link( $deprecated = '' ) { if ( ! empty( $deprecated ) ) { _deprecated_argument( __FUNCTION__, '2.1.0' ); } echo get_the_author_posts_link(); } /** * Retrieve the URL to the author page for the user with the ID provided. * * @since 2.1.0 * * @global WP_Rewrite $wp_rewrite * * @param int $author_id Author ID. * @param string $author_nicename Optional. The author's nicename (slug). Default empty. * @return string The URL to the author's page. */ function get_author_posts_url( $author_id, $author_nicename = '' ) { global $wp_rewrite; $auth_ID = (int) $author_id; $link = $wp_rewrite->get_author_permastruct(); if ( empty($link) ) { $file = home_url( '/' ); $link = $file . '?author=' . $auth_ID; } else { if ( '' == $author_nicename ) { $user = get_userdata($author_id); if ( !empty($user->user_nicename) ) $author_nicename = $user->user_nicename; } $link = str_replace('%author%', $author_nicename, $link); $link = home_url( user_trailingslashit( $link ) ); } /** * Filters the URL to the author's page. * * @since 2.1.0 * * @param string $link The URL to the author's page. * @param int $author_id The author's id. * @param string $author_nicename The author's nice name. */ $link = apply_filters( 'author_link', $link, $author_id, $author_nicename ); return $link; } /** * List all the authors of the site, with several options available. * * @link https://codex.wordpress.org/Template_Tags/wp_list_authors * * @since 1.2.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param string|array $args { * Optional. Array or string of default arguments. * * @type string $orderby How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered', * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'. * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'. * @type int $number Maximum authors to return or display. Default empty (all authors). * @type bool $optioncount Show the count in parenthesis next to the author's name. Default false. * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false. * @type bool $show_fullname Whether to show the author's full name. Default false. * @type bool $hide_empty Whether to hide any authors with no posts. Default true. * @type string $feed If not empty, show a link to the author's feed and use this text as the alt * parameter of the link. Default empty. * @type string $feed_image If not empty, show a link to the author's feed and use this image URL as * clickable anchor. Default empty. * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type. * @type bool $echo Whether to output the result or instead return it. Default true. * @type string $style If 'list', each author is wrapped in an `
  • ` element, otherwise the authors * will be separated by commas. * @type bool $html Whether to list the items in HTML form or plaintext. Default true. * @type array|string $exclude Array or comma/space-separated list of author IDs to exclude. Default empty. * @type array|string $include Array or comma/space-separated list of author IDs to include. Default empty. * } * @return string|void The output, if echo is set to false. */ function wp_list_authors( $args = '' ) { global $wpdb; $defaults = array( 'orderby' => 'name', 'order' => 'ASC', 'number' => '', 'optioncount' => false, 'exclude_admin' => true, 'show_fullname' => false, 'hide_empty' => true, 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => '' ); $args = wp_parse_args( $args, $defaults ); $return = ''; $query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); $query_args['fields'] = 'ids'; $authors = get_users( $query_args ); $author_count = array(); foreach ( (array) $wpdb->get_results( "SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author" ) as $row ) { $author_count[$row->post_author] = $row->count; } foreach ( $authors as $author_id ) { $author = get_userdata( $author_id ); if ( $args['exclude_admin'] && 'admin' == $author->display_name ) { continue; } $posts = isset( $author_count[$author->ID] ) ? $author_count[$author->ID] : 0; if ( ! $posts && $args['hide_empty'] ) { continue; } if ( $args['show_fullname'] && $author->first_name && $author->last_name ) { $name = "$author->first_name $author->last_name"; } else { $name = $author->display_name; } if ( ! $args['html'] ) { $return .= $name . ', '; continue; // No need to go further to process HTML. } if ( 'list' == $args['style'] ) { $return .= '
  • '; } $link = sprintf( '%3$s', get_author_posts_url( $author->ID, $author->user_nicename ), /* translators: %s: author's display name */ esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ), $name ); if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { $link .= ' '; if ( empty( $args['feed_image'] ) ) { $link .= '('; } $link .= ''; } else { $link .= $name; } $link .= ''; if ( empty( $args['feed_image'] ) ) { $link .= ')'; } } if ( $args['optioncount'] ) { $link .= ' ('. $posts . ')'; } $return .= $link; $return .= ( 'list' == $args['style'] ) ? '
  • ' : ', '; } $return = rtrim( $return, ', ' ); if ( ! $args['echo'] ) { return $return; } echo $return; } /** * Does this site have more than one author * * Checks to see if more than one author has published posts. * * @since 3.2.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @return bool Whether or not we have more than one author */ function is_multi_author() { global $wpdb; if ( false === ( $is_multi_author = get_transient( 'is_multi_author' ) ) ) { $rows = (array) $wpdb->get_col("SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 2"); $is_multi_author = 1 < count( $rows ) ? 1 : 0; set_transient( 'is_multi_author', $is_multi_author ); } /** * Filters whether the site has more than one author with published posts. * * @since 3.2.0 * * @param bool $is_multi_author Whether $is_multi_author should evaluate as true. */ return apply_filters( 'is_multi_author', (bool) $is_multi_author ); } /** * Helper function to clear the cache for number of authors. * * @since 3.2.0 * @access private */ function __clear_multi_author_cache() { delete_transient( 'is_multi_author' ); } e. */ public function as_error() { if ( ! $this->is_error() ) { return null; } $error = new WP_Error; if ( is_array( $this->get_data() ) ) { $data = $this->get_data(); $error->add( $data['code'], $data['message'], $data['data'] ); if ( ! empty( $data['additional_errors'] ) ) { foreach( $data['additional_errors'] as $err ) { $error->add( $err['code'], $err['message'], $err['data'] ); } } } else { $error->add( $this->get_status(), '', array( 'status' => $this->get_status() ) ); } return $error; } /** * Retrieves the CURIEs (compact URIs) used for relations. * * @since 4.5.0 * * @return array Compact URIs. */ public function get_curies() { $curies = array( array( 'name' => 'wp', 'href' => 'https://api.w.org/{rel}', 'templated' => true, ), ); /** * Filters extra CURIEs available on API responses. * * CURIEs allow a shortened version of URI relations. This allows a more * usable form for custom relations than using the full URI. These work * similarly to how XML namespaces work. * * Registered CURIES need to specify a name and URI template. This will * automatically transform URI relations into their shortened version. * The shortened relation follows the format `{name}:{rel}`. `{rel}` in * the URI template will be replaced with the `{rel}` part of the * shortened relation. * * For example, a CURIE with name `example` and URI template * `http://w.org/{rel}` would transform a `http://w.org/term` relation * into `example:term`. * * Well-behaved clients should expand and normalise these back to their * full URI relation, however some naive clients may not resolve these * correctly, so adding new CURIEs may break backward compatibility. * * @since 4.5.0 * * @param array $additional Additional CURIEs to register with the API. */ $additional = apply_filters( 'rest_response_link_curies', array() ); return array_merge( $curies, $additional ); } } ram WP_Customize_Manager $wp_customize Theme Customizer object. */ public function add_checkout_section( $wp_customize ) { $wp_customize->add_section( 'woocommerce_checkout', array( 'title' => __( 'Checkout', 'woocommerce' ), 'priority' => 20, 'panel' => 'woocommerce', 'description' => __( 'These options let you change the appearance of the WooCommerce checkout.', 'woocommerce' ), ) ); // Checkout field controls. $fields = array( 'company' => __( 'Company name', 'woocommerce' ), 'address_2' => __( 'Address line 2', 'woocommerce' ), 'phone' => __( 'Phone', 'woocommerce' ), ); foreach ( $fields as $field => $label ) { $wp_customize->add_setting( 'woocommerce_checkout_' . $field . '_field', array( 'default' => 'phone' === $field ? 'required' : 'optional', 'type' => 'option', 'capability' => 'manage_woocommerce', 'sanitize_callback' => array( $this, 'sanitize_checkout_field_display' ), ) ); $wp_customize->add_control( 'woocommerce_checkout_' . $field . '_field', array( /* Translators: %s field name. */ 'label' => sprintf( __( '%s field', 'woocommerce' ), $label ), 'section' => 'woocommerce_checkout', 'settings' => 'woocommerce_checkout_' . $field . '_field', 'type' => 'select', 'choices' => array( 'hidden' => __( 'Hidden', 'woocommerce' ), 'optional' => __( 'Optional', 'woocommerce' ), 'required' => __( 'Required', 'woocommerce' ), ), ) ); } // Register settings. $wp_customize->add_setting( 'woocommerce_checkout_highlight_required_fields', array( 'default' => 'yes', 'type' => 'option', 'capability' => 'manage_woocommerce', 'sanitize_callback' => 'wc_bool_to_string', 'sanitize_js_callback' => 'wc_string_to_bool', ) ); $wp_customize->add_setting( 'woocommerce_checkout_terms_and_conditions_checkbox_text', array( /* translators: %s terms and conditions page name and link */ 'default' => sprintf( __( 'I have read and agree to the website %s', 'woocommerce' ), '[terms]' ), 'type' => 'option', 'capability' => 'manage_woocommerce', 'sanitize_callback' => 'wp_kses_post', 'transport' => 'postMessage', ) ); $wp_customize->add_setting( 'woocommerce_checkout_privacy_policy_text', array( /* translators: %s privacy policy page name and link */ 'default' => sprintf( __( 'Your personal data will be used to process your order, support your experience throughout this website, and for other purposes described in our %s.', 'woocommerce' ), '[privacy_policy]' ), 'type' => 'option', 'capability' => 'manage_woocommerce', 'sanitize_callback' => 'wp_kses_post', 'transport' => 'postMessage', ) ); // Register controls. $wp_customize->add_control( 'woocommerce_checkout_highlight_required_fields', array( 'label' => __( 'Highlight required fields with an asterisk', 'woocommerce' ), 'section' => 'woocommerce_checkout', 'settings' => 'woocommerce_checkout_highlight_required_fields', 'type' => 'checkbox', ) ); $choose_pages = array( 'wp_page_for_privacy_policy' => __( 'Privacy policy', 'woocommerce' ), 'woocommerce_terms_page_id' => __( 'Terms and conditions', 'woocommerce' ), ); $pages = get_pages( array( 'post_type' => 'page', 'post_status' => 'publish,private,draft', 'child_of' => 0, 'parent' => -1, 'exclude' => array( wc_get_page_id( 'cart' ), wc_get_page_id( 'checkout' ), wc_get_page_id( 'myaccount' ), ), 'sort_order' => 'asc', 'sort_column' => 'post_title', ) ); $page_choices = array( '' => __( 'No page set', 'woocommerce' ) ) + array_combine( array_map( 'strval', wp_list_pluck( $pages, 'ID' ) ), wp_list_pluck( $pages, 'post_title' ) ); foreach ( $choose_pages as $id => $name ) { $wp_customize->add_setting( $id, array( 'default' => '', 'type' => 'option', 'capability' => 'manage_woocommerce', ) ); $wp_customize->add_control( $id, array( /* Translators: %s: page name. */ 'label' => sprintf( __( '%s page', 'woocommerce' ), $name ), 'section' => 'woocommerce_checkout', 'settings' => $id, 'type' => 'select', 'choices' => $page_choices, ) ); } $wp_customize->add_control( 'woocommerce_checkout_privacy_policy_text', array( 'label' => __( 'Privacy policy', 'woocommerce' ), 'description' => __( 'Optionally add some text about your store privacy policy to show during checkout.', 'woocommerce' ), 'section' => 'woocommerce_checkout', 'settings' => 'woocommerce_checkout_privacy_policy_text', 'active_callback' => 'wc_privacy_policy_page_id', 'type' => 'textarea', ) ); $wp_customize->add_control( 'woocommerce_checkout_terms_and_conditions_checkbox_text', array( 'label' => __( 'Terms and conditions', 'woocommerce' ), 'description' => __( 'Optionally add some text for the terms checkbox that customers must accept.', 'woocommerce' ), 'section' => 'woocommerce_checkout', 'settings' => 'woocommerce_checkout_terms_and_conditions_checkbox_text', 'active_callback' => 'wc_terms_and_conditions_page_id', 'type' => 'text', ) ); if ( isset( $wp_customize->selective_refresh ) ) { $wp_customize->selective_refresh->add_partial( 'woocommerce_checkout_privacy_policy_text', array( 'selector' => '.woocommerce-privacy-policy-text', 'container_inclusive' => true, 'render_callback' => 'wc_checkout_privacy_policy_text', ) ); $wp_customize->selective_refresh->add_partial( 'woocommerce_checkout_terms_and_conditions_checkbox_text', array( 'selector' => '.woocommerce-terms-and-conditions-checkbox-text', 'container_inclusive' => false, 'render_callback' => 'wc_terms_and_conditions_checkbox_text', ) ); } } /** * Sanitize field display. * * @param string $value '', 'subcategories', or 'both'. * @return string */ public function sanitize_checkout_field_display( $value ) { $options = array( 'hidden', 'optional', 'required' ); return in_array( $value, $options, true ) ? $value : ''; } } new WC_Shop_Customizer(); wp_safe_redirect( $url ); exit; } elseif ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { wp_safe_redirect( wc_get_cart_url() ); exit; } } } /** * Handle adding simple products to the cart. * * @since 2.4.6 Split from add_to_cart_action. * @param int $product_id Product ID to add to the cart. * @return bool success or not */ private static function add_to_cart_handler_simple( $product_id ) { $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] ); $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) { wc_add_to_cart_message( array( $product_id => $quantity ), true ); return true; } return false; } /** * Handle adding grouped products to the cart. * * @since 2.4.6 Split from add_to_cart_action. * @param int $product_id Product ID to add to the cart. * @return bool success or not */ private static function add_to_cart_handler_grouped( $product_id ) { $was_added_to_cart = false; $added_to_cart = array(); if ( ! empty( $_REQUEST['quantity'] ) && is_array( $_REQUEST['quantity'] ) ) { $quantity_set = false; foreach ( $_REQUEST['quantity'] as $item => $quantity ) { if ( $quantity <= 0 ) { continue; } $quantity_set = true; // Add to cart validation $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity ); // Suppress total recalculation until finished. remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 ); if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) { $was_added_to_cart = true; $added_to_cart[ $item ] = $quantity; } add_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 ); } if ( ! $was_added_to_cart && ! $quantity_set ) { wc_add_notice( __( 'Please choose the quantity of items you wish to add to your cart…', 'woocommerce' ), 'error' ); } elseif ( $was_added_to_cart ) { wc_add_to_cart_message( $added_to_cart ); WC()->cart->calculate_totals(); return true; } } elseif ( $product_id ) { /* Link on product archives */ wc_add_notice( __( 'Please choose a product to add to your cart…', 'woocommerce' ), 'error' ); } return false; } /** * Handle adding variable products to the cart. * * @since 2.4.6 Split from add_to_cart_action. * @param int $product_id Product ID to add to the cart. * @return bool success or not */ private static function add_to_cart_handler_variable( $product_id ) { try { $variation_id = empty( $_REQUEST['variation_id'] ) ? '' : absint( wp_unslash( $_REQUEST['variation_id'] ) ); $quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST['quantity'] ) ); // WPCS: sanitization ok. $missing_attributes = array(); $variations = array(); $adding_to_cart = wc_get_product( $product_id ); if ( ! $adding_to_cart ) { return false; } // If the $product_id was in fact a variation ID, update the variables. if ( $adding_to_cart->is_type( 'variation' ) ) { $variation_id = $product_id; $product_id = $adding_to_cart->get_parent_id(); $adding_to_cart = wc_get_product( $product_id ); if ( ! $adding_to_cart ) { return false; } } // Gather posted attributes. $posted_attributes = array(); foreach ( $adding_to_cart->get_attributes() as $attribute ) { if ( ! $attribute['is_variation'] ) { continue; } $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] ); if ( isset( $_REQUEST[ $attribute_key ] ) ) { if ( $attribute['is_taxonomy'] ) { // Don't use wc_clean as it destroys sanitized characters. $value = sanitize_title( wp_unslash( $_REQUEST[ $attribute_key ] ) ); } else { $value = html_entity_decode( wc_clean( wp_unslash( $_REQUEST[ $attribute_key ] ) ), ENT_QUOTES, get_bloginfo( 'charset' ) ); // WPCS: sanitization ok. } $posted_attributes[ $attribute_key ] = $value; } } // If no variation ID is set, attempt to get a variation ID from posted attributes. if ( empty( $variation_id ) ) { $data_store = WC_Data_Store::load( 'product' ); $variation_id = $data_store->find_matching_product_variation( $adding_to_cart, $posted_attributes ); } // Do we have a variation ID? if ( empty( $variation_id ) ) { throw new Exception( __( 'Please choose product options…', 'woocommerce' ) ); } // Check the data we have is valid. $variation_data = wc_get_product_variation_attributes( $variation_id ); foreach ( $adding_to_cart->get_attributes() as $attribute ) { if ( ! $attribute['is_variation'] ) { continue; } // Get valid value from variation data. $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] ); $valid_value = isset( $variation_data[ $attribute_key ] ) ? $variation_data[ $attribute_key ]: ''; /** * If the attribute value was posted, check if it's valid. * * If no attribute was posted, only error if the variation has an 'any' attribute which requires a value. */ if ( isset( $posted_attributes[ $attribute_key ] ) ) { $value = $posted_attributes[ $attribute_key ]; // Allow if valid or show error. if ( $valid_value === $value ) { $variations[ $attribute_key ] = $value; } elseif ( '' === $valid_value && in_array( $value, $attribute->get_slugs() ) ) { // If valid values are empty, this is an 'any' variation so get all possible values. $variations[ $attribute_key ] = $value; } else { throw new Exception( sprintf( __( 'Invalid value posted for %s', 'woocommerce' ), wc_attribute_label( $attribute['name'] ) ) ); } } elseif ( '' === $valid_value ) { $missing_attributes[] = wc_attribute_label( $attribute['name'] ); } } if ( ! empty( $missing_attributes ) ) { throw new Exception( sprintf( _n( '%s is a required field', '%s are required fields', count( $missing_attributes ), 'woocommerce' ), wc_format_list_of_items( $missing_attributes ) ) ); } } catch ( Exception $e ) { wc_add_notice( $e->getMessage(), 'error' ); return false; } $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations ); if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) { wc_add_to_cart_message( array( $product_id => $quantity ), true ); return true; } return false; } /** * Process the login form. */ public static function process_login() { // The global form-login.php template used `_wpnonce` in template versions < 3.3.0. $nonce_value = wc_get_var( $_REQUEST['woocommerce-login-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. if ( ! empty( $_POST['login'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-login' ) ) { try { $creds = array( 'user_login' => trim( $_POST['username'] ), 'user_password' => $_POST['password'], 'remember' => isset( $_POST['rememberme'] ), ); $validation_error = new WP_Error(); $validation_error = apply_filters( 'woocommerce_process_login_errors', $validation_error, $_POST['username'], $_POST['password'] ); if ( $validation_error->get_error_code() ) { throw new Exception( '' . __( 'Error:', 'woocommerce' ) . ' ' . $validation_error->get_error_message() ); } if ( empty( $creds['user_login'] ) ) { throw new Exception( '' . __( 'Error:', 'woocommerce' ) . ' ' . __( 'Username is required.', 'woocommerce' ) ); } // On multisite, ensure user exists on current site, if not add them before allowing login. if ( is_multisite() ) { $user_data = get_user_by( is_email( $creds['user_login'] ) ? 'email' : 'login', $creds['user_login'] ); if ( $user_data && ! is_user_member_of_blog( $user_data->ID, get_current_blog_id() ) ) { add_user_to_blog( get_current_blog_id(), $user_data->ID, 'customer' ); } } // Perform the login $user = wp_signon( apply_filters( 'woocommerce_login_credentials', $creds ), is_ssl() ); if ( is_wp_error( $user ) ) { $message = $user->get_error_message(); $message = str_replace( '' . esc_html( $creds['user_login'] ) . '', '' . esc_html( $creds['user_login'] ) . '', $message ); throw new Exception( $message ); } else { if ( ! empty( $_POST['redirect'] ) ) { $redirect = $_POST['redirect']; } elseif ( wc_get_raw_referer() ) { $redirect = wc_get_raw_referer(); } else { $redirect = wc_get_page_permalink( 'myaccount' ); } wp_redirect( wp_validate_redirect( apply_filters( 'woocommerce_login_redirect', remove_query_arg( 'wc_error', $redirect ), $user ), wc_get_page_permalink( 'myaccount' ) ) ); exit; } } catch ( Exception $e ) { wc_add_notice( apply_filters( 'login_errors', $e->getMessage() ), 'error' ); do_action( 'woocommerce_login_failed' ); } } } /** * Handle lost password form. */ public static function process_lost_password() { if ( isset( $_POST['wc_reset_password'], $_POST['user_login'] ) ) { $nonce_value = wc_get_var( $_REQUEST['woocommerce-lost-password-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. if ( ! wp_verify_nonce( $nonce_value, 'lost_password' ) ) { return; } $success = WC_Shortcode_My_Account::retrieve_password(); // If successful, redirect to my account with query arg set. if ( $success ) { wp_redirect( add_query_arg( 'reset-link-sent', 'true', wc_get_account_endpoint_url( 'lost-password' ) ) ); exit; } } } /** * Handle reset password form. */ public static function process_reset_password() { $posted_fields = array( 'wc_reset_password', 'password_1', 'password_2', 'reset_key', 'reset_login' ); foreach ( $posted_fields as $field ) { if ( ! isset( $_POST[ $field ] ) ) { return; } $posted_fields[ $field ] = $_POST[ $field ]; } $nonce_value = wc_get_var( $_REQUEST['woocommerce-reset-password-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. if ( ! wp_verify_nonce( $nonce_value, 'reset_password' ) ) { return; } $user = WC_Shortcode_My_Account::check_password_reset_key( $posted_fields['reset_key'], $posted_fields['reset_login'] ); if ( $user instanceof WP_User ) { if ( empty( $posted_fields['password_1'] ) ) { wc_add_notice( __( 'Please enter your password.', 'woocommerce' ), 'error' ); } if ( $posted_fields['password_1'] !== $posted_fields['password_2'] ) { wc_add_notice( __( 'Passwords do not match.', 'woocommerce' ), 'error' ); } $errors = new WP_Error(); do_action( 'validate_password_reset', $errors, $user ); wc_add_wp_error_notices( $errors ); if ( 0 === wc_notice_count( 'error' ) ) { WC_Shortcode_My_Account::reset_password( $user, $posted_fields['password_1'] ); do_action( 'woocommerce_customer_reset_password', $user ); wp_redirect( add_query_arg( 'password-reset', 'true', wc_get_page_permalink( 'myaccount' ) ) ); exit; } } } /** * Process the registration form. */ public static function process_registration() { $nonce_value = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : ''; $nonce_value = isset( $_POST['woocommerce-register-nonce'] ) ? $_POST['woocommerce-register-nonce'] : $nonce_value; if ( ! empty( $_POST['register'] ) && wp_verify_nonce( $nonce_value, 'woocommerce-register' ) ) { $username = 'no' === get_option( 'woocommerce_registration_generate_username' ) ? $_POST['username'] : ''; $password = 'no' === get_option( 'woocommerce_registration_generate_password' ) ? $_POST['password'] : ''; $email = $_POST['email']; try { $validation_error = new WP_Error(); $validation_error = apply_filters( 'woocommerce_process_registration_errors', $validation_error, $username, $password, $email ); if ( $validation_error->get_error_code() ) { throw new Exception( $validation_error->get_error_message() ); } $new_customer = wc_create_new_customer( sanitize_email( $email ), wc_clean( $username ), $password ); if ( is_wp_error( $new_customer ) ) { throw new Exception( $new_customer->get_error_message() ); } if ( apply_filters( 'woocommerce_registration_auth_new_customer', true, $new_customer ) ) { wc_set_customer_auth_cookie( $new_customer ); } if ( ! empty( $_POST['redirect'] ) ) { $redirect = wp_sanitize_redirect( $_POST['redirect'] ); } elseif ( wc_get_raw_referer() ) { $redirect = wc_get_raw_referer(); } else { $redirect = wc_get_page_permalink( 'myaccount' ); } wp_redirect( wp_validate_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ), wc_get_page_permalink( 'myaccount' ) ) ); exit; } catch ( Exception $e ) { wc_add_notice( '' . __( 'Error:', 'woocommerce' ) . ' ' . $e->getMessage(), 'error' ); } } } } WC_Form_Handler::init();