Example #1
0
function wpv_duplicate_ct_callback() {
	if ( ! current_user_can( 'manage_options' ) ) {
		$data = array(
			'type' => 'capability',
			'message' => __( 'You do not have permissions for that.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	if ( 
		! isset( $_POST["wpnonce"] )
		|| ! wp_verify_nonce( $_POST["wpnonce"], 'work_view_template' )
	) {
		$data = array(
			'type' => 'nonce',
			'message' => __( 'Your security credentials have expired. Please reload the page to get new ones.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	
	$title = '';
	if ( isset( $_POST['title'] ) ) {
	   $title = sanitize_text_field( $_POST['title'] );
	}
	if ( empty( $title ) ) {
		$data = array(
			'type' => 'title',
			'message' => __( 'You can not create a Content Template with an empty name.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

    // Load the original CT.
    $original_ct_id = $_POST["id"];
    try {
        $original_ct = new WPV_Content_Template( $original_ct_id );
    } catch( Exception $e ) {
		$data = array(
			'type' => 'error',
			'message' => __( 'An unexpected error happened.', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }

    // Check for uniqueness of the new title.
    if( WPV_Content_Template_Embedded::is_name_used( $title ) ) {
		$data = array(
			'type' => 'title',
			'message' => __( 'A Content Template with that name already exists. Please use another name.', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }

    // Clone and report the result.
    $cloned_ct = $original_ct->clone_this( $title, false );

    if ( null == $cloned_ct ) {
		$data = array(
			'type' => 'error',
			'message' => __( 'An unexpected error happened.', 'wpv-views' )
		);
		wp_send_json_error( $data );
    } else {
        wp_send_json_success();
    }
}
    /**
     * 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_Content_Template_Embedded::is_name_used( $sanitized_value, $this->id, $collision_data ) ) {
            switch( $collision_data['colliding_field'] ) {
                case 'post_name':
                    $exception_message = sprintf(
                        __( 'Another Content Template (%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 Content Template already uses this slug value as it\'s title. Please use another slug.', 'wpv-views' );
                    break;
                case 'both':
                    $exception_message = __( 'Another Content Template 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 CT',
                $exception_message,
                WPV_RuntimeExceptionWithMessage::EXCEPTION_VALUE_ALREADY_USED
            );
        }

        return $sanitized_value;
    }