plugin install
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Blocks;
|
||||
|
||||
/**
|
||||
* Gravity Forms Block Attributes class.
|
||||
*
|
||||
* @since 2.7.4
|
||||
*
|
||||
* Class GF_Block_Attributes
|
||||
*/
|
||||
class GF_Block_Attributes {
|
||||
|
||||
public function store( $attributes ) {
|
||||
add_filter( 'gform_form_block_attribute_values', function( $attr ) use ( $attributes ) {
|
||||
$form_id = rgar( $attributes, 'formId', 0 );
|
||||
|
||||
if ( ! array_key_exists( $form_id, $attr ) ) {
|
||||
$attr[ $form_id ] = array();
|
||||
}
|
||||
|
||||
$attr[ $form_id ][] = $attributes;
|
||||
return $attr;
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
// If Gravity Forms Block Manager is not available, do not run.
|
||||
if ( ! class_exists( 'GF_Blocks' ) || ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class GF_Block_Form extends GF_Block {
|
||||
|
||||
/**
|
||||
* Contains an instance of this block, if available.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var GF_Block $_instance If available, contains an instance of this block.
|
||||
*/
|
||||
private static $_instance = null;
|
||||
|
||||
/**
|
||||
* Block type.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'gravityforms/form';
|
||||
|
||||
/**
|
||||
* Handle of primary block script.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var string
|
||||
*/
|
||||
public $script_handle = 'gform_editor_block_form';
|
||||
|
||||
/**
|
||||
* Handle of primary block style.
|
||||
*
|
||||
* @since 2.5.6
|
||||
* @var string
|
||||
*/
|
||||
public $style_handle = 'gform_editor_block_form';
|
||||
|
||||
public function __construct() {
|
||||
$this->assign_attributes();
|
||||
}
|
||||
|
||||
private function assign_attributes() {
|
||||
$default_attributes = GFForms::get_service_container()->get( \Gravity_Forms\Gravity_Forms\Blocks\GF_Blocks_Service_Provider::FORM_BLOCK_ATTRIBUTES );
|
||||
$attributes = apply_filters( 'gform_form_block_attributes', $default_attributes );
|
||||
|
||||
array_walk( $attributes, function ( &$value ) {
|
||||
$value = array( 'type' => $value['type'] );
|
||||
} );
|
||||
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance of this class.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return GF_Block_Form
|
||||
*/
|
||||
public static function get_instance() {
|
||||
|
||||
if ( null === self::$_instance ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// # SCRIPT / STYLES -----------------------------------------------------------------------------------------------
|
||||
public function register_block_assets() {
|
||||
parent::register_block_assets();
|
||||
if ( function_exists( 'wp_enqueue_block_style' ) && is_admin() ) {
|
||||
wp_enqueue_block_style( $this->type, array( 'handle' => 'gravity_forms_theme_reset' ) );
|
||||
wp_enqueue_block_style( $this->type, array( 'handle' => 'gravity_forms_theme_foundation' ) );
|
||||
wp_enqueue_block_style( $this->type, array( 'handle' => 'gravity_forms_theme_framework' ) );
|
||||
wp_enqueue_block_style( $this->type, array( 'handle' => 'gravity_forms_orbital_theme' ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register scripts for block.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scripts() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Localize Form block script.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @param array $script Script arguments.
|
||||
*/
|
||||
public function localize_script( $script = array() ) {
|
||||
|
||||
wp_localize_script(
|
||||
$script['handle'],
|
||||
'gform_block_form',
|
||||
array(
|
||||
'adminURL' => admin_url( 'admin.php' ),
|
||||
'forms' => $this->get_forms(),
|
||||
'preview' => GFCommon::get_base_url() . '/images/gf_block_preview.svg',
|
||||
)
|
||||
);
|
||||
|
||||
if ( function_exists( 'wp_set_script_translations' ) ) {
|
||||
wp_set_script_translations( $script['handle'], 'gravityforms', GFCommon::get_base_path() . '/languages' );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register styles for block.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function styles() {
|
||||
|
||||
// Prepare styling dependencies.
|
||||
$deps = array( 'wp-edit-blocks' );
|
||||
|
||||
/**
|
||||
* Allows users to disable all CSS files from being loaded on the Front End.
|
||||
*
|
||||
* @since 2.8
|
||||
*
|
||||
* @param boolean Whether to disable css.
|
||||
*/
|
||||
$disable_css = apply_filters( 'gform_disable_css', get_option( 'rg_gforms_disable_css' ) );
|
||||
|
||||
// Add Gravity Forms styling if CSS is enabled.
|
||||
if ( ! $disable_css ) {
|
||||
$deps = array_merge( $deps, array( 'gform_basic', 'gforms_formsmain_css', 'gforms_ready_class_css', 'gforms_browsers_css', 'gform_theme' ) );
|
||||
|
||||
/**
|
||||
* Allows users to disable the main theme.css file from being loaded on the Front End.
|
||||
*
|
||||
* @since 2.5-beta-3
|
||||
*
|
||||
* @param boolean Whether to disable the theme css.
|
||||
*/
|
||||
$disable_theme_css = apply_filters( 'gform_disable_form_theme_css', false );
|
||||
|
||||
if ( ! $disable_theme_css ) {
|
||||
$deps[] = 'gform_theme';
|
||||
}
|
||||
}
|
||||
|
||||
$dev_min = defined( 'GF_SCRIPT_DEBUG' ) && GF_SCRIPT_DEBUG ? '' : '.min';
|
||||
|
||||
return array(
|
||||
array(
|
||||
'handle' => $this->style_handle,
|
||||
'src' => GFCommon::get_base_url() . "/assets/css/dist/blocks{$dev_min}.css",
|
||||
'deps' => $deps,
|
||||
'version' => defined( 'GF_SCRIPT_DEBUG' ) && GF_SCRIPT_DEBUG ? filemtime( GFCommon::get_base_path() . "/assets/css/dist/blocks{$dev_min}.css" ) : GFForms::$version,
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// # BLOCK RENDER -------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Display block contents on frontend.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @param array $attributes Block attributes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render_block( $attributes = array() ) {
|
||||
GFForms::get_service_container()->get( 'block_attributes' )->store( $attributes );
|
||||
|
||||
// Prepare variables.
|
||||
$form_id = rgar( $attributes, 'formId' ) ? $attributes['formId'] : false;
|
||||
$title = isset( $attributes['title'] ) ? $attributes['title'] : true;
|
||||
$description = isset( $attributes['description'] ) ? $attributes['description'] : true;
|
||||
$ajax = isset( $attributes['ajax'] ) ? $attributes['ajax'] : false;
|
||||
$tabindex = isset( $attributes['tabindex'] ) ? intval( $attributes['tabindex'] ) : 0;
|
||||
$field_values = isset( $attributes['fieldValues'] ) ? $attributes['fieldValues'] : '';
|
||||
|
||||
// If form ID was not provided or form does not exist, return.
|
||||
if ( ! $form_id || ( $form_id && ! GFAPI::get_form( $form_id ) ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Use Gravity Forms function for REST API requests.
|
||||
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
||||
|
||||
// Start output buffering.
|
||||
ob_start();
|
||||
|
||||
// Prepare field values.
|
||||
if ( ! empty( $field_values ) ) {
|
||||
$field_values = str_replace( '&', '&', $field_values );
|
||||
parse_str( $field_values, $field_value_array );
|
||||
$field_values = stripslashes_deep( $field_value_array );
|
||||
}
|
||||
|
||||
// Get form output string.
|
||||
$form_string = gravity_form( $form_id, $title, $description, false, $field_values, $ajax, $tabindex, false );
|
||||
|
||||
// Get output buffer contents.
|
||||
$buffer_contents = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
// Return buffer contents with form string.
|
||||
return $buffer_contents . $form_string;
|
||||
|
||||
}
|
||||
|
||||
// Encode field values.
|
||||
$field_values = htmlspecialchars_decode( $field_values );
|
||||
$field_values = str_replace( array( '&', '[', ']' ), array( '&', '[', ']' ), $field_values );
|
||||
parse_str( $field_values, $field_value_array ); //parsing query string like string for field values and placing them into an associative array
|
||||
$field_values = stripslashes_deep( $field_value_array );
|
||||
|
||||
// If no field values are set, set field values to an empty string
|
||||
if ( empty( $field_values ) ) {
|
||||
$field_values = '';
|
||||
}
|
||||
|
||||
return gravity_form( $form_id, $title, $description, false, $field_values, $ajax, $tabindex, false );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Register block.
|
||||
if ( true !== ( $registered = GF_Blocks::register( GF_Block_Form::get_instance() ) ) && is_wp_error( $registered ) ) {
|
||||
|
||||
// Log that block could not be registered.
|
||||
GFCommon::log_error( 'Unable to register block; ' . $registered->get_error_message() );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
<?php
|
||||
|
||||
// If Gravity Forms Block Manager is not available, do not run.
|
||||
if ( ! class_exists( 'GF_Blocks' ) || ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base Gravity Forms Block class.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* Class GF_Block
|
||||
*/
|
||||
class GF_Block {
|
||||
|
||||
/**
|
||||
* Contains an instance of this block, if available.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var GF_Block $_instance If available, contains an instance of this block.
|
||||
*/
|
||||
private static $_instance = null;
|
||||
|
||||
/**
|
||||
* Block type.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var string
|
||||
*/
|
||||
public $type = '';
|
||||
|
||||
/**
|
||||
* Handle of primary block editor script.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var string
|
||||
*/
|
||||
public $script_handle = '';
|
||||
|
||||
/**
|
||||
* Handle of primary block editor style.
|
||||
*
|
||||
* @since 2.5.6
|
||||
* @var string
|
||||
*/
|
||||
public $style_handle = '';
|
||||
|
||||
/**
|
||||
* Handle of primary block FE script.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var string
|
||||
*/
|
||||
public $fe_script_handle = '';
|
||||
|
||||
/**
|
||||
* Handle of primary block FE style.
|
||||
*
|
||||
* @since 2.5.6
|
||||
* @var string
|
||||
*/
|
||||
public $fe_style_handle = '';
|
||||
|
||||
/**
|
||||
* Block attributes.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var array
|
||||
*/
|
||||
public $attributes = array();
|
||||
|
||||
/**
|
||||
* Register block type.
|
||||
* Enqueue editor assets.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @uses GF_Block::register_block_type()
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
$this->register_block_type();
|
||||
|
||||
$this->register_block_assets();
|
||||
|
||||
add_action( 'gform_post_enqueue_scripts', array( $this, 'post_enqueue_scripts' ), 10, 3 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// # BLOCK REGISTRATION --------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get block type.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_type() {
|
||||
|
||||
return $this->type;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Register block with WordPress.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*/
|
||||
public function register_block_type() {
|
||||
register_block_type( $this->get_type(), $this->get_block_properties() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array representing the properties for this block. Can be overriden by inheriting
|
||||
* classes in order to provide more/fewer/different properties.
|
||||
*
|
||||
* @since 2.5.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_block_properties() {
|
||||
return array(
|
||||
'render_callback' => array( $this, 'render_block' ),
|
||||
'editor_script' => $this->script_handle,
|
||||
'editor_style' => $this->style_handle,
|
||||
'attributes' => $this->attributes,
|
||||
'script' => $this->fe_script_handle,
|
||||
'style' => $this->fe_style_handle,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue/register the block's assets upon init
|
||||
*
|
||||
* @since 2.5.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_block_assets() {
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'register_scripts' ) );
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'register_styles' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks allowed blocks for Gravity forms blocks to only enqueue block editor assets when necessary.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*
|
||||
* @deprecated since 2.5.6. See GF_Block::register_block_assets()
|
||||
*
|
||||
* @param bool|array $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
public function check_allowed_blocks( $allowed_block_types ) {
|
||||
|
||||
// Only enqueue block editor assets if all blocks are allowed or if the current block type is an allowed block.
|
||||
if ( $allowed_block_types === true || ( is_array( $allowed_block_types ) && in_array( $this->get_type(), $allowed_block_types ) ) ) {
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'register_scripts' ) );
|
||||
add_action( 'enqueue_block_editor_assets', array( $this, 'register_styles' ) );
|
||||
}
|
||||
|
||||
return $allowed_block_types;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// # SCRIPT ENQUEUEING ---------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Enqueue block scripts.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @uses GF_Block::scripts()
|
||||
*/
|
||||
public function register_scripts() {
|
||||
|
||||
// Get registered scripts.
|
||||
$scripts = $this->scripts();
|
||||
|
||||
// If no scripts are registered, return.
|
||||
if ( empty( $scripts ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Loop through scripts.
|
||||
foreach ( $scripts as $script ) {
|
||||
|
||||
// Prepare parameters.
|
||||
$src = isset( $script['src'] ) ? $script['src'] : false;
|
||||
$deps = isset( $script['deps'] ) ? $script['deps'] : array();
|
||||
$version = isset( $script['version'] ) ? $script['version'] : false;
|
||||
$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false;
|
||||
|
||||
// Enqueue script.
|
||||
if ( $this->script_handle === $script['handle'] ) {
|
||||
// Support for the editor_style property, if a style_handle is defined. No need to enqueue.
|
||||
wp_register_script( $script['handle'], $src, $deps, $version, $in_footer );
|
||||
} else {
|
||||
// style_handle isn't defined, or this is an additional style. Enqueue it manually.
|
||||
wp_enqueue_script( $script['handle'], $src, $deps, $version, $in_footer );
|
||||
}
|
||||
|
||||
// Localize script.
|
||||
if ( rgar( $script, 'strings' ) ) {
|
||||
wp_localize_script( $script['handle'], $script['handle'] . '_strings', $script['strings'] );
|
||||
}
|
||||
|
||||
// Run script callback.
|
||||
if ( rgar( $script, 'callback' ) && is_callable( $script['callback'] ) ) {
|
||||
call_user_func( $script['callback'], $script );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts
|
||||
*
|
||||
* @depecated since 2.5.6. Use ::register_scripts() instead.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_scripts() {
|
||||
$this->register_scripts();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this function to provide a list of scripts to be enqueued.
|
||||
* Following is an example of the array that is expected to be returned by this function:
|
||||
* <pre>
|
||||
* <code>
|
||||
*
|
||||
* array(
|
||||
* array(
|
||||
* 'handle' => 'super_signature_script',
|
||||
* 'src' => $this->get_base_url() . '/super_signature/ss.js',
|
||||
* 'version' => $this->_version,
|
||||
* 'deps' => array( 'jquery'),
|
||||
* 'callback' => array( $this, 'localize_scripts' ),
|
||||
* 'strings' => array(
|
||||
* // Accessible in JavaScript using the global variable "[script handle]_strings"
|
||||
* 'stringKey1' => __( 'The string', 'gravityforms' ),
|
||||
* 'stringKey2' => __( 'Another string.', 'gravityforms' )
|
||||
* )
|
||||
* )
|
||||
* );
|
||||
*
|
||||
* </code>
|
||||
* </pre>
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scripts() {
|
||||
|
||||
return array();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// # STYLE ENQUEUEING ----------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Enqueue block styles.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*/
|
||||
public function register_styles() {
|
||||
|
||||
// Get registered styles.
|
||||
$styles = $this->styles();
|
||||
|
||||
// If no styles are registered, return.
|
||||
if ( empty( $styles ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Loop through styles.
|
||||
foreach ( $styles as $style ) {
|
||||
|
||||
// Prepare parameters.
|
||||
$src = isset( $style['src'] ) ? $style['src'] : false;
|
||||
$deps = isset( $style['deps'] ) ? $style['deps'] : array();
|
||||
$version = isset( $style['version'] ) ? $style['version'] : false;
|
||||
$media = isset( $style['media'] ) ? $style['media'] : 'all';
|
||||
|
||||
if ( $this->style_handle === $style['handle'] ) {
|
||||
// Support for the editor_style property, if a style_handle is defined. No need to enqueue.
|
||||
wp_register_style( $style['handle'], $src, $deps, $version, $media );
|
||||
} else {
|
||||
// style_handle isn't defined, or this is an additional style. Enqueue it manually.
|
||||
wp_enqueue_style( $style['handle'], $src, $deps, $version, $media );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue styles
|
||||
*
|
||||
* @depecated since 2.5.6. Use ::register_styles() instead.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_styles() {
|
||||
$this->register_styles();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this function to provide a list of styles to be enqueued.
|
||||
* See scripts() for an example of the format expected to be returned.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function styles() {
|
||||
|
||||
return array();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// # BLOCK RENDER -------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Display block contents on frontend.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @param array $attributes Block attributes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render_block( $attributes = array() ) {
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to perform additional actions when scripts/styles are enqueued on the wp_enqueue_scripts hook.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*
|
||||
* @param array $found_forms An array of found forms using the form ID as the key to the ajax status.
|
||||
* @param array $found_blocks An array of found GF blocks.
|
||||
* @param WP_Post $post The post which was processed.
|
||||
*/
|
||||
public function post_enqueue_scripts( $found_forms, $found_blocks, $post ) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Blocks;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Blocks\Config\GF_Blocks_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Blocks\GF_Block_Attributes;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
use GFFormDisplay;
|
||||
use GFCommon;
|
||||
|
||||
/**
|
||||
* Class GF_Blocks_Service_Provider
|
||||
*
|
||||
* Service provider for the Blocks Service.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Blocks;
|
||||
*/
|
||||
class GF_Blocks_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
// Configs
|
||||
const BLOCKS_CONFIG = 'blocks_config';
|
||||
|
||||
// Attributes
|
||||
const FORM_BLOCK_ATTRIBUTES = 'form_block_attributes';
|
||||
|
||||
const BLOCK_ATTRIBUTES = 'block_attributes';
|
||||
|
||||
/**
|
||||
* Array mapping config class names to their container ID.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $configs = array(
|
||||
self::BLOCKS_CONFIG => GF_Blocks_Config::class,
|
||||
);
|
||||
|
||||
/**
|
||||
* Register services to the container.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
|
||||
require_once( plugin_dir_path( __FILE__ ) . 'class-gf-block-attributes.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/config/class-gf-blocks-config.php' );
|
||||
|
||||
$container->add( self::FORM_BLOCK_ATTRIBUTES, function () {
|
||||
require_once( GFCommon::get_base_path() . '/form_display.php' );
|
||||
$global_styles = GFFormDisplay::validate_form_styles( apply_filters( 'gform_default_styles', false ) );
|
||||
|
||||
return array(
|
||||
'formId' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'title' =>
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
'description' =>
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
'ajax' =>
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
),
|
||||
'tabindex' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'fieldValues' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
),
|
||||
'formPreview' =>
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
'imgPreview' =>
|
||||
array(
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
),
|
||||
'theme' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => '',
|
||||
),
|
||||
'inputSize' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'inputSize' ) ? $global_styles['inputSize'] : 'md',
|
||||
),
|
||||
'inputBorderRadius' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'inputBorderRadius' ) ? $global_styles['inputBorderRadius'] : 3,
|
||||
),
|
||||
'inputBorderColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'inputBorderColor' ) ? $global_styles['inputBorderColor'] : '#686e77',
|
||||
),
|
||||
'inputBackgroundColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'inputBackgroundColor' ) ? $global_styles['inputBackgroundColor'] : '#fff',
|
||||
),
|
||||
'inputColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'inputColor' ) ? $global_styles['inputColor'] : '#112337',
|
||||
),
|
||||
'inputPrimaryColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
// Setting this to empty allows us to set this to what the appropriate default
|
||||
// should be from within the block. When empty, it defaults to:
|
||||
// buttonPrimaryBackgroundColor
|
||||
'default' => rgar( $global_styles, 'inputPrimaryColor' ) ? $global_styles['inputPrimaryColor'] : '', // #204ce5
|
||||
),
|
||||
'labelFontSize' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'labelFontSize' ) ? $global_styles['labelFontSize'] : 14,
|
||||
),
|
||||
'labelColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'labelColor' ) ? $global_styles['labelColor'] : '#112337',
|
||||
),
|
||||
'descriptionFontSize' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'descriptionFontSize' ) ? $global_styles['descriptionFontSize'] : 13,
|
||||
),
|
||||
'descriptionColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'descriptionColor' ) ? $global_styles['descriptionColor'] : '#585e6a',
|
||||
),
|
||||
'buttonPrimaryBackgroundColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'buttonPrimaryBackgroundColor' ) ? $global_styles['buttonPrimaryBackgroundColor'] : '#204ce5',
|
||||
),
|
||||
'buttonPrimaryColor' =>
|
||||
array(
|
||||
'type' => 'string',
|
||||
'default' => rgar( $global_styles, 'buttonPrimaryColor' ) ? $global_styles['buttonPrimaryColor'] : '#fff',
|
||||
),
|
||||
);
|
||||
} );
|
||||
|
||||
$this->add_configs( $container );
|
||||
$this->block_attributes( $container );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize any actions or hooks.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
|
||||
add_action( 'gform_post_enqueue_scripts', function( $found_forms, $found_blocks, $post ) use ( $container ) {
|
||||
foreach( $found_blocks as $block ) {
|
||||
$attributes = $block['attrs'];
|
||||
$container->get( self::BLOCK_ATTRIBUTES )->store( $attributes );
|
||||
}
|
||||
}, -10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* For each config defined in $configs, instantiate and add to container.
|
||||
*
|
||||
* @since
|
||||
*
|
||||
* @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 ) {
|
||||
if ( $class == GF_Blocks_Config::class ) {
|
||||
return new $class( $container->get( GF_Config_Service_Provider::DATA_PARSER ), $container->get( self::FORM_BLOCK_ATTRIBUTES ) );
|
||||
}
|
||||
|
||||
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 Block services.
|
||||
*
|
||||
* @since 2.7.4
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function block_attributes( GF_Service_Container $container ) {
|
||||
$container->add( self::BLOCK_ATTRIBUTES, function () use ( $container ) {
|
||||
return new GF_Block_Attributes();
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) or die();
|
||||
|
||||
// Load core Block class.
|
||||
require_once( plugin_dir_path( __FILE__ ) . 'class-gf-block.php' );
|
||||
|
||||
/**
|
||||
* Handles management of Gravity Forms editor blocks.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* Class GF_Blocks
|
||||
*/
|
||||
class GF_Blocks {
|
||||
|
||||
/**
|
||||
* Registered Gravity Forms editor blocks.
|
||||
*
|
||||
* @since 2.4.10
|
||||
* @var GF_Block[]
|
||||
*/
|
||||
private static $_blocks = array();
|
||||
|
||||
/**
|
||||
* Register a block type.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @param GF_Block $block Block class.
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public static function register( $block ) {
|
||||
|
||||
if ( ! is_subclass_of( $block, 'GF_Block' ) ) {
|
||||
return new WP_Error( 'block_not_subclass', 'Must be a subclass of GF_Block' );
|
||||
}
|
||||
|
||||
// Get block type.
|
||||
$block_type = $block->get_type();
|
||||
|
||||
if ( empty( $block_type ) ) {
|
||||
return new WP_Error( 'block_type_undefined', 'The type must be set' );
|
||||
}
|
||||
|
||||
if ( isset( self::$_blocks[ $block_type ] ) ) {
|
||||
return new WP_Error( 'block_already_registered', 'Block type already registered: ' . $block_type );
|
||||
}
|
||||
|
||||
// Register block.
|
||||
self::$_blocks[ $block_type ] = $block;
|
||||
|
||||
// Initialize block.
|
||||
call_user_func( array( $block, 'init' ) );
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance of block.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @param string $block_type Block type.
|
||||
*
|
||||
* @return GF_Block|bool
|
||||
*/
|
||||
public static function get( $block_type ) {
|
||||
|
||||
return isset( self::$_blocks[ $block_type ] ) ? self::$_blocks[ $block_type ] : false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of registered block types.
|
||||
*
|
||||
* @since 2.4.18
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_all_types() {
|
||||
return array_keys( self::$_blocks );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new GF_Blocks();
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Blocks\Config;
|
||||
|
||||
use GFSettings;
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Data_Parser;
|
||||
use \GFCommon;
|
||||
use \GFAPI;
|
||||
use \GFFormDisplay;
|
||||
|
||||
/**
|
||||
* Config items for Blocks.
|
||||
*
|
||||
* @since
|
||||
*/
|
||||
class GF_Blocks_Config extends GF_Config {
|
||||
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
protected $attributes = array();
|
||||
|
||||
public function __construct( GF_Config_Data_Parser $parser, array $attributes ) {
|
||||
parent::__construct( $parser );
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
public function should_enqueue() {
|
||||
return GFCommon::is_block_editor_page();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of forms for Block control.
|
||||
*
|
||||
* @since 2.4.10
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_forms() {
|
||||
|
||||
// Initialize forms array.
|
||||
$forms = array();
|
||||
|
||||
// Load GFFormDisplay class.
|
||||
if ( ! class_exists( 'GFFormDisplay' ) ) {
|
||||
require_once GFCommon::get_base_path() . '/form_display.php';
|
||||
}
|
||||
|
||||
// Get form objects.
|
||||
$form_objects = GFAPI::get_forms( true, false, 'title', 'ASC' );
|
||||
|
||||
// Loop through forms, add conditional logic check.
|
||||
foreach ( $form_objects as $form ) {
|
||||
$forms[] = array(
|
||||
'id' => $form['id'],
|
||||
'title' => $form['title'],
|
||||
'hasConditionalLogic' => GFFormDisplay::has_conditional_logic( $form ),
|
||||
'isLegacyMarkup' => GFCommon::is_legacy_markup_enabled( $form ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the list of available forms displayed in the Form block.
|
||||
*
|
||||
* @since 2.4.23
|
||||
*
|
||||
* @param array $forms A collection of active forms on site.
|
||||
*/
|
||||
return apply_filters( 'gform_block_form_forms', $forms );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
$attributes = apply_filters( 'gform_form_block_attributes', $this->attributes );
|
||||
|
||||
$orbital_default = GFSettings::is_orbital_default();
|
||||
|
||||
return array(
|
||||
'gravityforms/form' => array(
|
||||
'data' => array(
|
||||
'attributes' => $attributes,
|
||||
'adminURL' => admin_url( 'admin.php' ),
|
||||
'forms' => $this->get_forms(),
|
||||
'preview' => GFCommon::get_base_url() . '/images/gf_block_preview.svg',
|
||||
'orbitalDefault' => $orbital_default,
|
||||
'styles' => array(
|
||||
'defaults' => \GFForms::get_service_container()->get( \Gravity_Forms\Gravity_Forms\Form_Display\GF_Form_Display_Service_Provider::BLOCK_STYLES_DEFAULTS ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user