WP_Error example


    public function handle_error( array $error ) {

        $extension = $this->get_extension_for_error( $error );

        if ( ! $extension || $this->is_network_plugin( $extension ) ) {
            return new WP_Error( 'invalid_source', __( 'Error not caused by a plugin or theme.' ) );
        }

        if ( ! $this->is_active() ) {
            if ( ! is_protected_endpoint() ) {
                return new WP_Error( 'non_protected_endpoint', __( 'Error occurred on a non-protected endpoint.' ) );
            }

            if ( ! function_exists( 'wp_generate_password' ) ) {
                require_once ABSPATH . WPINC . '/pluggable.php';
            }

            

    public function load() {
        if ( $this->image instanceof Imagick ) {
            return true;
        }

        if ( ! is_file( $this->file ) && ! wp_is_stream( $this->file ) ) {
            return new WP_Error( 'error_loading_image', __( 'File does not exist?' )$this->file );
        }

        /* * Even though Imagick uses less PHP memory than GD, set higher limit * for users that have low PHP.ini limits. */
        wp_raise_memory_limit( 'image' );

        try {
            $this->image    = new Imagick();
            $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );

            

    do_action_ref_array( 'wp_feed_options', array( &$feed$url ) );

    $feed->init();
    $feed->set_output_encoding( get_option( 'blog_charset' ) );

    if ( $feed->error() ) {
        return new WP_Error( 'simplepie-error', $feed->error() );
    }

    return $feed;
}

    public function get_items_permissions_check( $request ) {
        $parent = $this->get_parent( $request['id'] );
        if ( is_wp_error( $parent ) ) {
            return $parent;
        }

        if ( ! current_user_can( 'edit_post', $parent->ID ) ) {
            return new WP_Error(
                'rest_cannot_read',
                __( 'Sorry, you are not allowed to view autosaves of this post.' ),
                array( 'status' => rest_authorization_required_code() )
            );
        }

        return true;
    }

    /** * Checks if a given request has access to create an autosave revision. * * Autosave revisions inherit permissions from the parent post, * check if the current user has permission to edit the post. * * @since 5.0.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to create the item, WP_Error object otherwise. */

    public function get_items_permissions_check( $request ) {
        $tax_obj = get_taxonomy( $this->taxonomy );

        if ( ! $tax_obj || ! $this->check_is_taxonomy_allowed( $this->taxonomy ) ) {
            return false;
        }

        if ( 'edit' === $request['context'] && ! current_user_can( $tax_obj->cap->edit_terms ) ) {
            return new WP_Error(
                'rest_forbidden_context',
                __( 'Sorry, you are not allowed to edit terms in this taxonomy.' ),
                array( 'status' => rest_authorization_required_code() )
            );
        }

        if ( ! empty( $request['post'] ) ) {
            $post = get_post( $request['post'] );

            if ( ! $post ) {
                return new WP_Error(
                    
if ( ! WP_Filesystem( $credentials$directories[0]$allow_relaxed_file_ownership ) ) {
            $error = true;
            if ( is_object( $wp_filesystem ) && $wp_filesystem->errors->has_errors() ) {
                $error = $wp_filesystem->errors;
            }
            // Failed to connect. Error and request again.             $this->skin->request_filesystem_credentials( $error$directories[0]$allow_relaxed_file_ownership );
            return false;
        }

        if ( ! is_object( $wp_filesystem ) ) {
            return new WP_Error( 'fs_unavailable', $this->strings['fs_unavailable'] );
        }

        if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
            return new WP_Error( 'fs_error', $this->strings['fs_error']$wp_filesystem->errors );
        }

        foreach ( (array) $directories as $dir ) {
            switch ( $dir ) {
                case ABSPATH:
                    if ( ! $wp_filesystem->abspath() ) {
                        return new WP_Error( 'fs_no_root_dir', $this->strings['fs_no_root_dir'] );
                    }

    public function validate_cookie( $cookie = '' ) {

        if ( ! $cookie ) {
            if ( empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] ) ) {
                return new WP_Error( 'no_cookie', __( 'No cookie present.' ) );
            }

            $cookie = $_COOKIE[ RECOVERY_MODE_COOKIE ];
        }

        $parts = $this->parse_cookie( $cookie );

        if ( is_wp_error( $parts ) ) {
            return $parts;
        }

        


    /** * Get the parent post, if the ID is valid. * * @since 4.7.2 * * @param int $parent_post_id Supplied ID. * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. */
    protected function get_parent( $parent_post_id ) {
        $error = new WP_Error(
            'rest_post_invalid_parent',
            __( 'Invalid post parent ID.' ),
            array( 'status' => 404 )
        );

        if ( (int) $parent_post_id <= 0 ) {
            return $error;
        }

        $parent_post = get_post( (int) $parent_post_id );

        
'spam'         => 0,
        'deleted'      => 0,
        'lang_id'      => 0,
    );

    $prepared_data = wp_prepare_site_data( $data$defaults );
    if ( is_wp_error( $prepared_data ) ) {
        return $prepared_data;
    }

    if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) {
        return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' )$wpdb->last_error );
    }

    $site_id = (int) $wpdb->insert_id;

    clean_blog_cache( $site_id );

    $new_site = get_site( $site_id );

    if ( ! $new_site ) {
        return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) );
    }

    
$connection_error_str,
                    $connect_timeout,
                    STREAM_CLIENT_CONNECT,
                    $context
                );
            }
        }

        if ( false === $handle ) {
            // SSL connection failed due to expired/invalid cert, or, OpenSSL configuration is broken.             if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str ) {
                return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
            }

            return new WP_Error( 'http_request_failed', $connection_error . ': ' . $connection_error_str );
        }

        // Verify that the SSL certificate is valid for this request.         if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) {
            if ( ! self::verify_ssl_certificate( $handle$parsed_url['host'] ) ) {
                return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
            }
        }

        
'update'       => $update,
            ),
            $redirect
        );
        wp_redirect( $redirect );
        exit;

    case 'resetpassword':
        check_admin_referer( 'bulk-users' );

        if ( ! current_user_can( 'edit_users' ) ) {
            $errors = new WP_Error( 'edit_users', __( 'Sorry, you are not allowed to edit users.' ) );
        }

        if ( empty( $_REQUEST['users'] ) ) {
            wp_redirect( $redirect );
            exit();
        }

        $user_ids = array_map( 'intval', (array) $_REQUEST['users'] );

        $reset_count = 0;

        
public $keys = false;

    /** * Constructor. * * @since 2.7.0 * * @param array $opt */
    public function __construct( $opt = '' ) {
        $this->method = 'ssh2';
        $this->errors = new WP_Error();

        // Check if possible to use ssh2 functions.         if ( ! extension_loaded( 'ssh2' ) ) {
            $this->errors->add( 'no_ssh2_ext', __( 'The ssh2 PHP extension is not available' ) );
            return;
        }

        // Set defaults:         if ( empty( $opt['port'] ) ) {
            $this->options['port'] = 22;
        } else {
            
do_action( 'akismet_spam_caught', $discard );

            if ( $discard ) {
                // The spam is obvious, so we're bailing out early.                 // akismet_result_spam() won't be called so bump the counter here                 if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) {
                    update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr );
                }

                if ( 'rest_api' === $context ) {
                    return new WP_Error( 'akismet_rest_comment_discarded', __( 'Comment discarded.', 'akismet' ) );
                } else if ( 'xml-rpc' === $context ) {
                    // If this is a pingback that we're pre-checking, the discard behavior is the same as the normal spam response behavior.                     return $commentdata;
                } else {
                    // Redirect back to the previous page, or failing that, the post permalink, or failing that, the homepage of the blog.                     $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() );
                    wp_safe_redirect( esc_url_raw( $redirect_to ) );
                    die();
                }
            }
            else if ( 'rest_api' === $context ) {
                

    public function get_items_permissions_check( $request ) {
        if ( current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' ) ) {
            return true;
        }

        $registered = $this->get_collection_params();
        if ( isset( $registered['status']$request['status'] ) && is_array( $request['status'] ) && array( 'active' ) === $request['status'] ) {
            return $this->check_read_active_theme_permission();
        }

        return new WP_Error(
            'rest_cannot_view_themes',
            __( 'Sorry, you are not allowed to view themes.' ),
            array( 'status' => rest_authorization_required_code() )
        );
    }

    /** * Checks if a given request has access to read the theme. * * @since 5.7.0 * * @param WP_REST_Request $request Full details about the request. * @return bool|WP_Error True if the request has read access for the item, otherwise WP_Error object. */
if ( $edit_link ) {
                    $message .= sprintf( ' <a href="%s">%s</a>', $edit_link__( 'Edit user' ) );
                }

                $messages[] = $message;
                break;
            case 'addexisting':
                $messages[] = __( 'That user is already a member of this site.' );
                break;
            case 'could_not_add':
                $add_user_errors = new WP_Error( 'could_not_add', __( 'That user could not be added to this site.' ) );
                break;
            case 'created_could_not_add':
                $add_user_errors = new WP_Error( 'created_could_not_add', __( 'User has been created, but could not be added to this site.' ) );
                break;
            case 'does_not_exist':
                $add_user_errors = new WP_Error( 'does_not_exist', __( 'The requested user does not exist.' ) );
                break;
            case 'enter_email':
                $add_user_errors = new WP_Error( 'enter_email', __( 'Please enter a valid email address.' ) );
                break;
        }
    }
Home | Imprint | This part of the site doesn't use cookies.