initial
This commit is contained in:
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Form_Switcher;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Form_Switcher\Config\GF_Form_Switcher_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Form_Switcher\Endpoints\GF_Form_Switcher_Endpoint_Get_Forms;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Form_Switcher_Service_Provider
|
||||
*
|
||||
* Service provider for the Form_Switcher Service.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Form_Switcher;
|
||||
*/
|
||||
class GF_Form_Switcher_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
// Configs
|
||||
const FORM_SWITCHER_CONFIG = 'form_switcher_config';
|
||||
|
||||
const ENDPOINT_GET_FORMS = 'endpoint_get_forms';
|
||||
|
||||
/**
|
||||
* Array mapping config class names to their container ID.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $configs = array(
|
||||
self::FORM_SWITCHER_CONFIG => GF_Form_Switcher_Config::class,
|
||||
);
|
||||
|
||||
/**
|
||||
* Register services to the container.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
// Configs
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/config/class-gf-form-switcher-config.php' );
|
||||
$this->add_configs( $container );
|
||||
|
||||
// Endpoints
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/endpoints/class-gf-form-switcher-endpoint-get-forms.php' );
|
||||
$this->add_endpoints( $container );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize any actions or hooks.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
add_action( 'wp_ajax_' . GF_Form_Switcher_Endpoint_Get_Forms::ACTION_NAME, function () use ( $container ) {
|
||||
$container->get( self::ENDPOINT_GET_FORMS )->handle();
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* For each config defined in $configs, instantiate and add to container.
|
||||
*
|
||||
* @since 2.9.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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For each endpoint defined in $endpoints, instantiate and add to container.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_endpoints( GF_Service_Container $container ) {
|
||||
$container->add( self::ENDPOINT_GET_FORMS, function () {
|
||||
return new GF_Form_Switcher_Endpoint_Get_Forms();
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Form_Switcher\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use GFForms;
|
||||
|
||||
/**
|
||||
* Config items for Form_Switcher.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*/
|
||||
class GF_Form_Switcher_Config extends GF_Config {
|
||||
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
|
||||
/**
|
||||
* Only enqueue in the admin.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_enqueue() {
|
||||
return GFForms::is_gravity_page();
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
return [
|
||||
'components' => [
|
||||
'form_switcher' => [
|
||||
'endpoints' => $this->get_endpoints(),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the endpoints for the Form Switcher.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_endpoints() {
|
||||
return [
|
||||
'get_forms' => [
|
||||
'action' => [
|
||||
'value' => 'gf_form_switcher_get_forms',
|
||||
'default' => 'mock_endpoint',
|
||||
],
|
||||
'nonce' => [
|
||||
'value' => wp_create_nonce( 'gf_form_switcher_get_forms' ),
|
||||
'default' => 'nonce',
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Form_Switcher\Endpoints;
|
||||
|
||||
use GFFormsModel;
|
||||
use GFForms;
|
||||
|
||||
/**
|
||||
* AJAX Endpoint for getting a form based on a search query.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Form_Switcher\Endpoints
|
||||
*/
|
||||
class GF_Form_Switcher_Endpoint_Get_Forms {
|
||||
|
||||
// Strings
|
||||
const ACTION_NAME = 'gf_form_switcher_get_forms';
|
||||
|
||||
// Parameters
|
||||
const PARAM_SEARCH = 'search';
|
||||
|
||||
// Defaults
|
||||
const DEFAULT_SEARCH = '';
|
||||
|
||||
/**
|
||||
* Handle the AJAX request.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle() {
|
||||
check_ajax_referer( self::ACTION_NAME );
|
||||
|
||||
$search = rgpost( self::PARAM_SEARCH ) ? rgpost( self::PARAM_SEARCH ) : self::DEFAULT_SEARCH;
|
||||
|
||||
$forms = GFFormsModel::search_forms( $search );
|
||||
|
||||
/* Filters the form switcher search results.
|
||||
*
|
||||
* @since 2.9.6
|
||||
*
|
||||
* @param array $forms The list of forms retrieved by the search.
|
||||
*/
|
||||
$forms = (array) apply_filters( 'gform_form_switcher_forms', $forms );
|
||||
|
||||
$results = array();
|
||||
|
||||
foreach ( $forms as $form ) {
|
||||
if ( is_numeric( $form ) ) {
|
||||
$form = GFFormsModel::get_form( $form );
|
||||
}
|
||||
|
||||
if ( ! is_object( $form ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$results[] = [
|
||||
'value' => $form->id,
|
||||
'label' => $form->title,
|
||||
'attributes' => [
|
||||
GFForms::get_form_switcher_results_page_attr( $form->id ),
|
||||
GFForms::get_form_switcher_subview_attr( $form->id ),
|
||||
"title = '" . esc_attr( $form->title ) . "'",
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
wp_send_json_success( $results );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user