plugin install
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Config\GF_Embed_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Config\GF_Embed_Config_I18N;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Config\GF_Embed_Endpoints_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Dom\GF_Embed_Button;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints\GF_Embed_Endpoint_Create_With_Block;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints\GF_Embed_Endpoint_Get_Posts;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Embed_Service_Provider
|
||||
*
|
||||
* Service provider for the Embed Form Service.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Embed_Form;
|
||||
*/
|
||||
class GF_Embed_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
// Configs
|
||||
const EMBED_CONFIG = 'embed_config';
|
||||
const EMBED_CONFIG_I18N = 'embed_config_i18n';
|
||||
const EMBED_CONFIG_ENDPOINTS = 'embed_config_endpoints';
|
||||
|
||||
// Endpoints
|
||||
const ENDPOINT_GET_POSTS = 'endpoint_get_posts';
|
||||
const ENDPOINT_CREATE_WITH_BLOCK = 'endpoint_create_with_block';
|
||||
|
||||
// DOM
|
||||
const DOM_EMBED_BUTTON = 'dom_embed_button';
|
||||
|
||||
// Strings
|
||||
const ADD_BLOCK_PARAM = 'gfAddBlock';
|
||||
|
||||
/**
|
||||
* Array mapping config class names to their container ID.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $configs = array(
|
||||
self::EMBED_CONFIG => GF_Embed_Config::class,
|
||||
self::EMBED_CONFIG_I18N => GF_Embed_Config_I18N::class,
|
||||
self::EMBED_CONFIG_ENDPOINTS => GF_Embed_Endpoints_Config::class,
|
||||
);
|
||||
|
||||
/**
|
||||
* Register services to the container.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
// Configs
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/config/class-gf-embed-config.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/config/class-gf-embed-config-i18n.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/config/class-gf-embed-endpoints-config.php' );
|
||||
|
||||
// Endpoints
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/endpoints/class-gf-embed-endpoint-get-posts.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/endpoints/class-gf-embed-endpoint-create-with-block.php' );
|
||||
|
||||
// Dom
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/dom/class-gf-embed-button.php' );
|
||||
|
||||
$this->add_configs( $container );
|
||||
$this->add_endpoints( $container );
|
||||
$this->dom( $container );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiailize any actions or hooks.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
add_action( 'wp_ajax_' . GF_Embed_Endpoint_Get_Posts::ACTION_NAME, function () use ( $container ) {
|
||||
$container->get( self::ENDPOINT_GET_POSTS )->handle();
|
||||
} );
|
||||
|
||||
add_action( 'wp_ajax_' . GF_Embed_Endpoint_Create_With_Block::ACTION_NAME, function () use ( $container ) {
|
||||
$container->get( self::ENDPOINT_CREATE_WITH_BLOCK )->handle();
|
||||
} );
|
||||
|
||||
add_action( 'gform_before_toolbar_buttons', function () use ( $container ) {
|
||||
$container->get( self::DOM_EMBED_BUTTON )->output_button();
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* For each config defined in $configs, instantiate and add to container.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_configs( GF_Service_Container $container ) {
|
||||
foreach ( $this->configs as $name => $class ) {
|
||||
$container->add( $name, function () use ( $container, $class ) {
|
||||
return new $class( $container->get( GF_Config_Service_Provider::DATA_PARSER ) );
|
||||
} );
|
||||
|
||||
$container->get( GF_Config_Service_Provider::CONFIG_COLLECTION )->add_config( $container->get( $name ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register AJAX endpoints for the Embed UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_endpoints( GF_Service_Container $container ) {
|
||||
$container->add( self::ENDPOINT_GET_POSTS, function () use ( $container ) {
|
||||
return new GF_Embed_Endpoint_Get_Posts();
|
||||
} );
|
||||
|
||||
$container->add( self::ENDPOINT_CREATE_WITH_BLOCK, function () use ( $container ) {
|
||||
return new GF_Embed_Endpoint_Create_With_Block();
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register DOM-related services.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function dom( GF_Service_Container $container ) {
|
||||
$container->add( self::DOM_EMBED_BUTTON, function() {
|
||||
return new GF_Embed_Button();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
|
||||
/**
|
||||
* Config items for the Embed Form I18N
|
||||
*
|
||||
* @since 2.6
|
||||
*/
|
||||
class GF_Embed_Config_I18N extends GF_Config {
|
||||
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
|
||||
/**
|
||||
* Determine if the config should enqueue its data.
|
||||
*
|
||||
* @since 2.6.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_enqueue() {
|
||||
return \GFCommon::is_form_editor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
return array(
|
||||
'components' => array(
|
||||
'embed_form' => array(
|
||||
'i18n' => array(
|
||||
'title' => esc_html__( 'Embed Form', 'gravityforms' ),
|
||||
'id' => esc_html__( 'Form ID: %s', 'gravityforms' ),
|
||||
'add_title' => esc_html__( 'Add to Existing Content', 'gravityforms' ),
|
||||
'add_post_type_choice_label' => esc_html__( '%1$sAdd to Existing Content:%2$s %3$s', 'gravityforms' ),
|
||||
'add_dropdown_placeholder' => esc_html__( 'Select a %s', 'gravityforms' ),
|
||||
'add_trigger_aria_text' => esc_html__( 'Select a post', 'gravityforms' ),
|
||||
'add_search_aria_text' => esc_html__( 'Search all %ss', 'gravityforms' ),
|
||||
'add_button_label' => esc_html__( 'Insert Form', 'gravityforms' ),
|
||||
'create_title' => esc_html__( 'Create New', 'gravityforms' ),
|
||||
'create_post_type_choice_label' => esc_html__( '%1$sCreate New:%2$s %3$s', 'gravityforms' ),
|
||||
'create_placeholder' => esc_html__( 'Enter %s Name', 'gravityforms' ),
|
||||
'create_button_label' => esc_html__( 'Create', 'gravityforms' ),
|
||||
'dialog_title' => esc_html__( 'Unsaved Changes', 'gravityforms' ),
|
||||
'dialog_content' => esc_html__( 'Oops! You have unsaved changes in the form, before you can continue with embedding it please save your changes.', 'gravityforms' ),
|
||||
'dialog_confirm_text' => esc_html__( 'Save Changes', 'gravityforms' ),
|
||||
'dialog_confirm_saving' => esc_html__( 'Saving', 'gravityforms' ),
|
||||
'dialog_cancel_text' => esc_html__( 'Cancel', 'gravityforms' ),
|
||||
'dialog_close_title' => esc_html__( 'Close this dialog and return to form editor.', 'gravityforms' ),
|
||||
'shortcode_title' => esc_html__( 'Not Using the Block Editor?', 'gravityforms' ),
|
||||
'shortcode_description' => esc_html__( 'Copy and paste the shortcode within your page builder.', 'gravityforms' ),
|
||||
'shortcode_button_label' => esc_html__( 'Copy Shortcode', 'gravityforms' ),
|
||||
'shortcode_button_copied' => esc_html__( 'Copied', 'gravityforms' ),
|
||||
'shortcode_helper' => esc_html__( '%1$sLearn more%2$s about the shortcode.', 'gravityforms' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
|
||||
/**
|
||||
* Config items for the Embed Form UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*/
|
||||
class GF_Embed_Config extends GF_Config {
|
||||
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
|
||||
/**
|
||||
* Determine if the config should enqueue its data.
|
||||
*
|
||||
* @since 2.6.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_enqueue() {
|
||||
return \GFCommon::is_form_editor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
return array(
|
||||
'components' => array(
|
||||
'embed_form' => array(
|
||||
'urls' => $this->get_urls(),
|
||||
'data' => array(
|
||||
'form_id' => array(
|
||||
'value' => rgget( 'id' ),
|
||||
'default' => 1,
|
||||
),
|
||||
'post_types' => array(
|
||||
'value' => $this->get_available_post_types(),
|
||||
'default' => $this->placeholder_post_types(),
|
||||
),
|
||||
'items' => array(
|
||||
'value' => $this->get_items_by_type(),
|
||||
'default' => $this->placeholder_items(),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the various URLs for the Embed UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_urls() {
|
||||
$edit_link = '';
|
||||
|
||||
$post_type_object = get_post_type_object( 'page' );
|
||||
if ( ! empty( $post_type_object->_edit_link ) ) {
|
||||
$edit_link = admin_url( str_replace( '%d', '%1$s', $post_type_object->_edit_link ) . '&action=edit' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the edit post link to be customized.
|
||||
*
|
||||
* @since 2.6.2
|
||||
*
|
||||
* @param string $link The edit link. Use %1$s as the placeholder for the ID.
|
||||
*/
|
||||
$edit_link = apply_filters( 'gform_embed_edit_post_link', $edit_link );
|
||||
|
||||
return [
|
||||
'edit_post' => [
|
||||
'value' => $edit_link,
|
||||
'default' => 'https://gravity.loc/wp-admin/post.php?post=%1$s&action=edit',
|
||||
],
|
||||
'shortcode_docs' => 'https://docs.gravityforms.com/shortcodes/',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Post Types data for the Embed UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_available_post_types() {
|
||||
$types = array(
|
||||
array(
|
||||
'slug' => 'page',
|
||||
'label' => get_post_type_object( 'page' )->labels->singular_name,
|
||||
),
|
||||
array(
|
||||
'slug' => 'post',
|
||||
'label' => get_post_type_object( 'post' )->labels->singular_name,
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Allows users to modify the post types sent as selectable options in the Embed UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param array $types
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
return apply_filters( 'gform_embed_post_types', $types );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the items to localize for each post type.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_items_by_type() {
|
||||
$types = $this->get_available_post_types();
|
||||
$data = array();
|
||||
foreach ( $types as $type ) {
|
||||
$slug = $type['slug'];
|
||||
$label = $type['label'];
|
||||
|
||||
$items = get_posts( array( 'post_type' => $slug, 'posts_per_page' => 5 ) );
|
||||
array_walk( $items, function ( &$item ) {
|
||||
$item = array(
|
||||
'value' => $item->ID,
|
||||
'label' => $item->post_title,
|
||||
);
|
||||
} );
|
||||
|
||||
$data[ $slug ]['entries'] = $items;
|
||||
$data[ $slug ]['count'] = $this->get_total_posts_by_type( $slug );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the totals for the given post type.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param string $type - The Post Type to query for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_total_posts_by_type( $type ) {
|
||||
$args = array(
|
||||
'post_type' => $type,
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
$query = new \WP_Query( $args );
|
||||
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the placeholder post type values for use in Mocks.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function placeholder_post_types() {
|
||||
return array(
|
||||
array( 'slug' => 'page', 'label' => __( 'Page', 'gravityforms' ) ),
|
||||
array( 'slug' => 'post', 'label' => __( 'Post', 'gravityforms' ) ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the placeholder post items for use in Mocks.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function placeholder_items() {
|
||||
return array(
|
||||
'post' => array(
|
||||
'count' => 2,
|
||||
'entries' => array(
|
||||
array(
|
||||
'value' => 1,
|
||||
'label' => 'Post One',
|
||||
),
|
||||
array(
|
||||
'value' => 2,
|
||||
'label' => 'Post Two',
|
||||
),
|
||||
),
|
||||
),
|
||||
'page' => array(
|
||||
'count' => 25,
|
||||
'entries' => array(
|
||||
array(
|
||||
'value' => 3,
|
||||
'label' => 'Page Three',
|
||||
),
|
||||
array(
|
||||
'value' => 4,
|
||||
'label' => 'Page Four',
|
||||
),
|
||||
array(
|
||||
'value' => 5,
|
||||
'label' => 'Page Five',
|
||||
),
|
||||
array(
|
||||
'value' => 6,
|
||||
'label' => 'Page Six',
|
||||
),
|
||||
array(
|
||||
'value' => 7,
|
||||
'label' => 'Page Seven',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints\GF_Embed_Endpoint_Create_With_Block;
|
||||
use Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints\GF_Embed_Endpoint_Get_Posts;
|
||||
|
||||
/**
|
||||
* Config items for the Embed Forms REST Endpoints.
|
||||
*
|
||||
* @since 2.6
|
||||
*/
|
||||
class GF_Embed_Endpoints_Config extends GF_Config {
|
||||
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $overwrite = false;
|
||||
|
||||
/**
|
||||
* Determine if the config should enqueue its data.
|
||||
*
|
||||
* @since 2.6.2
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_enqueue() {
|
||||
return \GFCommon::is_form_editor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
return array(
|
||||
'components' => array(
|
||||
'embed_form' => array(
|
||||
'endpoints' => $this->get_endpoints(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the various endpoints for the Embed UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_endpoints() {
|
||||
return array(
|
||||
|
||||
// Endpoint to get posts for typeahead
|
||||
'get_posts' => array(
|
||||
'action' => array(
|
||||
'value' => 'gf_embed_query_posts',
|
||||
'default' => 'mock_endpoint',
|
||||
),
|
||||
'nonce' => array(
|
||||
'value' => wp_create_nonce( GF_Embed_Endpoint_Get_Posts::ACTION_NAME ),
|
||||
'default' => 'nonce',
|
||||
)
|
||||
),
|
||||
|
||||
// Endpoint to create a new page with our block inserted.
|
||||
'create_post_with_block' => array(
|
||||
'action' => array(
|
||||
'value' => GF_Embed_Endpoint_Create_With_Block::ACTION_NAME,
|
||||
'default' => 'mock_endpoint',
|
||||
),
|
||||
'nonce' => array(
|
||||
'value' => wp_create_nonce( GF_Embed_Endpoint_Create_With_Block::ACTION_NAME ),
|
||||
'default' => 'nonce',
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Dom;
|
||||
|
||||
/**
|
||||
* Handle outputting the Embed Button in the UI.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Embed_Form\Dom
|
||||
*/
|
||||
class GF_Embed_Button {
|
||||
|
||||
/**
|
||||
* Output the HTML for the Embed Button.
|
||||
*/
|
||||
public function output_button() {
|
||||
?>
|
||||
<button data-js="embed-flyout-trigger" class="gform-button gform-button--white gform-button--icon-leading">
|
||||
<i class="gform-button__icon gform-icon gform-icon--embed-alt"></i>
|
||||
<?php _e( 'Embed', 'gravityforms' ); ?>
|
||||
</button>
|
||||
<?php
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints;
|
||||
|
||||
/**
|
||||
* AJAX Endpoint for creating a new post with a specific block already added.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints
|
||||
*/
|
||||
class GF_Embed_Endpoint_Create_With_Block {
|
||||
|
||||
// Strings
|
||||
const ACTION_NAME = 'gf_embed_create_post_with_block';
|
||||
|
||||
// Request Params
|
||||
const PARAM_FORM_ID = 'form_id';
|
||||
const PARAM_POST_TYPE = 'post_type';
|
||||
const PARAM_POST_TITLE = 'post_title';
|
||||
|
||||
/**
|
||||
* Handle the AJAX request.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle() {
|
||||
check_ajax_referer( self::ACTION_NAME );
|
||||
|
||||
$post_type = rgpost( self::PARAM_POST_TYPE );
|
||||
$form_id = rgpost( self::PARAM_FORM_ID );
|
||||
$post_title = rgpost( self::PARAM_POST_TITLE );
|
||||
|
||||
if ( empty( $post_type ) || empty( $form_id ) ) {
|
||||
wp_send_json_error( 'Request must include a post_type and form_id.', 400 );
|
||||
}
|
||||
|
||||
$post_data = array(
|
||||
'post_title' => $post_title,
|
||||
'post_type' => $post_type,
|
||||
'post_content' => $this->get_content_for_form( $form_id ),
|
||||
);
|
||||
|
||||
$new_id = wp_insert_post( $post_data );
|
||||
|
||||
wp_send_json_success( array( 'ID' => $new_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the properly-formatted comment string for the block we're inserting.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $form_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_content_for_form( $form_id ) {
|
||||
$attrs = array(
|
||||
'formId' => $form_id
|
||||
);
|
||||
|
||||
return get_comment_delimited_block_content( 'gravityforms/form', $attrs, '' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints;
|
||||
|
||||
/**
|
||||
* AJAX Endpoint for getting posts based on search params.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Embed_Form\Endpoints
|
||||
*/
|
||||
class GF_Embed_Endpoint_Get_Posts {
|
||||
|
||||
// Strings
|
||||
const ACTION_NAME = 'gf_embed_query_posts';
|
||||
|
||||
// Parameters
|
||||
const PARAM_OFFSET = 'offset';
|
||||
const PARAM_COUNT = 'count';
|
||||
const PARAM_STATUS = 'status';
|
||||
const PARAM_SEARCH = 'search';
|
||||
const PARAM_POST_TYPE = 'post_type';
|
||||
|
||||
// Defaults
|
||||
const DEFAULT_OFFSET = 0;
|
||||
const DEFAULT_COUNT = 10;
|
||||
const DEFAULT_STATUS = 'publish';
|
||||
const DEFAULT_SEARCH = '';
|
||||
const DEFAULT_POST_TYPE = 'post';
|
||||
|
||||
/**
|
||||
* Handle the AJAX request.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle() {
|
||||
check_ajax_referer( self::ACTION_NAME );
|
||||
|
||||
$offset = rgpost( self::PARAM_OFFSET ) ? rgpost( self::PARAM_OFFSET ) : self::DEFAULT_OFFSET;
|
||||
$count = rgpost( self::PARAM_COUNT ) ? rgpost( self::PARAM_COUNT ) : self::DEFAULT_COUNT;
|
||||
$status = rgpost( self::PARAM_STATUS ) ? rgpost( self::PARAM_STATUS ) : self::DEFAULT_STATUS;
|
||||
$search = rgpost( self::PARAM_SEARCH ) ? rgpost( self::PARAM_SEARCH ) : self::DEFAULT_SEARCH;
|
||||
$post_type = rgpost( self::PARAM_POST_TYPE ) ? rgpost( self::PARAM_POST_TYPE ) : self::DEFAULT_POST_TYPE;
|
||||
|
||||
$args = array(
|
||||
'post_type' => $post_type,
|
||||
'post_status' => $status,
|
||||
'posts_per_page' => $count,
|
||||
'offset' => $offset,
|
||||
's' => $search,
|
||||
);
|
||||
|
||||
$query = new \WP_Query( $args );
|
||||
|
||||
$posts = $query->get_posts();
|
||||
|
||||
array_walk( $posts, function ( &$post ) {
|
||||
$post = array(
|
||||
'value' => $post->ID,
|
||||
'label' => $post->post_title,
|
||||
);
|
||||
} );
|
||||
|
||||
wp_send_json_success( $posts );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user