EST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_items( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $passwords = WP_Application_Passwords::get_user_application_passwords( $user->ID ); $response = array(); foreach ( $passwords as $password ) { $response[] = $this->prepare_response_for_collection( $this->prepare_item_for_response( $password, $request ) ); } return new WP_REST_Response( $response ); } /** * Checks if a given request has access to get a specific application password. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ public function get_item_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'read_app_password', $user->ID, $request['uuid'] ) ) { return new WP_Error( 'rest_cannot_read_application_password', __( 'Sorry, you are not allowed to read this application password.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Retrieves one application password from the collection. * * @since 5.6.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 get_item( $request ) { $password = $this->get_application_password( $request ); if ( is_wp_error( $password ) ) { return $password; } return $this->prepare_item_for_response( $password, $request ); } /** * Checks if a given request has access to create application passwords. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function create_item_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'create_app_password', $user->ID ) ) { return new WP_Error( 'rest_cannot_create_application_passwords', __( 'Sorry, you are not allowed to create application passwords for this user.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Creates an application password. * * @since 5.6.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 create_item( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $prepared = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared ) ) { return $prepared; } $created = WP_Application_Passwords::create_new_application_password( $user->ID, wp_slash( (array) $prepared ) ); if ( is_wp_error( $created ) ) { return $created; } $password = $created[0]; $item = WP_Application_Passwords::get_user_application_password( $user->ID, $created[1]['uuid'] ); $item['new_password'] = WP_Application_Passwords::chunk_password( $password ); $fields_update = $this->update_additional_fields_for_object( $item, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } /** * Fires after a single application password is completely created or updated via the REST API. * * @since 5.6.0 * * @param array $item Inserted or updated password item. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating an application password, false when updating. */ do_action( 'rest_after_insert_application_password', $item, $request, true ); $request->set_param( 'context', 'edit' ); $response = $this->prepare_item_for_response( $item, $request ); $response->set_status( 201 ); $response->header( 'Location', $response->get_links()['self'][0]['href'] ); return $response; } /** * Checks if a given request has access to update application passwords. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. */ public function update_item_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'edit_app_password', $user->ID, $request['uuid'] ) ) { return new WP_Error( 'rest_cannot_edit_application_password', __( 'Sorry, you are not allowed to edit this application password.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Updates an application password. * * @since 5.6.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 ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $item = $this->get_application_password( $request ); if ( is_wp_error( $item ) ) { return $item; } $prepared = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared ) ) { return $prepared; } $saved = WP_Application_Passwords::update_application_password( $user->ID, $item['uuid'], wp_slash( (array) $prepared ) ); if ( is_wp_error( $saved ) ) { return $saved; } $fields_update = $this->update_additional_fields_for_object( $item, $request ); if ( is_wp_error( $fields_update ) ) { return $fields_update; } $item = WP_Application_Passwords::get_user_application_password( $user->ID, $item['uuid'] ); /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php */ do_action( 'rest_after_insert_application_password', $item, $request, false ); $request->set_param( 'context', 'edit' ); return $this->prepare_item_for_response( $item, $request ); } /** * Checks if a given request has access to delete all application passwords for a user. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. */ public function delete_items_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'delete_app_passwords', $user->ID ) ) { return new WP_Error( 'rest_cannot_delete_application_passwords', __( 'Sorry, you are not allowed to delete application passwords for this user.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Deletes all application passwords for a user. * * @since 5.6.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_items( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $deleted = WP_Application_Passwords::delete_all_application_passwords( $user->ID ); if ( is_wp_error( $deleted ) ) { return $deleted; } return new WP_REST_Response( array( 'deleted' => true, 'count' => $deleted, ) ); } /** * Checks if a given request has access to delete a specific application password for a user. * * @since 5.6.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. */ public function delete_item_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'delete_app_password', $user->ID, $request['uuid'] ) ) { return new WP_Error( 'rest_cannot_delete_application_password', __( 'Sorry, you are not allowed to delete this application password.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Deletes an application password for a user. * * @since 5.6.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 ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $password = $this->get_application_password( $request ); if ( is_wp_error( $password ) ) { return $password; } $request->set_param( 'context', 'edit' ); $previous = $this->prepare_item_for_response( $password, $request ); $deleted = WP_Application_Passwords::delete_application_password( $user->ID, $password['uuid'] ); if ( is_wp_error( $deleted ) ) { return $deleted; } return new WP_REST_Response( array( 'deleted' => true, 'previous' => $previous->get_data(), ) ); } /** * Checks if a given request has access to get the currently used application password for a user. * * @since 5.7.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ public function get_current_item_permissions_check( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( get_current_user_id() !== $user->ID ) { return new WP_Error( 'rest_cannot_introspect_app_password_for_non_authenticated_user', __( 'The authenticated application password can only be introspected for the current user.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Retrieves the application password being currently used for authentication of a user. * * @since 5.7.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 get_current_item( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $uuid = rest_get_authenticated_app_password(); if ( ! $uuid ) { return new WP_Error( 'rest_no_authenticated_app_password', __( 'Cannot introspect application password.' ), array( 'status' => 404 ) ); } $password = WP_Application_Passwords::get_user_application_password( $user->ID, $uuid ); if ( ! $password ) { return new WP_Error( 'rest_application_password_not_found', __( 'Application password not found.' ), array( 'status' => 500 ) ); } return $this->prepare_item_for_response( $password, $request ); } /** * Performs a permissions check for the request. * * @since 5.6.0 * @deprecated 5.7.0 Use `edit_user` directly or one of the specific meta capabilities introduced in 5.7.0. * * @param WP_REST_Request $request * @return true|WP_Error */ protected function do_permissions_check( $request ) { _deprecated_function( __METHOD__, '5.7.0' ); $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } if ( ! current_user_can( 'edit_user', $user->ID ) ) { return new WP_Error( 'rest_cannot_manage_application_passwords', __( 'Sorry, you are not allowed to manage application passwords for this user.' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Prepares an application password for a create or update operation. * * @since 5.6.0 * * @param WP_REST_Request $request Request object. * @return object|WP_Error The prepared item, or WP_Error object on failure. */ protected function prepare_item_for_database( $request ) { $prepared = (object) array( 'name' => $request['name'], ); if ( $request['app_id'] && ! $request['uuid'] ) { $prepared->app_id = $request['app_id']; } /** * Filters an application password before it is inserted via the REST API. * * @since 5.6.0 * * @param stdClass $prepared An object representing a single application password prepared for inserting or updating the database. * @param WP_REST_Request $request Request object. */ return apply_filters( 'rest_pre_insert_application_password', $prepared, $request ); } /** * Prepares the application password for the REST response. * * @since 5.6.0 * * @param array $item WordPress representation of the item. * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function prepare_item_for_response( $item, $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $fields = $this->get_fields_for_response( $request ); $prepared = array( 'uuid' => $item['uuid'], 'app_id' => empty( $item['app_id'] ) ? '' : $item['app_id'], 'name' => $item['name'], 'created' => gmdate( 'Y-m-d\TH:i:s', $item['created'] ), 'last_used' => $item['last_used'] ? gmdate( 'Y-m-d\TH:i:s', $item['last_used'] ) : null, 'last_ip' => $item['last_ip'] ? $item['last_ip'] : null, ); if ( isset( $item['new_password'] ) ) { $prepared['password'] = $item['new_password']; } $prepared = $this->add_additional_fields_to_object( $prepared, $request ); $prepared = $this->filter_response_by_context( $prepared, $request['context'] ); $response = new WP_REST_Response( $prepared ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $response->add_links( $this->prepare_links( $user, $item ) ); } /** * Filters the REST API response for an application password. * * @since 5.6.0 * * @param WP_REST_Response $response The response object. * @param array $item The application password array. * @param WP_REST_Request $request The request object. */ return apply_filters( 'rest_prepare_application_password', $response, $item, $request ); } /** * Prepares links for the request. * * @since 5.6.0 * * @param WP_User $user The requested user. * @param array $item The application password. * @return array The list of links. */ protected function prepare_links( WP_User $user, $item ) { return array( 'self' => array( 'href' => rest_url( sprintf( '%s/users/%d/application-passwords/%s', $this->namespace, $user->ID, $item['uuid'] ) ), ), ); } /** * Gets the requested user. * * @since 5.6.0 * * @param WP_REST_Request $request The request object. * @return WP_User|WP_Error The WordPress user associated with the request, or a WP_Error if none found. */ protected function get_user( $request ) { if ( ! wp_is_application_passwords_available() ) { return new WP_Error( 'application_passwords_disabled', __( 'Application passwords are not available.' ), array( 'status' => 501 ) ); } $error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) ); $id = $request['user_id']; if ( 'me' === $id ) { if ( ! is_user_logged_in() ) { return new WP_Error( 'rest_not_logged_in', __( 'You are not currently logged in.' ), array( 'status' => 401 ) ); } $user = wp_get_current_user(); } else { $id = (int) $id; if ( $id <= 0 ) { return $error; } $user = get_userdata( $id ); } if ( empty( $user ) || ! $user->exists() ) { return $error; } if ( is_multisite() && ! user_can( $user->ID, 'manage_sites' ) && ! is_user_member_of_blog( $user->ID ) ) { return $error; } if ( ! wp_is_application_passwords_available_for_user( $user ) ) { return new WP_Error( 'application_passwords_disabled_for_user', __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' ), array( 'status' => 501 ) ); } return $user; } /** * Gets the requested application password for a user. * * @since 5.6.0 * * @param WP_REST_Request $request The request object. * @return array|WP_Error The application password details if found, a WP_Error otherwise. */ protected function get_application_password( $request ) { $user = $this->get_user( $request ); if ( is_wp_error( $user ) ) { return $user; } $password = WP_Application_Passwords::get_user_application_password( $user->ID, $request['uuid'] ); if ( ! $password ) { return new WP_Error( 'rest_application_password_not_found', __( 'Application password not found.' ), array( 'status' => 404 ) ); } return $password; } /** * Retrieves the query params for the collections. * * @since 5.6.0 * * @return array Query parameters for the collection. */ public function get_collection_params() { return array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), ); } /** * Retrieves the application password's schema, conforming to JSON Schema. * * @since 5.6.0 * * @return array Item schema data. */ public function get_item_schema() { if ( $this->schema ) { return $this->add_additional_fields_schema( $this->schema ); } $this->schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'application-password', 'type' => 'object', 'properties' => array( 'uuid' => array( 'description' => __( 'The unique identifier for the application password.' ), 'type' => 'string', 'format' => 'uuid', 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), 'app_id' => array( 'description' => __( 'A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace.' ), 'type' => 'string', 'format' => 'uuid', 'context' => array( 'view', 'edit', 'embed' ), ), 'name' => array( 'description' => __( 'The name of the application password.' ), 'type' => 'string', 'required' => true, 'context' => array( 'view', 'edit', 'embed' ), 'minLength' => 1, 'pattern' => '.*\S.*', ), 'password' => array( 'description' => __( 'The generated password. Only available after adding an application.' ), 'type' => 'string', 'context' => array( 'edit' ), 'readonly' => true, ), 'created' => array( 'description' => __( 'The GMT date the application password was created.' ), 'type' => 'string', 'format' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'last_used' => array( 'description' => __( 'The GMT date the application password was last used.' ), 'type' => array( 'string', 'null' ), 'format' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'last_ip' => array( 'description' => __( 'The IP address the application password was last used by.' ), 'type' => array( 'string', 'null' ), 'format' => 'ip', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ), ); return $this->add_additional_fields_schema( $this->schema ); } } $dependents[] = $dependent; } } return $dependents; } /** * Gets the slugs of plugins that the dependent requires. * * @since 6.5.0 * * @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory. * @return array An array of dependency plugin slugs. */ public static function get_dependencies( $plugin_file ) { if ( isset( self::$dependencies[ $plugin_file ] ) ) { return self::$dependencies[ $plugin_file ]; } return array(); } /** * Gets a dependent plugin's filepath. * * @since 6.5.0 * * @param string $slug The dependent plugin's slug. * @return string|false The dependent plugin's filepath, relative to the plugins directory, * or false if the plugin has no dependencies. */ public static function get_dependent_filepath( $slug ) { $filepath = array_search( $slug, self::$dependent_slugs, true ); return $filepath ? $filepath : false; } /** * Determines whether the plugin has unmet dependencies. * * @since 6.5.0 * * @param string $plugin_file The plugin's filepath, relative to the plugins directory. * @return bool Whether the plugin has unmet dependencies. */ public static function has_unmet_dependencies( $plugin_file ) { if ( ! isset( self::$dependencies[ $plugin_file ] ) ) { return false; } require_once ABSPATH . '/wp-admin/includes/plugin.php'; foreach ( self::$dependencies[ $plugin_file ] as $dependency ) { $dependency_filepath = self::get_dependency_filepath( $dependency ); if ( false === $dependency_filepath || is_plugin_inactive( $dependency_filepath ) ) { return true; } } return false; } /** * Determines whether the plugin has a circular dependency. * * @since 6.5.0 * * @param string $plugin_file The plugin's filepath, relative to the plugins directory. * @return bool Whether the plugin has a circular dependency. */ public static function has_circular_dependency( $plugin_file ) { if ( ! is_array( self::$circular_dependencies_slugs ) ) { self::get_circular_dependencies(); } if ( ! empty( self::$circular_dependencies_slugs ) ) { $slug = self::convert_to_slug( $plugin_file ); if ( in_array( $slug, self::$circular_dependencies_slugs, true ) ) { return true; } } return false; } /** * Gets the names of plugins that require the plugin. * * @since 6.5.0 * * @param string $plugin_file The plugin's filepath, relative to the plugins directory. * @return array An array of dependent names. */ public static function get_dependent_names( $plugin_file ) { $dependent_names = array(); $plugins = self::get_plugins(); $slug = self::convert_to_slug( $plugin_file ); foreach ( self::get_dependents( $slug ) as $dependent ) { $dependent_names[ $dependent ] = $plugins[ $dependent ]['Name']; } sort( $dependent_names ); return $dependent_names; } /** * Gets the names of plugins required by the plugin. * * @since 6.5.0 * * @param string $plugin_file The dependent plugin's filepath, relative to the plugins directory. * @return array An array of dependency names. */ public static function get_dependency_names( $plugin_file ) { $dependency_api_data = self::get_dependency_api_data(); $dependencies = self::get_dependencies( $plugin_file ); $plugins = self::get_plugins(); $dependency_names = array(); foreach ( $dependencies as $dependency ) { // Use the name if it's available, otherwise fall back to the slug. if ( isset( $dependency_api_data[ $dependency ]['name'] ) ) { $name = $dependency_api_data[ $dependency ]['name']; } else { $dependency_filepath = self::get_dependency_filepath( $dependency ); if ( false !== $dependency_filepath ) { $name = $plugins[ $dependency_filepath ]['Name']; } else { $name = $dependency; } } $dependency_names[ $dependency ] = $name; } return $dependency_names; } /** * Gets the filepath for a dependency, relative to the plugin's directory. * * @since 6.5.0 * * @param string $slug The dependency's slug. * @return string|false If installed, the dependency's filepath relative to the plugins directory, otherwise false. */ public static function get_dependency_filepath( $slug ) { $dependency_filepaths = self::get_dependency_filepaths(); if ( ! isset( $dependency_filepaths[ $slug ] ) ) { return false; } return $dependency_filepaths[ $slug ]; } /** * Returns API data for the dependency. * * @since 6.5.0 * * @param string $slug The dependency's slug. * @return array|false The dependency's API data on success, otherwise false. */ public static function get_dependency_data( $slug ) { $dependency_api_data = self::get_dependency_api_data(); if ( isset( $dependency_api_data[ $slug ] ) ) { return $dependency_api_data[ $slug ]; } return false; } /** * Displays an admin notice if dependencies are not installed. * * @since 6.5.0 */ public static function display_admin_notice_for_unmet_dependencies() { if ( in_array( false, self::get_dependency_filepaths(), true ) ) { $error_message = __( 'Some required plugins are missing or inactive.' ); if ( is_multisite() ) { if ( current_user_can( 'manage_network_plugins' ) ) { $error_message .= ' ' . sprintf( /* translators: %s: Link to the network plugins page. */ __( 'Manage plugins.' ), esc_url( network_admin_url( 'plugins.php' ) ) ); } else { $error_message .= ' ' . __( 'Please contact your network administrator.' ); } } elseif ( 'plugins' !== get_current_screen()->base ) { $error_message .= ' ' . sprintf( /* translators: %s: Link to the plugins page. */ __( 'Manage plugins.' ), esc_url( admin_url( 'plugins.php' ) ) ); } wp_admin_notice( $error_message, array( 'type' => 'warning', ) ); } } /** * Displays an admin notice if circular dependencies are installed. * * @since 6.5.0 */ public static function display_admin_notice_for_circular_dependencies() { $circular_dependencies = self::get_circular_dependencies(); if ( ! empty( $circular_dependencies ) && count( $circular_dependencies ) > 1 ) { $circular_dependencies = array_unique( $circular_dependencies, SORT_REGULAR ); $plugins = self::get_plugins(); $plugin_dirnames = self::get_plugin_dirnames(); // Build output lines. $circular_dependency_lines = ''; foreach ( $circular_dependencies as $circular_dependency ) { $first_filepath = $plugin_dirnames[ $circular_dependency[0] ]; $second_filepath = $plugin_dirnames[ $circular_dependency[1] ]; $circular_dependency_lines .= sprintf( /* translators: 1: First plugin name, 2: Second plugin name. */ '
  • ' . _x( '%1$s requires %2$s', 'The first plugin requires the second plugin.' ) . '
  • ', '' . esc_html( $plugins[ $first_filepath ]['Name'] ) . '', '' . esc_html( $plugins[ $second_filepath ]['Name'] ) . '' ); } wp_admin_notice( sprintf( '

    %1$s

    %3$s

    ', __( 'These plugins cannot be activated because their requirements are invalid.' ), $circular_dependency_lines, __( 'Please contact the plugin authors for more information.' ) ), array( 'type' => 'warning', 'paragraph_wrap' => false, ) ); } } /** * Checks plugin dependencies after a plugin is installed via AJAX. * * @since 6.5.0 */ public static function check_plugin_dependencies_during_ajax() { check_ajax_referer( 'updates' ); if ( empty( $_POST['slug'] ) ) { wp_send_json_error( array( 'slug' => '', 'pluginName' => '', 'errorCode' => 'no_plugin_specified', 'errorMessage' => __( 'No plugin specified.' ), ) ); } $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); $status = array( 'slug' => $slug ); self::get_plugins(); self::get_plugin_dirnames(); if ( ! isset( self::$plugin_dirnames[ $slug ] ) ) { $status['errorCode'] = 'plugin_not_installed'; $status['errorMessage'] = __( 'The plugin is not installed.' ); wp_send_json_error( $status ); } $plugin_file = self::$plugin_dirnames[ $slug ]; $status['pluginName'] = self::$plugins[ $plugin_file ]['Name']; $status['plugin'] = $plugin_file; if ( current_user_can( 'activate_plugin', $plugin_file ) && is_plugin_inactive( $plugin_file ) ) { $status['activateUrl'] = add_query_arg( array( '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_file ), 'action' => 'activate', 'plugin' => $plugin_file, ), is_multisite() ? network_admin_url( 'plugins.php' ) : admin_url( 'plugins.php' ) ); } if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) { $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] ); } self::initialize(); $dependencies = self::get_dependencies( $plugin_file ); if ( empty( $dependencies ) ) { $status['message'] = __( 'The plugin has no required plugins.' ); wp_send_json_success( $status ); } require_once ABSPATH . '/wp-admin/includes/plugin.php'; $inactive_dependencies = array(); foreach ( $dependencies as $dependency ) { if ( false === self::$plugin_dirnames[ $dependency ] || is_plugin_inactive( self::$plugin_dirnames[ $dependency ] ) ) { $inactive_dependencies[] = $dependency; } } if ( ! empty( $inactive_dependencies ) ) { $inactive_dependency_names = array_map( function ( $dependency ) { if ( isset( self::$dependency_api_data[ $dependency ]['Name'] ) ) { $inactive_dependency_name = self::$dependency_api_data[ $dependency ]['Name']; } else { $inactive_dependency_name = $dependency; } return $inactive_dependency_name; }, $inactive_dependencies ); $status['errorCode'] = 'inactive_dependencies'; $status['errorMessage'] = sprintf( /* translators: %s: A list of inactive dependency plugin names. */ __( 'The following plugins must be activated first: %s.' ), implode( ', ', $inactive_dependency_names ) ); $status['errorData'] = array_combine( $inactive_dependencies, $inactive_dependency_names ); wp_send_json_error( $status ); } $status['message'] = __( 'All required plugins are installed and activated.' ); wp_send_json_success( $status ); } /** * Gets data for installed plugins. * * @since 6.5.0 * * @return array An array of plugin data. */ protected static function get_plugins() { if ( is_array( self::$plugins ) ) { return self::$plugins; } require_once ABSPATH . '/wp-admin/includes/plugin.php'; self::$plugins = get_plugins(); return self::$plugins; } /** * Reads and stores dependency slugs from a plugin's 'Requires Plugins' header. * * @since 6.5.0 */ protected static function read_dependencies_from_plugin_headers() { self::$dependencies = array(); self::$dependency_slugs = array(); self::$dependent_slugs = array(); $plugins = self::get_plugins(); foreach ( $plugins as $plugin => $header ) { if ( '' === $header['RequiresPlugins'] ) { continue; } $dependency_slugs = self::sanitize_dependency_slugs( $header['RequiresPlugins'] ); self::$dependencies[ $plugin ] = $dependency_slugs; self::$dependency_slugs = array_merge( self::$dependency_slugs, $dependency_slugs ); $dependent_slug = self::convert_to_slug( $plugin ); self::$dependent_slugs[ $plugin ] = $dependent_slug; } self::$dependency_slugs = array_unique( self::$dependency_slugs ); } /** * Sanitizes slugs. * * @since 6.5.0 * * @param string $slugs A comma-separated string of plugin dependency slugs. * @return array An array of sanitized plugin dependency slugs. */ protected static function sanitize_dependency_slugs( $slugs ) { $sanitized_slugs = array(); $slugs = explode( ',', $slugs ); foreach ( $slugs as $slug ) { $slug = trim( $slug ); /** * Filters a plugin dependency's slug before matching to * the WordPress.org slug format. * * Can be used to switch between free and premium plugin slugs, for example. * * @since 6.5.0 * * @param string $slug The slug. */ $slug = apply_filters( 'wp_plugin_dependencies_slug', $slug ); // Match to WordPress.org slug format. if ( preg_match( '/^[a-z0-9]+(-[a-z0-9]+)*$/mu', $slug ) ) { $sanitized_slugs[] = $slug; } } $sanitized_slugs = array_unique( $sanitized_slugs ); sort( $sanitized_slugs ); return $sanitized_slugs; } /** * Gets the filepath of installed dependencies. * If a dependency is not installed, the filepath defaults to false. * * @since 6.5.0 * * @return array An array of install dependencies filepaths, relative to the plugins directory. */ protected static function get_dependency_filepaths() { if ( is_array( self::$dependency_filepaths ) ) { return self::$dependency_filepaths; } if ( null === self::$dependency_slugs ) { return array(); } self::$dependency_filepaths = array(); $plugin_dirnames = self::get_plugin_dirnames(); foreach ( self::$dependency_slugs as $slug ) { if ( isset( $plugin_dirnames[ $slug ] ) ) { self::$dependency_filepaths[ $slug ] = $plugin_dirnames[ $slug ]; continue; } self::$dependency_filepaths[ $slug ] = false; } return self::$dependency_filepaths; } /** * Retrieves and stores dependency plugin data from the WordPress.org Plugin API. * * @since 6.5.0 * * @global string $pagenow The filename of the current screen. * * @return array|void An array of dependency API data, or void on early exit. */ protected static function get_dependency_api_data() { global $pagenow; if ( ! is_admin() || ( 'plugins.php' !== $pagenow && 'plugin-install.php' !== $pagenow ) ) { return; } if ( is_array( self::$dependency_api_data ) ) { return self::$dependency_api_data; } $plugins = self::get_plugins(); self::$dependency_api_data = (array) get_site_transient( 'wp_plugin_dependencies_plugin_data' ); foreach ( self::$dependency_slugs as $slug ) { // Set transient for individual data, remove from self::$dependency_api_data if transient expired. if ( ! get_site_transient( "wp_plugin_dependencies_plugin_timeout_{$slug}" ) ) { unset( self::$dependency_api_data[ $slug ] ); set_site_transient( "wp_plugin_dependencies_plugin_timeout_{$slug}", true, 12 * HOUR_IN_SECONDS ); } if ( isset( self::$dependency_api_data[ $slug ] ) ) { if ( false === self::$dependency_api_data[ $slug ] ) { $dependency_file = self::get_dependency_filepath( $slug ); if ( false === $dependency_file ) { self::$dependency_api_data[ $slug ] = array( 'Name' => $slug ); } else { self::$dependency_api_data[ $slug ] = array( 'Name' => $plugins[ $dependency_file ]['Name'] ); } continue; } // Don't hit the Plugin API if data exists. if ( ! empty( self::$dependency_api_data[ $slug ]['last_updated'] ) ) { continue; } } if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } $information = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'short_description' => true, 'icons' => true, ), ) ); if ( is_wp_error( $information ) ) { continue; } self::$dependency_api_data[ $slug ] = (array) $information; // plugins_api() returns 'name' not 'Name'. self::$dependency_api_data[ $slug ]['Name'] = self::$dependency_api_data[ $slug ]['name']; set_site_transient( 'wp_plugin_dependencies_plugin_data', self::$dependency_api_data, 0 ); } // Remove from self::$dependency_api_data if slug no longer a dependency. $differences = array_diff( array_keys( self::$dependency_api_data ), self::$dependency_slugs ); foreach ( $differences as $difference ) { unset( self::$dependency_api_data[ $difference ] ); } ksort( self::$dependency_api_data ); // Remove empty elements. self::$dependency_api_data = array_filter( self::$dependency_api_data ); set_site_transient( 'wp_plugin_dependencies_plugin_data', self::$dependency_api_data, 0 ); return self::$dependency_api_data; } /** * Gets plugin directory names. * * @since 6.5.0 * * @return array An array of plugin directory names. */ protected static function get_plugin_dirnames() { if ( is_array( self::$plugin_dirnames ) ) { return self::$plugin_dirnames; } self::$plugin_dirnames = array(); $plugin_files = array_keys( self::get_plugins() ); foreach ( $plugin_files as $plugin_file ) { $slug = self::convert_to_slug( $plugin_file ); self::$plugin_dirnames[ $slug ] = $plugin_file; } return self::$plugin_dirnames; } /** * Gets circular dependency data. * * @since 6.5.0 * * @return array[] An array of circular dependency pairings. */ protected static function get_circular_dependencies() { if ( is_array( self::$circular_dependencies_pairs ) ) { return self::$circular_dependencies_pairs; } if ( null === self::$dependencies ) { return array(); } self::$circular_dependencies_slugs = array(); self::$circular_dependencies_pairs = array(); foreach ( self::$dependencies as $dependent => $dependencies ) { /* * $dependent is in 'a/a.php' format. Dependencies are stored as slugs, i.e. 'a'. * * Convert $dependent to slug format for checking. */ $dependent_slug = self::convert_to_slug( $dependent ); self::$circular_dependencies_pairs = array_merge( self::$circular_dependencies_pairs, self::check_for_circular_dependencies( array( $dependent_slug ), $dependencies ) ); } return self::$circular_dependencies_pairs; } /** * Checks for circular dependencies. * * @since 6.5.0 * * @param array $dependents Array of dependent plugins. * @param array $dependencies Array of plugins dependencies. * @return array A circular dependency pairing, or an empty array if none exists. */ protected static function check_for_circular_dependencies( $dependents, $dependencies ) { $circular_dependencies_pairs = array(); // Check for a self-dependency. $dependents_location_in_its_own_dependencies = array_intersect( $dependents, $dependencies ); if ( ! empty( $dependents_location_in_its_own_dependencies ) ) { foreach ( $dependents_location_in_its_own_dependencies as $self_dependency ) { self::$circular_dependencies_slugs[] = $self_dependency; $circular_dependencies_pairs[] = array( $self_dependency, $self_dependency ); // No need to check for itself again. unset( $dependencies[ array_search( $self_dependency, $dependencies, true ) ] ); } } /* * Check each dependency to see: * 1. If it has dependencies. * 2. If its list of dependencies includes one of its own dependents. */ foreach ( $dependencies as $dependency ) { // Check if the dependency is also a dependent. $dependency_location_in_dependents = array_search( $dependency, self::$dependent_slugs, true ); if ( false !== $dependency_location_in_dependents ) { $dependencies_of_the_dependency = self::$dependencies[ $dependency_location_in_dependents ]; foreach ( $dependents as $dependent ) { // Check if its dependencies includes one of its own dependents. $dependent_location_in_dependency_dependencies = array_search( $dependent, $dependencies_of_the_dependency, true ); if ( false !== $dependent_location_in_dependency_dependencies ) { self::$circular_dependencies_slugs[] = $dependent; self::$circular_dependencies_slugs[] = $dependency; $circular_dependencies_pairs[] = array( $dependent, $dependency ); // Remove the dependent from its dependency's dependencies. unset( $dependencies_of_the_dependency[ $dependent_location_in_dependency_dependencies ] ); } } $dependents[] = $dependency; /* * Now check the dependencies of the dependency's dependencies for the dependent. * * Yes, that does make sense. */ $circular_dependencies_pairs = array_merge( $circular_dependencies_pairs, self::check_for_circular_dependencies( $dependents, array_unique( $dependencies_of_the_dependency ) ) ); } } return $circular_dependencies_pairs; } /** * Converts a plugin filepath to a slug. * * @since 6.5.0 * * @param string $plugin_file The plugin's filepath, relative to the plugins directory. * @return string The plugin's slug. */ protected static function convert_to_slug( $plugin_file ) { if ( 'hello.php' === $plugin_file ) { return 'hello-dolly'; } return str_contains( $plugin_file, '/' ) ? dirname( $plugin_file ) : str_replace( '.php', '', $plugin_file ); } } ption = ''; $metaData = aioseo()->meta->metaData->getMetaData( $post ); if ( ! empty( $metaData->description ) && ! $default ) { $description = $this->helpers->prepare( $metaData->description, $post->ID, false ); } if ( $description || ( in_array( 'autogenerateDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) && ! aioseo()->options->deprecated->searchAppearance->advanced->autogenerateDescriptions ) ) { $posts[ $post->ID ] = $description; return $description; } $description = $this->helpers->sanitize( $this->getPostTypeDescription( $post->post_type ), $post->ID, $default ); $generateDescriptions = apply_filters( 'aioseo_generate_descriptions_from_content', true, [ $post ] ); if ( ! $description && ! post_password_required( $post ) ) { $description = $post->post_excerpt; if ( $generateDescriptions && in_array( 'useContentForAutogeneratedDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) && aioseo()->options->deprecated->searchAppearance->advanced->useContentForAutogeneratedDescriptions ) { $description = aioseo()->helpers->getDescriptionFromContent( $post ); } $description = $this->helpers->sanitize( $description, $post->ID, $default ); if ( ! $description && $generateDescriptions && $post->post_content ) { $description = $this->helpers->sanitize( aioseo()->helpers->getDescriptionFromContent( $post ), $post->ID, $default ); } } if ( ! is_paged() ) { if ( in_array( 'descriptionFormat', aioseo()->internalOptions->deprecatedOptions, true ) ) { $descriptionFormat = aioseo()->options->deprecated->searchAppearance->global->descriptionFormat; if ( $descriptionFormat ) { $description = preg_replace( '/#description/', $description, $descriptionFormat ); } } } $posts[ $post->ID ] = $description ? $this->helpers->prepare( $description, $post->ID, $default ) : $this->helpers->prepare( term_description( '' ), $post->ID, $default ); return $posts[ $post->ID ]; } /** * Retrieve the default description for the post type. * * @since 4.0.6 * * @param string $postType The post type. * @return string The description. */ public function getPostTypeDescription( $postType ) { static $postTypeDescription = []; if ( isset( $postTypeDescription[ $postType ] ) ) { return $postTypeDescription[ $postType ]; } if ( aioseo()->dynamicOptions->searchAppearance->postTypes->has( $postType ) ) { $description = aioseo()->dynamicOptions->searchAppearance->postTypes->{$postType}->metaDescription; } $postTypeDescription[ $postType ] = empty( $description ) ? '' : $description; return $postTypeDescription[ $postType ]; } /** * Returns the term description. * * @since 4.0.6 * * @param \WP_Term $term The term object. * @param boolean $default Whether we want the default value, not the post one. * @return string The term description. */ public function getTermDescription( $term, $default = false ) { if ( ! is_a( $term, 'WP_Term' ) ) { return ''; } static $terms = []; if ( isset( $terms[ $term->term_id ] ) ) { return $terms[ $term->term_id ]; } $description = ''; if ( in_array( 'autogenerateDescriptions', aioseo()->internalOptions->deprecatedOptions, true ) && ! aioseo()->options->deprecated->searchAppearance->advanced->autogenerateDescriptions ) { $terms[ $term->term_id ] = $description; return $description; } $dynamicOptions = aioseo()->dynamicOptions->noConflict(); if ( ! $description && $dynamicOptions->searchAppearance->taxonomies->has( $term->taxonomy ) ) { $description = $this->helpers->prepare( aioseo()->dynamicOptions->searchAppearance->taxonomies->{$term->taxonomy}->metaDescription, false, $default ); } $terms[ $term->term_id ] = $description ? $description : $this->helpers->prepare( term_description( $term->term_id ), false, $default ); return $terms[ $term->term_id ]; } }
    Fatal error: Uncaught Error: Class "AIOSEO\Plugin\Common\Meta\Description" not found in /htdocs/wp-content/plugins/all-in-one-seo-pack/app/Common/Meta/Meta.php:70 Stack trace: #0 /htdocs/wp-content/plugins/all-in-one-seo-pack/app/AIOSEO.php(298): AIOSEO\Plugin\Common\Meta\Meta->__construct() #1 /htdocs/wp-content/plugins/all-in-one-seo-pack/app/AIOSEO.php(97): AIOSEO\Plugin\AIOSEO->load() #2 /htdocs/wp-content/plugins/all-in-one-seo-pack/app/AIOSEO.php(76): AIOSEO\Plugin\AIOSEO->init() #3 /htdocs/wp-content/plugins/all-in-one-seo-pack/app/AIOSEO.php(414): AIOSEO\Plugin\AIOSEO::instance() #4 /htdocs/wp-content/plugins/all-in-one-seo-pack/all_in_one_seo_pack.php(96): aioseo() #5 /htdocs/wp-settings.php(526): include_once('/htdocs/wp-cont...') #6 /htdocs/wp-config.php(85): require_once('/htdocs/wp-sett...') #7 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #8 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #9 /htdocs/index.php(17): require('/htdocs/wp-blog...') #10 {main} thrown in /htdocs/wp-content/plugins/all-in-one-seo-pack/app/Common/Meta/Meta.php on line 70