plugin install
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Factories\Definition_Engine_Factory;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Factories\Output_Engine_Factory;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\GF_Theme_Layer;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Traits\Enqueues_Assets;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Traits\Has_Block_Settings;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Traits\Has_Settings_Fields;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Traits\Modifies_Markup;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Traits\Outputs_Form_CSS_Properties;
|
||||
|
||||
/**
|
||||
* Implementation of GF_Theme_Layer which uses all available traits.
|
||||
*
|
||||
* @since 2.7
|
||||
*/
|
||||
abstract class GF_All_Access_Theme_Layer extends GF_Theme_Layer {
|
||||
|
||||
use Has_Settings_Fields;
|
||||
use Has_Block_Settings;
|
||||
use Modifies_Markup;
|
||||
use Outputs_Form_CSS_Properties;
|
||||
use Enqueues_Assets;
|
||||
|
||||
protected $_settings_fields = array();
|
||||
protected $_block_settings = array();
|
||||
protected $_overidden_fields = array();
|
||||
protected $_form_css_properties = array();
|
||||
protected $_scripts = array();
|
||||
protected $_styles = array();
|
||||
|
||||
public function __construct( Definition_Engine_Factory $definition_engine_factory, Output_Engine_Factory $output_engine_factory ) {
|
||||
$this->definition_engine_factory = $definition_engine_factory;
|
||||
$this->output_engine_factory = $output_engine_factory;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API\Fluent;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\API\Fluent\Layers\Fluent_Theme_Layer;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\GF_Theme_Layers_Provider;
|
||||
|
||||
/**
|
||||
* Wrapper around the Fluent_Theme_Layer that provides fluent access (each call returns the class so
|
||||
* future calls can be chained).
|
||||
*
|
||||
* @since 2.7
|
||||
*/
|
||||
class Theme_Layer_Builder {
|
||||
|
||||
private $layer;
|
||||
|
||||
/**
|
||||
* Gathers the various dependencies
|
||||
*
|
||||
* NOTE: we don't use DI here because this class is instantiated in various places, and it would make
|
||||
* the process onerous for third-party usage.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$def_factory = \GFForms::get_service_container()->get( GF_Theme_Layers_Provider::DEFINITION_ENGINE_FACTORY );
|
||||
$output_factory = \GFForms::get_service_container()->get( GF_Theme_Layers_Provider::OUTPUT_ENGINE_FACTORY );
|
||||
$this->layer = new Fluent_Theme_Layer( $def_factory, $output_factory );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the layer's engines and add the layer to the list of registered theme layers.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
$layer = $this->layer;
|
||||
$layer->init_engines();
|
||||
add_filter( 'gform_registered_theme_layers', function ( $layers ) use ( $layer ) {
|
||||
$layers[] = $layer;
|
||||
|
||||
return $layers;
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for name.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_name( $name ) {
|
||||
$this->layer->set_name( $name );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for title.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $title
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_short_title( $title ) {
|
||||
$this->layer->set_short_title( $title );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for priority.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $priority
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_priority( $priority ) {
|
||||
$this->layer->set_priority( $priority );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set_icon( $icon ) {
|
||||
$this->layer->set_icon( $icon );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for fields.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $fields
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_settings_fields( $fields ) {
|
||||
$this->layer->set_settings_fields( $fields );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for overidden fields.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $fields
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_overidden_fields( $fields ) {
|
||||
$this->layer->set_overidden_fields( $fields );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for css properties.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $properties
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_form_css_properties( $properties ) {
|
||||
$this->layer->set_form_css_properties( $properties );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for scripts.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $scripts
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_scripts( $scripts ) {
|
||||
$this->layer->set_scripts( $scripts );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for styles.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $styles
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_styles( $styles ) {
|
||||
$this->layer->set_styles( $styles );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for block settings.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $settings
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function set_block_settings( $settings ) {
|
||||
$this->layer->set_block_settings( $settings );
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API\Fluent\Layers;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\API\GF_All_Access_Theme_Layer;
|
||||
|
||||
/**
|
||||
* Theme Layer set up to be used in a fluent context. This layer extends GF_All_Access_Theme_Layer,
|
||||
* which allows it to have access to all available traits and engines.
|
||||
*
|
||||
* This layer mostly acts as a middleware to pass values from the fluent builder to the layer.
|
||||
*
|
||||
* @since 2.7
|
||||
*/
|
||||
class Fluent_Theme_Layer extends GF_All_Access_Theme_Layer {
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// Getters ////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public function settings_fields() {
|
||||
return $this->_settings_fields;
|
||||
}
|
||||
|
||||
public function block_settings() {
|
||||
return $this->_block_settings;
|
||||
}
|
||||
|
||||
public function overriden_fields() {
|
||||
return $this->_overidden_fields;
|
||||
}
|
||||
|
||||
public function form_css_properties( $form_id = 0, $settings = array(), $block_settings = array(), $form = array() ) {
|
||||
if ( is_callable( $this->_form_css_properties ) ) {
|
||||
return call_user_func_array( $this->_form_css_properties, array( $form_id, $settings, $block_settings, $form ) );
|
||||
}
|
||||
|
||||
return $this->_form_css_properties;
|
||||
}
|
||||
|
||||
public function scripts( $form, $ajax, $settings, $block_settings = array() ) {
|
||||
return is_callable( $this->_scripts ) ? call_user_func_array( $this->_scripts, array(
|
||||
$form,
|
||||
$ajax,
|
||||
$settings,
|
||||
$block_settings,
|
||||
) ) : array();
|
||||
}
|
||||
|
||||
public function styles( $form, $ajax, $settings, $block_settings = array() ) {
|
||||
return is_callable( $this->_styles ) ? call_user_func_array( $this->_styles, array(
|
||||
$form,
|
||||
$ajax,
|
||||
$settings,
|
||||
$block_settings,
|
||||
) ) : array();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// Setters ////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public function set_settings_fields( $fields ) {
|
||||
$this->_settings_fields = $fields;
|
||||
}
|
||||
|
||||
public function set_block_settings( $settings ) {
|
||||
$this->_block_settings = $settings;
|
||||
}
|
||||
|
||||
public function set_overidden_fields( $fields ) {
|
||||
$this->_overidden_fields = $fields;
|
||||
}
|
||||
|
||||
public function set_form_css_properties( $properties ) {
|
||||
$this->_form_css_properties = $properties;
|
||||
}
|
||||
|
||||
public function set_scripts( $scripts ) {
|
||||
$this->_scripts = $scripts;
|
||||
}
|
||||
|
||||
public function set_styles( $styles ) {
|
||||
$this->_styles = $styles;
|
||||
}
|
||||
|
||||
public function set_name( $name ) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function set_priority( $priority ) {
|
||||
$this->priority = $priority;
|
||||
}
|
||||
|
||||
public function set_short_title( $title ) {
|
||||
$this->short_title = $title;
|
||||
}
|
||||
|
||||
public function set_icon( $icon ) {
|
||||
$this->icon = $icon;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\API\JSON\Layers\Json_Theme_Layer;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\GF_Theme_Layers_Provider;
|
||||
|
||||
function gforms_register_theme_json( $path ) {
|
||||
$container = GFForms::get_service_container();
|
||||
$layer = new Json_Theme_Layer( $container->get( GF_Theme_Layers_Provider::DEFINITION_ENGINE_FACTORY ), $container->get( GF_Theme_Layers_Provider::OUTPUT_ENGINE_FACTORY ) );
|
||||
$layer->set_json( $path );
|
||||
$layer->init_engines();
|
||||
|
||||
add_filter( 'gform_registered_theme_layers', function ( $layers ) use ( $layer ) {
|
||||
$layers[] = $layer;
|
||||
|
||||
return $layers;
|
||||
} );
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API\JSON\Layers;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\API\GF_All_Access_Theme_Layer;
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\API\JSON\Rules\GF_Theme_Layer_Rule;
|
||||
|
||||
class Json_Theme_Layer extends GF_All_Access_Theme_Layer {
|
||||
|
||||
protected $_file;
|
||||
protected $_json_data;
|
||||
protected $_rules;
|
||||
|
||||
private function process_json() {
|
||||
$contents = file_get_contents( $this->_file );
|
||||
$data = json_decode( $contents, true );
|
||||
|
||||
if ( empty( $data['gravityforms'] ) ) {
|
||||
throw new \InvalidArgumentException( 'Invalid theme.json file provided.' );
|
||||
}
|
||||
|
||||
$this->_json_data = $data['gravityforms'];
|
||||
|
||||
if ( empty( $this->_json_data['name'] ) || empty( $this->_json_data['short_title'] ) ) {
|
||||
throw new \InvalidArgumentException( 'theme.json file must have a name and short_title value.' );
|
||||
}
|
||||
|
||||
$this->set_name( $this->_json_data['name'] );
|
||||
$this->set_short_title( $this->_json_data['short_title'] );
|
||||
|
||||
if ( isset( $this->_json_data['settings']['fields']['form'] ) ) {
|
||||
$this->set_settings_fields( $this->_json_data['settings']['fields']['form'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['fields']['blocks'] ) ) {
|
||||
$this->set_block_settings( $this->_json_data['settings']['fields']['blocks'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['cssProperties'] ) ) {
|
||||
$this->set_form_css_properties( $this->_json_data['settings']['cssProperties'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['templateParts'] ) ) {
|
||||
$this->set_overidden_fields( $this->_json_data['settings']['templateParts'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['assets']['scripts'] ) ) {
|
||||
$this->set_scripts( $this->_json_data['settings']['assets']['scripts'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['assets']['styles'] ) ) {
|
||||
$this->set_styles( $this->_json_data['settings']['assets']['styles'] );
|
||||
}
|
||||
|
||||
if ( isset( $this->_json_data['settings']['rules'] ) ) {
|
||||
$this->set_rules( $this->_json_data['settings']['rules'] );
|
||||
}
|
||||
}
|
||||
|
||||
private function evaluate_rule( $rule, $settings, $block_settings ) {
|
||||
if ( is_string( $rule ) && ! isset( $this->_rules[ $rule ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( is_string( $rule ) ) {
|
||||
$rule = $this->_rules[ $rule ];
|
||||
}
|
||||
|
||||
$rule_object = new GF_Theme_Layer_Rule( $rule );
|
||||
|
||||
return $rule_object->validate( array( 'form' => $settings, 'blocks' => $block_settings ) );
|
||||
}
|
||||
|
||||
private function filter_values_by_rule( $values, $settings, $block_settings ) {
|
||||
$self = $this;
|
||||
|
||||
return array_filter( $values, function ( $item ) use ( $self, $settings, $block_settings ) {
|
||||
if ( ! isset( $item['rules'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$rule = $item['rules'];
|
||||
|
||||
return $self->evaluate_rule( $rule, $settings, $block_settings );
|
||||
} );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// Setters ////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public function set_json( $file ) {
|
||||
$this->_file = $file;
|
||||
$this->process_json();
|
||||
}
|
||||
|
||||
public function set_rules( $rules ) {
|
||||
$this->_rules = $rules;
|
||||
}
|
||||
|
||||
public function set_settings_fields( $fields ) {
|
||||
$this->_settings_fields = $fields;
|
||||
}
|
||||
|
||||
public function set_block_settings( $settings ) {
|
||||
$this->_block_settings = $settings;
|
||||
}
|
||||
|
||||
public function set_overidden_fields( $fields ) {
|
||||
$this->_overidden_fields = $fields;
|
||||
}
|
||||
|
||||
public function set_form_css_properties( $properties ) {
|
||||
$this->_form_css_properties = $properties;
|
||||
}
|
||||
|
||||
public function set_scripts( $scripts ) {
|
||||
$this->_scripts = $scripts;
|
||||
}
|
||||
|
||||
public function set_styles( $styles ) {
|
||||
foreach( $styles as &$style ) {
|
||||
$parsed = str_replace( '%gforms_plugin_url%', \GFCommon::get_base_url(), $style['path'] );
|
||||
$style['path'] = $parsed;
|
||||
}
|
||||
|
||||
$this->_styles = $styles;
|
||||
}
|
||||
|
||||
public function set_name( $name ) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function set_priority( $priority ) {
|
||||
$this->priority = $priority;
|
||||
}
|
||||
|
||||
public function set_short_title( $title ) {
|
||||
$this->short_title = $title;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// Getters ////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
public function settings_fields() {
|
||||
return array(
|
||||
array(
|
||||
'description' => $this->short_title(),
|
||||
'fields' => $this->_settings_fields,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function block_settings() {
|
||||
return $this->_block_settings;
|
||||
}
|
||||
|
||||
public function overriden_fields() {
|
||||
return $this->_overidden_fields;
|
||||
}
|
||||
|
||||
public function form_css_properties( $form_id = 0, $settings = array(), $block_settings = array() ) {
|
||||
return array();
|
||||
|
||||
return $this->filter_values_by_rule( $this->_form_css_properties, $settings, $block_settings );
|
||||
}
|
||||
|
||||
public function scripts( $form, $ajax, $settings, $block_settings = array() ) {
|
||||
return $this->filter_values_by_rule( $this->_scripts, $settings, $block_settings );
|
||||
}
|
||||
|
||||
public function styles( $form, $ajax, $settings, $block_settings = array() ) {
|
||||
return $this->filter_values_by_rule( $this->_styles, $settings, $block_settings );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API\JSON\Rules;
|
||||
|
||||
class GF_Theme_Layer_Rule {
|
||||
|
||||
private $setting;
|
||||
private $operator;
|
||||
private $value;
|
||||
|
||||
public function __construct( $args ) {
|
||||
if ( ! isset( $args['setting'] ) || ! isset( $args['operator'] ) || ! isset( $args['value'] ) ) {
|
||||
throw new \InvalidArgumentException( 'Rules must have settings, operators, and values.' );
|
||||
}
|
||||
|
||||
$setting_parts = explode( '.', $args['setting'] );
|
||||
$this->setting = array( 'type' => $setting_parts[0], 'name' => $setting_parts[1] );
|
||||
$this->operator = $args['operator'];
|
||||
$this->value = $args['value'];
|
||||
}
|
||||
|
||||
public function validate( $settings ) {
|
||||
$type = $this->setting['type'];
|
||||
$name = $this->setting['name'];
|
||||
|
||||
if ( ! isset( $settings[ $type ][ $name ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = $settings[ $type ][ $name ];
|
||||
|
||||
switch ( $this->operator ) {
|
||||
case '=':
|
||||
return $value == $this->value;
|
||||
case '>':
|
||||
return $value > $this->value;
|
||||
case '>=':
|
||||
return $value >= $this->value;
|
||||
case '<':
|
||||
return $value < $this->value;
|
||||
case '<=':
|
||||
return $value <= $this->value;
|
||||
case '!=':
|
||||
return $value != $this->value;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Theme_Layers\API;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Theme_Layers\Framework\Engines\Output_Engines\PHP_Markup_Output_Engine;
|
||||
|
||||
/**
|
||||
* Class used to handle overriding the content of a field or form.
|
||||
*
|
||||
* @since 2.7
|
||||
*/
|
||||
abstract class View {
|
||||
|
||||
protected $engine;
|
||||
|
||||
/**
|
||||
* The Output_Engine for PHP Markup.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param PHP_Markup_Output_Engine $engine
|
||||
*/
|
||||
public function __construct( $engine ) {
|
||||
$this->engine = $engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the markup for an item.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $content
|
||||
* @param $object
|
||||
* @param $value
|
||||
* @param $lead_id
|
||||
* @param $form_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_markup( $content, $object, $value, $lead_id, $form_id );
|
||||
|
||||
/**
|
||||
* Whether this markup override should be in effect.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $object
|
||||
* @param $form_id
|
||||
* @param $block_settings
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_override( $object, $form_id, $block_settings = array() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a setting from the engine.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param $key
|
||||
* @param $form_id
|
||||
* @param null $default
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
protected function get_setting( $key, $form_id, $default = null ) {
|
||||
return $this->engine->get_setting( $key, $form_id, $default );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user