plugin install
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Assets\GF_Dependencies;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Admin_Script_Dependencies
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies;
|
||||
*/
|
||||
class GF_Admin_Script_Dependencies extends GF_Dependencies {
|
||||
|
||||
/**
|
||||
* Items to enqueue globally in admin.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $items = array(
|
||||
'gform_gravityforms_admin',
|
||||
);
|
||||
|
||||
/**
|
||||
* Enqueue the item by handle.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $handle
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function do_enqueue( $handle ) {
|
||||
wp_enqueue_script( $handle );
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the global scripts should enqueue.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function should_enqueue() {
|
||||
/***
|
||||
* The newer JavaScript added in 2.5 is now enqueued globally in the admin.
|
||||
* We implemented this as we are now using code splitting to only inject JavaScript
|
||||
* dynamically as it is needed, and to also allow our addons easy access to the core libraries
|
||||
* we use.
|
||||
*
|
||||
* This filter allows users to make our admin scripts only load on Gravity Forms admin screens.
|
||||
* Setting it to false may cause unexpected behavior/feature loss in some addons or core.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*
|
||||
* @param bool true Load admin scripts globally?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
return apply_filters( 'gform_load_admin_scripts_globally', true );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Assets\GF_Dependencies;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Admin_Style_Dependencies
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies;
|
||||
*/
|
||||
class GF_Admin_Style_Dependencies extends GF_Dependencies {
|
||||
|
||||
/**
|
||||
* Items to enqueue globally in admin.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $items = array(
|
||||
'gform_common_css_utilities',
|
||||
'gform_common_icons',
|
||||
'gform_admin_icons',
|
||||
'gform_admin_components',
|
||||
'gform_admin_css_utilities',
|
||||
);
|
||||
|
||||
/**
|
||||
* Enqueue the item by handle.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $handle
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function do_enqueue( $handle ) {
|
||||
wp_enqueue_style( $handle );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets;
|
||||
|
||||
class GF_Asset_Processor {
|
||||
|
||||
/**
|
||||
* @var array $map - The Hash Map generated by our node scripts.
|
||||
*/
|
||||
private $map;
|
||||
|
||||
/**
|
||||
* @var string $asset_path - The path to the js dist directory.
|
||||
*/
|
||||
private $asset_path;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param array $map
|
||||
* @param string $asset_path
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $map, $asset_path ) {
|
||||
$this->map = $map;
|
||||
$this->asset_path = $asset_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform processing actions on assets.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process_assets() {
|
||||
$this->process_versions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the ver values for all of the registered scripts in order to append a
|
||||
* file hash (if it exists) or the filemtime (if required).
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function process_versions() {
|
||||
global $wp_scripts;
|
||||
|
||||
$registered = $wp_scripts->registered;
|
||||
|
||||
foreach( $registered as &$asset ) {
|
||||
|
||||
// Bail if not one of our assets.
|
||||
if ( $asset->src && strpos( $asset->src, 'gravityforms/assets/js/dist' ) === false ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$basename = basename( $asset->src );
|
||||
$path = sprintf( '%s/%s', $this->asset_path, $basename );
|
||||
|
||||
// Asset doesn't exist in hash_map, skip.
|
||||
if ( ! array_key_exists( $basename, $this->map ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The hash is either the value from our map, or the filemtime for dev.
|
||||
$hash = defined( 'GF_DEV_TIME_AS_VER' ) && GF_DEV_TIME_AS_VER ?
|
||||
filemtime( $path ) :
|
||||
$this->map[ $basename ]['version'];
|
||||
|
||||
$asset->ver = $hash;
|
||||
}
|
||||
|
||||
$wp_scripts->registered = $registered;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Assets\Theme_Dependencies\GF_Theme_Script_Dependencies;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies\GF_Admin_Script_Dependencies;
|
||||
use Gravity_Forms\Gravity_Forms\Assets\Admin_Dependencies\GF_Admin_Style_Dependencies;
|
||||
|
||||
/**
|
||||
* Class GF_Asset_Service_Provider
|
||||
*
|
||||
* Service provider for assets.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Merge_Tags;
|
||||
*/
|
||||
class GF_Asset_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
const HASH_MAP = 'hash_map';
|
||||
const ASSET_PROCESSOR = 'asset_processor';
|
||||
const STYLE_DEPS = 'gf_global_style_deps';
|
||||
const SCRIPT_DEPS = 'gf_global_script_deps';
|
||||
const SCRIPT_DEPS_THEME = 'gf_global_script_deps_theme';
|
||||
const SVG_OPTIONS = 'gf_svg_options';
|
||||
|
||||
private $plugin_dir;
|
||||
|
||||
public function __construct( $plugin_dir ) {
|
||||
$this->plugin_dir = $plugin_dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services to the container.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/class-gf-asset-processor.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/class-gf-dependencies.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/admin-dependencies/class-gf-admin-script-dependencies.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/admin-dependencies/class-gf-admin-style-dependencies.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . '/theme-dependencies/class-gf-theme-script-dependencies.php' );
|
||||
|
||||
$container->add( self::HASH_MAP, function () {
|
||||
if ( ! file_exists( \GFCommon::get_base_path() . '/assets/js/dist/assets.php' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$map = require( \GFCommon::get_base_path() . '/assets/js/dist/assets.php' );
|
||||
|
||||
return rgar( $map, 'hash_map', array() );
|
||||
} );
|
||||
|
||||
$container->add( self::ASSET_PROCESSOR, function () use ( $container ) {
|
||||
$basepath = \GFCommon::get_base_path();
|
||||
$asset_path = sprintf( '%s/assets/js/dist/', $basepath );
|
||||
|
||||
return new GF_Asset_Processor( $container->get( self::HASH_MAP ), $asset_path );
|
||||
} );
|
||||
|
||||
$container->add( self::STYLE_DEPS, function () {
|
||||
return new GF_Admin_Style_Dependencies();
|
||||
} );
|
||||
|
||||
$container->add( self::SCRIPT_DEPS, function () {
|
||||
return new GF_Admin_Script_Dependencies();
|
||||
} );
|
||||
|
||||
$container->add( self::SCRIPT_DEPS_THEME, function () {
|
||||
return new GF_Theme_Script_Dependencies();
|
||||
} );
|
||||
|
||||
$this->svg_delivery( $container );
|
||||
}
|
||||
|
||||
public function init( GF_Service_Container $container ) {
|
||||
add_action( 'init', function () use ( $container ) {
|
||||
$container->get( self::ASSET_PROCESSOR )->process_assets();
|
||||
}, 9999 );
|
||||
|
||||
add_action( 'admin_enqueue_scripts', function () use ( $container ) {
|
||||
$container->get( self::STYLE_DEPS )->enqueue();
|
||||
$container->get( self::SCRIPT_DEPS )->enqueue();
|
||||
|
||||
// Styles and scripts required for the tooltips.
|
||||
wp_enqueue_style( 'gform_font_awesome' );
|
||||
wp_enqueue_script( 'gform_tooltip_init' );
|
||||
} );
|
||||
|
||||
add_action( 'gform_enqueue_scripts', function () use ( $container ) {
|
||||
$container->get( self::SCRIPT_DEPS_THEME )->enqueue();
|
||||
} );
|
||||
|
||||
add_filter( 'gform_noconflict_styles', function ( $styles ) use ( $container ) {
|
||||
return array_merge( $styles, $container->get( self::STYLE_DEPS )->get_items() );
|
||||
}, 1 );
|
||||
}
|
||||
|
||||
private function svg_delivery( GF_Service_Container $container ) {
|
||||
$default_path = sprintf( '%s/assets/img/base', untrailingslashit( $this->plugin_dir ) );
|
||||
|
||||
/**
|
||||
* Allows users to filter the path used to glob the available SVGs to use for display in themes.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param string $path The default orbital theme svg path within our plugin.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
$svg_path = apply_filters( 'gform_svg_theme_path', $default_path );
|
||||
|
||||
$svgs = array();
|
||||
|
||||
foreach ( \GFCommon::glob( '*.svg', trailingslashit( $svg_path ) ) as $filename ) {
|
||||
$key = pathinfo( $filename, PATHINFO_FILENAME );
|
||||
$svgs[ $key ] = file_get_contents( $filename );
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows users to filter the SVG options available to output in themes.
|
||||
*
|
||||
* @since 2.7
|
||||
*
|
||||
* @param array $svgs The current SVG options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
$svgs = apply_filters( 'gform_svg_theme_options', $svgs );
|
||||
|
||||
$container->add( self::SVG_OPTIONS, $svgs );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Gravity Forms Abstract Asset
|
||||
*
|
||||
* Provides base functionality for enqueueable/printable assets.
|
||||
*
|
||||
* @since 2.5
|
||||
* @package gravityforms
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'GFForms' ) ) {
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class GF_Asset
|
||||
*/
|
||||
abstract class GF_Asset {
|
||||
|
||||
/**
|
||||
* @var string $handle
|
||||
*/
|
||||
protected $handle;
|
||||
|
||||
/**
|
||||
* @var string $url
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* GF_Asset constructor.
|
||||
*
|
||||
* @param string $handle
|
||||
* @param string $url
|
||||
*/
|
||||
public function __construct( $handle, $url = '' ) {
|
||||
$this->handle = $handle;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle enqueueing the asset this class represents (e.g., using wp_enqueue_script() or wp_enqueue_style())
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function enqueue_asset();
|
||||
|
||||
/**
|
||||
* Handle printing the asset this class represents (e.g., using wp_print_scripts() or wp_print_styles())
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function print_asset();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets;
|
||||
|
||||
abstract class GF_Dependencies {
|
||||
|
||||
/**
|
||||
* Items to enqueue
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $items = array();
|
||||
|
||||
/**
|
||||
* The method for actually enqueueing the items.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $handle
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function do_enqueue( $handle );
|
||||
|
||||
/**
|
||||
* Get the dependency items.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_items() {
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the items.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $items
|
||||
*/
|
||||
public function enqueue() {
|
||||
if ( ! $this->should_enqueue() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->items as $handle ) {
|
||||
$this->do_enqueue( $handle );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override to determine whether the assets outlined should be enqueued.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function should_enqueue() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* Gravity Forms Script Asset
|
||||
*
|
||||
* @since 2.5
|
||||
* @package gravityforms
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'GFForms' ) ) {
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class GF_Script_Asset
|
||||
*/
|
||||
class GF_Script_Asset extends GF_Asset {
|
||||
|
||||
/**
|
||||
* The data for any localized information.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @var array $localize_data
|
||||
*/
|
||||
private $localize_data = array();
|
||||
|
||||
/**
|
||||
* Localize data to this script.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @param string $object_name
|
||||
* @param array $data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_localize_data( $object_name, $data ) {
|
||||
$this->localize_data[ $object_name ] = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the asset.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_asset() {
|
||||
wp_enqueue_script( $this->handle, $this->url );
|
||||
|
||||
if ( empty( $this->localize_data ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->localize_data as $object_name => $data ) {
|
||||
wp_localize_script( $this->handle, $object_name, $data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the asset.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function print_asset() {
|
||||
$this->enqueue_asset();
|
||||
|
||||
wp_print_scripts( $this->handle );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Gravity Forms Style Asset
|
||||
*
|
||||
* @since 2.5
|
||||
* @package gravityforms
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'GFForms' ) ) {
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class GF_Style_Asset
|
||||
*/
|
||||
class GF_Style_Asset extends GF_Asset {
|
||||
|
||||
/**
|
||||
* Enqueue the asset.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue_asset() {
|
||||
wp_enqueue_style( $this->handle, $this->url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the asset.
|
||||
*
|
||||
* @since 2.5
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function print_asset() {
|
||||
$this->enqueue_asset();
|
||||
wp_print_styles( $this->handle );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
//Nothing to see here
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Assets\Theme_Dependencies;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Assets\GF_Dependencies;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
/**
|
||||
* Class GF_Theme_Script_Dependencies
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Assets\Theme_Dependencies;
|
||||
*/
|
||||
class GF_Theme_Script_Dependencies extends GF_Dependencies {
|
||||
|
||||
/**
|
||||
* Items to enqueue globally in admin.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $items = array(
|
||||
'gform_gravityforms_theme',
|
||||
);
|
||||
|
||||
/**
|
||||
* Enqueue the item by handle.
|
||||
*
|
||||
* @since 2.6
|
||||
*
|
||||
* @param $handle
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function do_enqueue( $handle ) {
|
||||
wp_enqueue_script( $handle );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user