Пример #1
0
/**
 * View duplicate callback function.
 *
 * Expects following POST arguments:
 * - wpnonce: A valid wpv_duplicate_view_nonce.
 * - id: View ID.
 * - name: Name of the new View.
 *
 * Refer to WPV_View::duplicate() for more information about the duplication itself.
 *
 * @since unknown
 */
function wpv_duplicate_this_view_callback() {
	wpv_ajax_authenticate( 'wpv_duplicate_view_nonce', array( 'parameter_source' => 'post', 'type_of_death' => 'data' ) );
	
    $post_id = (int) wpv_getpost( 'id', 0 );
    $post_name= sanitize_text_field( wpv_getpost( 'name', '' ) );
	if ( ( 0 == $post_id ) || empty( $post_name ) ) {
		$data = array(
			'message' => __('Wrong data', 'wpv-views')
		);
		wp_send_json_error( $data );
	}

    if ( WPV_View_Base::is_name_used( $post_name ) ) {
        $data = array(
			'message' => __( 'A View with that name already exists. Please use another name', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

    // Get the original View.
    $original_view = WPV_View::get_instance( $post_id );
    if( null == $original_view ) {
		$data = array(
			'message' => __('Wrong data', 'wpv-views')
		);
		wp_send_json_error( $data );
    }
    
    $duplicate_view_id = $original_view->duplicate( $post_name );
    if ( $duplicate_view_id ) {
        // original post id (shouldn't we rather return new id?)
        wp_send_json_success();
    } else {
        $data = array(
			'message' => __( 'Unexpected error', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }

}
Пример #2
0
    /**
     * Create a duplicate of this View.
     *
     * Clone the View and most of it's postmeta. If there is a Loop Template assigned,
     * duplicate that as well and update references (in the appropriate postmeta,
     * in shortcodes in loop output, etc.) in the duplicated View.
     *
     * @todo more detailed description
     *
     * @param string $new_post_title Title of the new View. Must not be used in any
     *     existing View or WPA.
     *
     * @return bool|int ID of the new View or false on error.
     */
    public function duplicate( $new_post_title ) {

        // Sanitize and validate
        $new_post_title = sanitize_text_field( $new_post_title );
        if( empty( $new_post_title ) ) {
            return false;
        }

        if( WPV_View_Base::is_name_used( $new_post_title ) ) {
            return false;
        }

        // Clone existing View post object
        $new_post = (array) clone( $this->post() );
        $new_post['post_title'] = $new_post_title;

        $keys_to_unset = array( 'ID', 'post_name', 'post_date', 'post_date_gmt' );
        foreach( $keys_to_unset as $key ) {
            unset( $new_post[ $key ] );
        }

        $new_post_id = wp_insert_post( $new_post );

        // Clone existing View postmeta
        $postmeta_keys_to_copy = array( '_wpv_settings', '_wpv_layout_settings', '_wpv_description' );

        $new_postmeta_values = array();
        foreach ( $postmeta_keys_to_copy as $key ) {
            $new_postmeta_values[ $key ] = $this->get_postmeta( $key );
        }

        // If this View has a loop Template, we need to clone it and adjust the layout settings.
        if ( $this->has_loop_template ) {
            $new_postmeta_values = $this->duplicate_loop_template( $new_postmeta_values, $new_post_id, $new_post_title );
        }

        // Update postmeta of the new View.
        foreach ( $new_postmeta_values as $meta_key => $meta_value ) {
            update_post_meta( $new_post_id, $meta_key, $meta_value );
        }

        return $new_post_id;
    }
Пример #3
0
    /**
     * Post slug validation.
     *
     * Accepts a non-empty value containing only lowercase letters, numbers or dashes.
     *
     * @param string $value New post slug.
     * @return string Sanitized value safe to be used.
     * @throws WPV_RuntimeExceptionWithMessage
     * @since 1.9
     */
    protected function _validate_slug( $value ) {

        $sanitized_value = sanitize_title( $value );
        if( $value != $sanitized_value ) {
            throw new WPV_RuntimeExceptionWithMessage(
                '_validate_slug failed: invalid characters',
                __( 'The slug can only contain lowercase latin letters, numbers or dashes.', 'wpv-views' )
            );
        }

        if( empty( $sanitized_value ) ) {
            throw new WPV_RuntimeExceptionWithMessage(
                '_validate_slug failed: empty value',
                __( 'You can not leave the slug empty.', 'wpv-views' )
            );
        }

        $collision_data = array();
        if( WPV_View_Base::is_name_used( $sanitized_value, $this->id, $collision_data ) ) {
            switch( $collision_data['colliding_field'] ) {
                case 'post_name':
                    $exception_message = sprintf(
                        __( 'Another item (%s) with that slug already exists. Please use another slug.', 'wpv-views' ),
                        sanitize_text_field( $collision_data['post_title'] )
                    );
                    break;
                case 'post_title':
                    $exception_message = __( 'Another item already uses this slug value as it\'s title. Please use another slug.', 'wpv-views' );
                    break;
                case 'both':
                    $exception_message = __( 'Another item already uses this slug value as it\'s slug and title. Please use another slug.', 'wpv-views' );
                    break;
                default:
                    $exception_message = __( 'Another item with that slug or title already exists. Please use another slug.', 'wpv-views' );
                    break;
            }
            throw new WPV_RuntimeExceptionWithMessage(
                '_validate_slug failed: name is already being used for another View/WPA',
                $exception_message,
                WPV_RuntimeExceptionWithMessage::EXCEPTION_VALUE_ALREADY_USED
            );
        }

        return $sanitized_value;
    }