plugin install
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\License\GF_License_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Config\GF_Template_Library_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Endpoints\GF_Create_Form_Template_Library_Endpoint;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Template_Library_File_Store;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Template_Library_Array_Store;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Store;
|
||||
|
||||
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Template_Library_Service_Provider
|
||||
*
|
||||
* Service provider for the Template_Library Service.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Template_Library;
|
||||
*/
|
||||
class GF_Template_Library_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
// Configs.
|
||||
const TEMPLATE_LIBRARY_CONFIG = 'template_library_config';
|
||||
|
||||
/**
|
||||
* Array mapping config class names to their container ID.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $configs = array(
|
||||
self::TEMPLATE_LIBRARY_CONFIG => GF_Template_Library_Config::class,
|
||||
);
|
||||
|
||||
// Endpoint label.
|
||||
const ENDPOINT_CREATE_FROM_TEMPLATE = 'create_from_template';
|
||||
|
||||
/**
|
||||
* The endpoint class names and their corresponding string keys in the service container.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $endpoints = array(
|
||||
self::ENDPOINT_CREATE_FROM_TEMPLATE => GF_Create_Form_Template_Library_Endpoint::class,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* The data store configuration.
|
||||
*
|
||||
* @var array $template_data_configurations The data store configuration.
|
||||
*/
|
||||
protected $template_data_configurations;
|
||||
|
||||
/**
|
||||
* Register services to the container.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @param GF_Service_Container $container The service container.
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
// Templates store.
|
||||
require_once plugin_dir_path( __FILE__ ) . '/templates/class-gf-template-library-templates-store.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/templates/class-gf-template-library-file-store.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/templates/class-gf-template-library-array-store.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/templates/class-gf-template-library-template.php';
|
||||
// Configs.
|
||||
require_once plugin_dir_path( __FILE__ ) . '/config/class-gf-template-library-config.php';
|
||||
// Endpoints.
|
||||
require_once plugin_dir_path( __FILE__ ) . '/endpoints/class-gf-create-form-template-endpoint.php';
|
||||
|
||||
$this->template_data_configurations = array(
|
||||
'data_store' => array(
|
||||
'type' => GF_Template_Library_Array_Store::class,
|
||||
'config' => array(
|
||||
'uri' => \GFCommon::get_base_path() . '/includes/template-library/templates/templates.php',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->add_data_store( $container );
|
||||
$this->add_configs( $container );
|
||||
$this->add_endpoints( $container );
|
||||
$this->register_template_library_app();
|
||||
}
|
||||
|
||||
private function register_template_library_app() {
|
||||
$dev_min = defined( 'GF_SCRIPT_DEBUG' ) && GF_SCRIPT_DEBUG ? '' : '.min';
|
||||
|
||||
$args = array(
|
||||
'app_name' => 'template_library',
|
||||
'script_name' => 'gform_gravityforms_admin_vendors',
|
||||
'object_name' => 'gform_admin_config',
|
||||
'chunk' => './template-library',
|
||||
'enqueue' => array( $this, 'should_enqueue_library' ),
|
||||
'css' => array(
|
||||
'handle' => 'template_library_styles',
|
||||
'src' => \GFCommon::get_base_url() . "/assets/css/dist/template-library{$dev_min}.css",
|
||||
'deps' => array( 'gform_admin_components' ),
|
||||
'ver' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? filemtime( \GFCommon::get_base_path() . "/assets/css/dist/template-library{$dev_min}.css" ) : \GFForms::$version,
|
||||
),
|
||||
'root_element' => 'gf-template-library',
|
||||
);
|
||||
|
||||
$this->register_app( $args );
|
||||
}
|
||||
|
||||
public function should_enqueue_library() {
|
||||
$current_page = trim( strtolower( rgget( 'page' ) ) );
|
||||
$gf_pages = array( 'gf_edit_forms', 'gf_new_form' );
|
||||
|
||||
if ( $current_page === 'gf_edit_forms' ) {
|
||||
return empty( rgget( 'id' ) );
|
||||
}
|
||||
|
||||
return in_array( $current_page, $gf_pages );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize any actions or hooks.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Service_Container $container The service container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
// add hooks or filters here.
|
||||
add_action(
|
||||
'wp_ajax_' . GF_Create_Form_Template_Library_Endpoint::ACTION_NAME,
|
||||
function () use ( $container ) {
|
||||
$container->get( self::ENDPOINT_CREATE_FROM_TEMPLATE )->handle();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the templates' data store service.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Service_Container $container The service container.
|
||||
*/
|
||||
public function add_data_store( GF_Service_Container $container ) {
|
||||
$container->add(
|
||||
$this->template_data_configurations['data_store']['type'],
|
||||
function () use ( $container ) {
|
||||
return new $this->template_data_configurations['data_store']['type']( $this->template_data_configurations['data_store']['config'] );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* For each config defined in $configs, instantiate and add to container.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Service_Container $container The service container.
|
||||
*/
|
||||
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( $this->template_data_configurations['data_store']['type'] ),
|
||||
$container->get( GF_License_Service_Provider::LICENSE_API_CONNECTOR )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->get( GF_Config_Service_Provider::CONFIG_COLLECTION )->add_config( $container->get( $name ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Creating Forms Endpoints.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Service_Container $container The service container.
|
||||
*/
|
||||
private function add_endpoints( GF_Service_Container $container ) {
|
||||
foreach ( $this->endpoints as $name => $class ) {
|
||||
$container->add(
|
||||
$name,
|
||||
function () use ( $container, $class ) {
|
||||
return new $class(
|
||||
$container->get( $this->template_data_configurations['data_store']['type'] )
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Data_Parser;
|
||||
use Gravity_Forms\Gravity_Forms\License\GF_License_API_Connector;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Endpoints\GF_Create_Form_Template_Library_Endpoint;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Store;
|
||||
|
||||
/**
|
||||
* Config items for Template_Library.
|
||||
*
|
||||
* @since
|
||||
*/
|
||||
class GF_Template_Library_Config extends GF_Config {
|
||||
|
||||
/**
|
||||
* The object name for this config.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'gform_admin_config';
|
||||
|
||||
/**
|
||||
* The ID of the script to localize the data to.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
|
||||
/**
|
||||
* The templates' data store to retrieve the templates' data from.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var GF_Templates_Store $templates_repos
|
||||
*/
|
||||
protected $templates_store;
|
||||
|
||||
/**
|
||||
* The license API connector to get license information.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var GF_License_API_Connector
|
||||
*/
|
||||
protected $license_api;
|
||||
|
||||
/**
|
||||
* Config class constructore.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Config_Data_Parser $parser Data Parser.
|
||||
* @param GF_Templates_Store $templates_store The templates' data store to retrieve the templates' data from.
|
||||
*/
|
||||
public function __construct( GF_Config_Data_Parser $parser, GF_Templates_Store $templates_store, GF_License_API_Connector $license_api ) {
|
||||
parent::__construct( $parser );
|
||||
$this->templates_store = $templates_store;
|
||||
$this->license_api = $license_api;
|
||||
}
|
||||
|
||||
public function should_enqueue() {
|
||||
$current_page = trim( strtolower( rgget( 'page' ) ) );
|
||||
$gf_pages = array( 'gf_edit_forms', 'gf_new_form' );
|
||||
|
||||
return in_array( $current_page, $gf_pages );
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
$license_info = $this->license_api->check_license();
|
||||
$bypassTemplateLibrary = apply_filters('gform_bypass_template_library', false);
|
||||
|
||||
return array(
|
||||
'components' => array(
|
||||
'template_library' => array(
|
||||
'endpoints' => $this->get_endpoints(),
|
||||
'i18n' => array(
|
||||
'description' => __( 'Form Description', 'gravityforms' ),
|
||||
'title' => __( 'Form Title', 'gravityforms' ),
|
||||
'titlePlaceholder' => __( 'Enter the form title', 'gravityforms' ),
|
||||
'required' => __( 'Required', 'gravityforms' ),
|
||||
'useTemplate' => __( 'Use Template', 'gravityforms' ),
|
||||
'closeButton' => __( 'Close', 'gravityforms' ),
|
||||
/* translators: title of template */
|
||||
'useTemplateWithTitle' => __( 'Use Template %s', 'gravityforms' ),
|
||||
'createActiveText' => __( 'Creating Form', 'gravityforms' ),
|
||||
'missingTitle' => __( 'Please enter a valid form title.', 'gravityforms' ),
|
||||
'duplicateTitle' => __( 'Please enter a unique form title.', 'gravityforms' ),
|
||||
'failedRequest' => __( 'There was an issue creating your form.', 'gravityforms' ),
|
||||
'failedRequestDialogTitle' => __( 'Import failed.', 'gravityforms' ),
|
||||
'importErrorCloseText' => __( 'Close.', 'gravityforms' ),
|
||||
/* translators: title of template */
|
||||
'previewWithTitle' => __( 'Preview %s', 'gravityforms' ),
|
||||
'cancel' => __( 'Cancel', 'gravityforms' ),
|
||||
'blankForm' => __( 'Blank Form', 'gravityforms' ),
|
||||
'createForm' => __( 'Create Blank Form', 'gravityforms' ),
|
||||
'blankFormTitle' => __( 'New Blank Form', 'gravityforms' ),
|
||||
'blankFormDescription' => __( 'A new blank form', 'gravityforms' ),
|
||||
'formDescriptionPlaceHolder' => __( 'A form description goes here', 'gravityforms' ),
|
||||
'heading' => __( 'Explore Form Templates', 'gravityforms' ),
|
||||
'subheading' => __( 'Quickly create an amazing form by using a pre-made template, or start from scratch to tailor your form to your specific needs.', 'gravityforms' ),
|
||||
'upgradeTag' => __( 'Upgrade', 'gravityforms' ),
|
||||
'upgradeAlert' => array(
|
||||
/* translators: %1$s is anchor opening tag, %2$s is anchor closing tag */
|
||||
'value' => sprintf( __( 'This template uses Add-ons not included in your current license plan. %1$sUpgrade%2$s'), '<a href="' . $license_info->get_upgrade_link() . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
|
||||
'default' => 'This template uses Add-ons not included in your current license plan. Upgrade.',
|
||||
),
|
||||
),
|
||||
'data' => array(
|
||||
'thumbnail_url' => \GFCommon::get_image_url( 'template-library/' ),
|
||||
'layout' => 'full-screen',
|
||||
'templates' => $bypassTemplateLibrary ? array() : array_values( $this->get_templates() ),
|
||||
'licenseType' => $license_info->get_data_value( 'product_code' ),
|
||||
'defaults' => array(
|
||||
'isLibraryOpen' => rgget( 'page' ) === 'gf_new_form',
|
||||
'flyoutOpen' => (bool)$bypassTemplateLibrary,
|
||||
'flyoutFooterButtonLabel' => $bypassTemplateLibrary ? __( 'Create Form', 'gravityforms' ) : '',
|
||||
'flyoutTitleValue' => '',
|
||||
'flyoutDescriptionValue' => '',
|
||||
'selectedTemplate' => array(
|
||||
'title' => __( 'New Form', 'gravityforms' ),
|
||||
'description' => __( 'A new form', 'gravityforms' ),
|
||||
'id' => 'blank',
|
||||
),
|
||||
'flyoutTitleErrorState' => false,
|
||||
'flyoutTitleErrorMessage' => '',
|
||||
'importError' => false,
|
||||
'flyoutPrimaryLoadingState' => false,
|
||||
'bypassTemplateLibrary' => $bypassTemplateLibrary,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the endpoints for handling form creation in the template library.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return \array[][]
|
||||
*/
|
||||
private function get_endpoints() {
|
||||
return array(
|
||||
'create_from_template' => array(
|
||||
'action' => array(
|
||||
'value' => GF_Create_Form_Template_Library_Endpoint::ACTION_NAME,
|
||||
'default' => 'mock_endpoint',
|
||||
),
|
||||
'nonce' => array(
|
||||
'value' => wp_create_nonce( GF_Create_Form_Template_Library_Endpoint::ACTION_NAME ),
|
||||
'default' => 'nonce',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of the available templates from the data store.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_templates() {
|
||||
return $this->templates_store->all();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Endpoints;
|
||||
|
||||
use GFForms;
|
||||
use Gravity_Forms\Gravity_Forms\Save_Form\GF_Save_Form_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Config\GF_Template_Library_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Template_Library_Template;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Repository;
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Store;
|
||||
|
||||
/**
|
||||
* AJAX Endpoint for Creating a form from a template.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Template_Library\Endpoints
|
||||
*/
|
||||
class GF_Create_Form_Template_Library_Endpoint {
|
||||
|
||||
// AJAX action name.
|
||||
const ACTION_NAME = 'create_from_template';
|
||||
|
||||
/**
|
||||
* The template id to import.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $template_id
|
||||
*/
|
||||
protected $template_id;
|
||||
|
||||
/**
|
||||
* The form title of the form to be imported from a template or created.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $form_title
|
||||
*/
|
||||
protected $form_title;
|
||||
|
||||
/**
|
||||
* The form title of the form to be imported from a template or created.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $form_description;
|
||||
*/
|
||||
protected $form_description = '';
|
||||
|
||||
/**
|
||||
* The templates' data store to retrieve the templates' data from.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var GF_Templates_Store $templates_repos
|
||||
*/
|
||||
protected $templates_store;
|
||||
|
||||
/**
|
||||
* Endpoint constructor.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param GF_Templates_Store $templates_store The templates' data store to retrieve the templates' data from.
|
||||
*/
|
||||
public function __construct( GF_Templates_Store $templates_store ) {
|
||||
$this->templates_store = $templates_store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle creating a form from a template or a blank form.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle() {
|
||||
|
||||
$this->template_id = sanitize_text_field( rgpost( 'templateId' ) );
|
||||
$this->form_title = sanitize_text_field( rgpost( 'form_title' ) );
|
||||
$this->form_description = sanitize_text_field( rgpost( 'form_description' ) );
|
||||
|
||||
if ( ! $this->template_id || ! $this->form_title ) {
|
||||
wp_send_json_error( array( 'message' => 'Missing required parameter' ), 400 );
|
||||
}
|
||||
|
||||
if ( $this->template_id === 'blank' ) {
|
||||
$template = new GF_Template_Library_Template(
|
||||
array(
|
||||
'id' => 'blank',
|
||||
'title' => '',
|
||||
'description' => '',
|
||||
'form_meta' => array(
|
||||
'fields' => array(),
|
||||
),
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$template = $this->templates_store->get( $this->template_id );
|
||||
}
|
||||
|
||||
if ( ! is_a( $template, GF_Template_Library_Template::class ) ) {
|
||||
wp_send_json_error( array( 'message' => 'Invalid template ID' ), 400 );
|
||||
}
|
||||
|
||||
$form_meta = $template->get_form_meta();
|
||||
$form_meta['title'] = $this->form_title;
|
||||
$form_meta['description'] = $this->form_description;
|
||||
|
||||
if ( $this->template_id !== 'blank' ) {
|
||||
$form_meta['template_id'] = $this->template_id;
|
||||
}
|
||||
|
||||
$form_crud_handler = GFForms::get_service_container()->get( GF_Save_Form_Service_Provider::GF_FORM_CRUD_HANDLER );
|
||||
$result = $form_crud_handler->save( 0, wp_json_encode( $form_meta ) );
|
||||
|
||||
$status = rgar( $result, 'status' );
|
||||
$form_id = rgars( $result, 'meta/id', false );
|
||||
if ( is_numeric( $form_id ) && $form_id !== 0 ) {
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'form_id' => abs( $form_id ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json_error(
|
||||
array(
|
||||
'message' => $status,
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Templates;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Store;
|
||||
use Unirest\Exception;
|
||||
|
||||
/**
|
||||
* Class GF_Template_Library_Array_Store
|
||||
*
|
||||
* Loads the templates' library data from an array.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Template_Library;
|
||||
*/
|
||||
class GF_Template_Library_Array_Store implements GF_Templates_Store {
|
||||
/**
|
||||
* The templates array.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $templates The templates array.
|
||||
*/
|
||||
protected $templates;
|
||||
|
||||
/**
|
||||
* The store configurations array.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $config The store configurations array.
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Store constructor.
|
||||
*
|
||||
* @param array $config The store configurations array.
|
||||
*/
|
||||
public function __construct( $config ) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves raw data and decodes it, returns an array of templates.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_templates() {
|
||||
if ( is_array( $this->templates ) ) {
|
||||
return $this->templates;
|
||||
}
|
||||
|
||||
$uri = rgar( $this->config, 'uri' );
|
||||
|
||||
$this->templates = include_once $uri;
|
||||
|
||||
return $this->templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template by its ID.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string $id The id of the template.
|
||||
*
|
||||
* @return GF_Template_Library_Template|false
|
||||
*/
|
||||
public function get( $id ) {
|
||||
$template_data = rgar( $this->get_templates(), $id );
|
||||
if ( ! $template_data ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return new GF_Template_Library_Template( $template_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the templates, optionally including the form meta.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param bool $include_meta whether to include the template form meta or not.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all( $include_meta = false ) {
|
||||
if ( $include_meta ) {
|
||||
return $this->get_templates();
|
||||
}
|
||||
|
||||
$templates_data = array_map(
|
||||
function( $template_data ) {
|
||||
unset( $template_data['form_meta'] );
|
||||
unset( $template_data['version'] );
|
||||
return $template_data;
|
||||
},
|
||||
$this->get_templates()
|
||||
);
|
||||
|
||||
return $templates_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Templates;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Templates_Store;
|
||||
use Unirest\Exception;
|
||||
|
||||
/**
|
||||
* Class GF_Template_Library_File_Store
|
||||
*
|
||||
* Loads the templates' library data from a JSON file.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Template_Library;
|
||||
*/
|
||||
class GF_Template_Library_File_Store implements GF_Templates_Store {
|
||||
/**
|
||||
* The templates.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var mixed $data The templates.
|
||||
*/
|
||||
protected $templates;
|
||||
|
||||
/**
|
||||
* The raw data returned from the source.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $raw_data The raw data returned from the data source.
|
||||
*/
|
||||
protected $raw_data;
|
||||
|
||||
/**
|
||||
* The store configurations array.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $config The store configurations array.
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Store constructor.
|
||||
*
|
||||
* @param array $config The store configurations array.
|
||||
*/
|
||||
public function __construct( $config ) {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves raw data and decodes it, returns an array of templates.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_templates() {
|
||||
|
||||
if ( is_array( $this->templates ) ) {
|
||||
return $this->templates;
|
||||
}
|
||||
|
||||
$uri = rgar( $this->config, 'uri' );
|
||||
try {
|
||||
$this->raw_data = @file_get_contents( $uri );
|
||||
} catch ( Exception $e ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$this->templates = json_decode( $this->raw_data, true );
|
||||
if ( ! is_array( $this->templates ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return $this->templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a template by its ID.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string $id The id of the template.
|
||||
*
|
||||
* @return GF_Template_Library_Template|false
|
||||
*/
|
||||
public function get( $id ) {
|
||||
$template_data = rgar( $this->get_templates(), $id );
|
||||
if ( ! $template_data ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return new GF_Template_Library_Template( $template_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the all the templates as an array.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param bool $include_meta whether to include the template form meta or not.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all( $include_meta = false ) {
|
||||
if ( $include_meta ) {
|
||||
return $this->get_templates();
|
||||
}
|
||||
|
||||
$templates_data = array_map(
|
||||
function( $template_data ) {
|
||||
unset( $template_data['form_meta'] );
|
||||
unset( $template_data['version'] );
|
||||
return $template_data;
|
||||
},
|
||||
$this->get_templates()
|
||||
);
|
||||
|
||||
return $templates_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Templates;
|
||||
|
||||
/**
|
||||
* Class GF_Template_Library_Template
|
||||
*
|
||||
* Represents Template library.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Template_Library;
|
||||
*/
|
||||
class GF_Template_Library_Template {
|
||||
/**
|
||||
* The template data.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $data The template raw data.
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* The template ID.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $id The id of the template.
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* The template name.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $name The name of the template.
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* The template description.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var string $description The description of the template.
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* The template form meta data.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @var array $form_meta The form meta data.
|
||||
*/
|
||||
protected $form_meta;
|
||||
|
||||
/**
|
||||
* Template constructor.
|
||||
*
|
||||
* @sine 2.7
|
||||
*
|
||||
* @param array $data The template data.
|
||||
*/
|
||||
public function __construct( $data ) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the template title.
|
||||
*
|
||||
* @sine 2.7
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
if ( ! isset( $this->title ) ) {
|
||||
$this->title = rgar( $this->data, 'title' );
|
||||
}
|
||||
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the template description.
|
||||
*
|
||||
* @sine 2.7
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
if ( ! isset( $this->description ) ) {
|
||||
$this->description = rgar( $this->data, 'description' );
|
||||
}
|
||||
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the template form meta.
|
||||
*
|
||||
* @sine 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_form_meta() {
|
||||
if ( ! isset( $this->form_meta ) ) {
|
||||
$this->form_meta = $this->cleanup_form_meta();
|
||||
}
|
||||
|
||||
return $this->form_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the template ID.
|
||||
*
|
||||
* @sine 2.7
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
if ( ! isset( $this->id ) ) {
|
||||
$this->id = rgar( $this->data, 'id' );
|
||||
}
|
||||
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up the form meta JSON.
|
||||
*
|
||||
* Some form exports will have the form id as one of the keys of the form fields, also some escaped characters cause some issues.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function cleanup_form_meta() {
|
||||
$form_meta = rgar( $this->data, 'form_meta' );
|
||||
if ( isset( $form_meta['id'] ) ) {
|
||||
unset( $form_meta['id'] );
|
||||
}
|
||||
// Unset form IDs left from exporting a form.
|
||||
$fields = rgar( $form_meta, 'fields' );
|
||||
if ( is_array( $fields ) && count( $fields ) > 0 ) {
|
||||
foreach ( $fields as &$field ) {
|
||||
if ( isset( $field['formId'] ) ) {
|
||||
unset( $field['formId'] );
|
||||
}
|
||||
}
|
||||
$form_meta['fields'] = $fields;
|
||||
}
|
||||
|
||||
// Some forms don't have this set, which causes some notices.
|
||||
if ( ! isset( $form_meta['button'] ) ) {
|
||||
$form_meta['button'] = array(
|
||||
'type' => 'text',
|
||||
'text' => '',
|
||||
'imageUrl' => '',
|
||||
);
|
||||
}
|
||||
|
||||
// escaping double quotes this way messes things up.
|
||||
$meta_json = str_replace( '\"', "'", wp_json_encode( $form_meta ) );
|
||||
|
||||
return json_decode( $meta_json, true );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Template_Library\Templates;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Template_Library\Templates\GF_Template_Library_Template;
|
||||
|
||||
interface GF_Templates_Store {
|
||||
/**
|
||||
* Retrieves raw data and store it in memory, returns it if it already exists.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_templates();
|
||||
|
||||
/**
|
||||
* Return a template by its ID.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string $id The id of the template.
|
||||
*
|
||||
* @return GF_Template_Library_Template
|
||||
*/
|
||||
public function get( $id );
|
||||
|
||||
/**
|
||||
* Returns all the templates as an array.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param bool $include_meta whether to include the template form meta or not.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function all( $include_meta );
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user