ation', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) ); return $response; } /** * Updates a single term from a taxonomy. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function update_item( $request ) { $term = $this->get_term( $request['id'] ); if ( is_wp_error( $term ) ) { return $term; } if ( isset( $request['parent'] ) ) { if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) { return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) ); } $parent = get_term( (int) $request['parent'], $this->taxonomy ); if ( ! $parent ) { return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) ); } } $prepared_term = $this->prepare_item_for_database( $request ); // Only update the term if we have something to update. if ( ! empty( $prepared_term ) ) { if ( ! isset( $prepared_term->{'menu-name'} ) ) { // wp_update_nav_menu_object() requires that the menu-name is always passed. $prepared_term->{'menu-name'} = $term->name; } $update = wp_update_nav_menu_object( $term->term_id, wp_slash( (array) $prepared_term ) ); if ( is_wp_error( $update ) ) { return $update; } } $term = get_term( $term->term_id, $this->taxonomy ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ do_action( "rest_insert_{$this->taxonomy}", $term, $request, false ); $schema = $this->get_item_schema(); if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) { $meta_update = $this->meta->update_value( $request['meta'], $term->term_id ); if ( is_wp_error( $meta_update ) ) { return $meta_update; } } $locations_update = $this->handle_locations( $term->term_id, $request ); if ( is_wp_error( $locations_update ) ) { return $locations_update; } $this->handle_auto_add( $term->term_id, $request ); $fields_update = $this->update_additional_fields_for_object( $term, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } $request->set_param( 'context', 'view' ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false ); $response = $this->prepare_item_for_response( $term, $request ); return rest_ensure_response( $response ); } /** * Deletes a single term from a taxonomy. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item( $request ) { $term = $this->get_term( $request['id'] ); if ( is_wp_error( $term ) ) { return $term; } // We don't support trashing for terms. if ( ! $request['force'] ) { /* translators: %s: force=true */ return new WP_Error( 'rest_trash_not_supported', sprintf( __( "Menus do not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) ); } $request->set_param( 'context', 'view' ); $previous = $this->prepare_item_for_response( $term, $request ); $result = wp_delete_nav_menu( $term ); if ( ! $result || is_wp_error( $result ) ) { return new WP_Error( 'rest_cannot_delete', __( 'The menu cannot be deleted.' ), array( 'status' => 500 ) ); } $response = new WP_REST_Response(); $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data(), ) ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */ do_action( "rest_delete_{$this->taxonomy}", $term, $response, $request ); return $response; } /** * Returns the value of a menu's auto_add setting. * * @since 5.9.0 * * @param int $menu_id The menu id to query. * @return bool The value of auto_add. */ protected function get_menu_auto_add( $menu_id ) { $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) ); return in_array( $menu_id, $nav_menu_option['auto_add'], true ); } /** * Updates the menu's auto add from a REST request. * * @since 5.9.0 * * @param int $menu_id The menu id to update. * @param WP_REST_Request $request Full details about the request. * @return bool True if the auto add setting was successfully updated. */ protected function handle_auto_add( $menu_id, $request ) { if ( ! isset( $request['auto_add'] ) ) { return true; } $nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) ); if ( ! isset( $nav_menu_option['auto_add'] ) ) { $nav_menu_option['auto_add'] = array(); } $auto_add = $request['auto_add']; $i = array_search( $menu_id, $nav_menu_option['auto_add'], true ); if ( $auto_add && false === $i ) { $nav_menu_option['auto_add'][] = $menu_id; } elseif ( ! $auto_add && false !== $i ) { array_splice( $nav_menu_option['auto_add'], $i, 1 ); } $update = update_option( 'nav_menu_options', $nav_menu_option ); /** This action is documented in wp-includes/nav-menu.php */ do_action( 'wp_update_nav_menu', $menu_id ); return $update; } /** * Returns the names of the locations assigned to the menu. * * @since 5.9.0 * * @param int $menu_id The menu id. * @return string[] The locations assigned to the menu. */ protected function get_menu_locations( $menu_id ) { $locations = get_nav_menu_locations(); $menu_locations = array(); foreach ( $locations as $location => $assigned_menu_id ) { if ( $menu_id === $assigned_menu_id ) { $menu_locations[] = $location; } } return $menu_locations; } /** * Updates the menu's locations from a REST request. * * @since 5.9.0 * * @param int $menu_id The menu id to update. * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True on success, a WP_Error on an error updating any of the locations. */ protected function handle_locations( $menu_id, $request ) { if ( ! isset( $request['locations'] ) ) { return true; } $menu_locations = get_registered_nav_menus(); $menu_locations = array_keys( $menu_locations ); $new_locations = array(); foreach ( $request['locations'] as $location ) { if ( ! in_array( $location, $menu_locations, true ) ) { return new WP_Error( 'rest_invalid_menu_location', __( 'Invalid menu location.' ), array( 'status' => 400, 'location' => $location, ) ); } $new_locations[ $location ] = $menu_id; } $assigned_menu = get_nav_menu_locations(); foreach ( $assigned_menu as $location => $term_id ) { if ( $term_id === $menu_id ) { unset( $assigned_menu[ $location ] ); } } $new_assignments = array_merge( $assigned_menu, $new_locations ); set_theme_mod( 'nav_menu_locations', $new_assignments ); return true; } /** * Retrieves the term's schema, conforming to JSON Schema. * * @since 5.9.0 * * @return array Item schema data. */ public function get_item_schema() { if ( $this->schema ) { return $this->add_additional_fields_schema( $this->schema ); } $schema = parent::get_item_schema(); unset( $schema['properties']['count'], $schema['properties']['link'], $schema['properties']['taxonomy'] ); $schema['properties']['locations'] = array( 'description' => __( 'The locations assigned to the menu.' ), 'type' => 'array', 'items' => array( 'type' => 'string', ), 'context' => array( 'view', 'edit' ), 'arg_options' => array( 'validate_callback' => static function ( $locations, $request, $param ) { $valid = rest_validate_request_arg( $locations, $request, $param ); if ( true !== $valid ) { return $valid; } $locations = rest_sanitize_request_arg( $locations, $request, $param ); foreach ( $locations as $location ) { if ( ! array_key_exists( $location, get_registered_nav_menus() ) ) { return new WP_Error( 'rest_invalid_menu_location', __( 'Invalid menu location.' ), array( 'location' => $location, ) ); } } return true; }, ), ); $schema['properties']['auto_add'] = array( 'description' => __( 'Whether to automatically add top level pages to this menu.' ), 'context' => array( 'view', 'edit' ), 'type' => 'boolean', ); $this->schema = $schema; return $this->add_additional_fields_schema( $this->schema ); } } , ), 'links' => array( array( '_type' => self::LINK_TYPE_PRICING, 'url' => 'https://gocardless.com/pricing/', ), array( '_type' => self::LINK_TYPE_ABOUT, 'url' => 'https://woocommerce.com/products/gocardless/', ), array( '_type' => self::LINK_TYPE_TERMS, 'url' => 'https://gocardless.com/legal/', ), array( '_type' => self::LINK_TYPE_DOCS, 'url' => 'https://woocommerce.com/document/gocardless/', ), array( '_type' => self::LINK_TYPE_SUPPORT, 'url' => 'https://woocommerce.com/my-account/contact-support/?select=gocardless', ), ), ), self::NEXI => array( '_type' => self::TYPE_PSP, 'icon' => plugins_url( 'assets/images/onboarding/icons/nexi.svg', WC_PLUGIN_FILE ), 'plugin' => array( '_type' => self::PLUGIN_TYPE_WPORG, 'slug' => 'dibs-easy-for-woocommerce', ), ), self::PAYPAL_ZETTLE => array( '_type' => self::TYPE_PSP, 'icon' => plugins_url( 'assets/images/onboarding/icons/paypal-zettle.svg', WC_PLUGIN_FILE ), 'plugin' => array( '_type' => self::PLUGIN_TYPE_WPORG, 'slug' => 'zettle-pos-integration', ), ), self::RAPYD => array( '_type' => self::TYPE_PSP, 'icon' => plugins_url( 'assets/images/onboarding/icons/rapyd.svg', WC_PLUGIN_FILE ), 'plugin' => array( '_type' => self::PLUGIN_TYPE_WPORG, 'slug' => 'rapyd-payments-plugin-for-woocommerce', ), ), self::PAYPAL_BRAINTREE => array( '_type' => self::TYPE_PSP, 'icon' => plugins_url( 'assets/images/onboarding/icons/paypal-braintree.svg', WC_PLUGIN_FILE ), 'plugin' => array( '_type' => self::PLUGIN_TYPE_WPORG, 'slug' => 'woocommerce-gateway-paypal-powered-by-braintree', ), ), ); return $this->extensions_base_details_memo; } /** * Get the base details for a specific extension. * * @see self::standardize_extension_details() for the supported entries. * * @param string $extension_id The extension ID. * * @return ?array The extension base details. * Null if the extension is not one we have details for. */ private function get_extension_base_details( string $extension_id ): ?array { $extensions = $this->get_all_extensions_base_details(); if ( ! isset( $extensions[ $extension_id ] ) ) { return null; } return $extensions[ $extension_id ]; } /** * Standardize the details for an extension. * * Ensures that the details array has all the required fields, and fills in any missing optional fields with defaults. * We also enforce a consistent order for the fields. * * @param array $extension_details The extension details. * * @return array The standardized extension details. */ private function standardize_extension_details( array $extension_details ): array { $standardized = array(); // Required fields. $standardized['id'] = $extension_details['id']; $standardized['_priority'] = $extension_details['_priority']; $standardized['_type'] = $extension_details['_type']; $standardized['plugin'] = $extension_details['plugin']; // Optional fields. $standardized['title'] = $extension_details['title'] ?? ''; $standardized['description'] = $extension_details['description'] ?? ''; $standardized['image'] = $extension_details['image'] ?? ''; $standardized['icon'] = $extension_details['icon'] ?? ''; $standardized['links'] = $extension_details['links'] ?? array(); $standardized['tags'] = $extension_details['tags'] ?? array(); $standardized['_incentive'] = $extension_details['_incentive'] ?? null; return $standardized; } /** * Based on the WC onboarding profile, determine if the merchant is selling online. * * If the user skipped the profiler (no data points provided), we assume they are selling online. * * @return bool True if the merchant is selling online, false otherwise. */ private function is_merchant_selling_online(): bool { /* * We consider a merchant to be selling online if: * - The profiler was skipped (no data points provided). * OR * - The merchant answered 'Which one of these best describes you?' with 'I’m already selling' AND: * - Didn't answer to the 'Are you selling online?' question. * OR * - Answered the 'Are you selling online?' question with either: * - 'Yes, I’m selling online'. * OR * - 'I’m selling both online and offline'. * * @see plugins/woocommerce/client/admin/client/core-profiler/pages/UserProfile.tsx for the values. */ $onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() ); if ( ! isset( $onboarding_profile['business_choice'] ) || ( 'im_already_selling' === $onboarding_profile['business_choice'] && ( ! isset( $onboarding_profile['selling_online_answer'] ) || ( 'yes_im_selling_online' === $onboarding_profile['selling_online_answer'] || 'im_selling_both_online_and_offline' === $onboarding_profile['selling_online_answer'] ) ) ) ) { return false; } return true; } /** * Based on the WC onboarding profile, determine if the merchant is selling offline. * * If the user skipped the profiler (no data points provided), we assume they are NOT selling offline. * * @return bool True if the merchant is selling offline, false otherwise. */ private function is_merchant_selling_offline(): bool { /* * We consider a merchant to be selling offline if: * - The profiler was NOT skipped (data points provided). * AND * - The merchant answered 'Which one of these best describes you?' with 'I’m already selling' AND: * - Answered the 'Are you selling online?' question with either: * - 'No, I’m selling offline'. * OR * - 'I’m selling both online and offline'. * * @see plugins/woocommerce/client/admin/client/core-profiler/pages/UserProfile.tsx for the values. */ $onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() ); if ( isset( $onboarding_profile['business_choice'] ) && ( 'im_already_selling' === $onboarding_profile['business_choice'] && ( isset( $onboarding_profile['selling_online_answer'] ) && ( 'no_im_selling_offline' === $onboarding_profile['selling_online_answer'] || 'im_selling_both_online_and_offline' === $onboarding_profile['selling_online_answer'] ) ) ) ) { return true; } return false; } }