2021-04-26 22:57:04 +01:00
< ? php
/**
* The public - facing functionality of the plugin .
*
2021-05-01 21:34:46 +01:00
* @ package footnotes
* @ since 2.8 . 0
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
declare ( strict_types = 1 );
2021-05-01 21:34:46 +01:00
namespace footnotes\general ;
2021-05-22 19:31:12 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/settings/general/class-reference-container-settings-group.php' ;
2021-08-07 16:45:47 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/settings/general/class-scrolling-settings-group.php' ;
2021-05-01 21:38:11 +01:00
2021-05-22 18:16:31 +01:00
use footnotes\includes\ { Footnotes , Convert , Settings };
2021-08-07 16:45:47 +01:00
/**
* @ todo Replace with constant imports .
*/
use footnotes\includes\settings\general\ReferenceContainerSettingsGroup ;
use footnotes\includes\settings\general\AMPCompatSettingsGroup ;
use footnotes\includes\settings\referrersandtooltips\TooltipsSettingsGroup ;
2021-05-01 21:34:46 +01:00
2021-04-26 22:57:04 +01:00
/**
2021-05-01 21:34:46 +01:00
* Class provide all public - facing functionality of the plugin .
2021-04-26 22:57:04 +01:00
*
* Defines the plugin name , version , and enqueues all public - facing stylesheets
* and JavaScript .
*
2021-05-01 21:34:46 +01:00
* @ package footnotes
* @ since 2.8 . 0
2021-04-26 22:57:04 +01:00
*/
2021-05-01 21:34:46 +01:00
class General {
2021-04-26 22:57:04 +01:00
/**
* The ID of this plugin .
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
2021-05-01 19:22:41 +01:00
2021-04-30 18:03:15 +01:00
* @ access private
* @ var string $plugin_name The ID of this plugin .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
private string $plugin_name ;
2021-04-26 22:57:04 +01:00
/**
* The version of this plugin .
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
2021-05-01 19:22:41 +01:00
2021-04-30 18:03:15 +01:00
* @ access private
* @ var string $version The current version of this plugin .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
private string $version ;
2021-04-27 09:54:07 +01:00
2021-04-27 09:31:08 +01:00
/**
* The reference container widget .
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
*
2021-05-01 21:34:46 +01:00
* @ var Widget\Reference_Container $reference_container_widget The reference container widget
2021-04-27 09:31:08 +01:00
*/
2021-05-02 19:33:29 +01:00
private Widget\Reference_Container $reference_container_widget ;
2021-04-26 22:57:04 +01:00
/**
2021-04-27 09:31:08 +01:00
* The footnote parser .
2021-04-26 22:57:04 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-05-02 19:33:29 +01:00
* @ todo Review null init .
2021-04-30 18:03:15 +01:00
*
2021-05-01 21:34:46 +01:00
* @ var Parser $task The Plugin task .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 20:46:44 +01:00
public ? Parser $task = null ;
2021-04-26 22:57:04 +01:00
/**
* Flag for using tooltips .
*
2021-04-30 18:03:15 +01:00
* @ since 2.4 . 0
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-30 18:03:15 +01:00
*
* @ var bool $tooltips_enabled Whether tooltips are enabled or not .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 20:46:44 +01:00
public static $tooltips_enabled = false ;
2021-04-26 22:57:04 +01:00
/**
* Allows to determine whether alternative tooltips are enabled .
*
2021-04-30 18:03:15 +01:00
* @ since 2.1 . 1
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-30 18:03:15 +01:00
*
* @ var bool
2021-04-26 22:57:04 +01:00
*/
2021-05-02 20:46:44 +01:00
public static $alternative_tooltips_enabled = false ;
2021-04-26 22:57:04 +01:00
/**
* Allows to determine whether AMP compatibility mode is enabled .
*
2021-04-30 18:03:15 +01:00
* @ since 2.6 . 0
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-30 18:03:15 +01:00
*
* @ var bool
2021-04-26 22:57:04 +01:00
*/
2021-05-02 20:46:44 +01:00
public static $amp_enabled = false ;
2021-04-26 22:57:04 +01:00
/**
* Allows to determine the script mode among jQuery or plain JS .
*
2021-04-30 18:03:15 +01:00
* @ since 2.5 . 6
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-30 18:03:15 +01:00
*
* @ var string ‘ js’ to use plain JavaScript , ‘ jquery’ to use jQuery .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 20:46:44 +01:00
public static $script_mode = 'js' ;
2021-04-26 22:57:04 +01:00
/**
* Initialize the class and set its properties .
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
2021-05-01 19:22:41 +01:00
* @ param string $plugin_name The name of this plugin .
* @ param string $version The version of this plugin .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
public function __construct ( string $plugin_name , string $version ) {
2021-04-26 22:57:04 +01:00
$this -> plugin_name = $plugin_name ;
$this -> version = $version ;
$this -> load_dependencies ();
2021-04-26 23:04:20 +01:00
2021-04-26 22:57:04 +01:00
// Set conditions re-used for stylesheet enqueuing and in class/task.php.
2021-08-07 16:45:47 +01:00
self :: $amp_enabled = Settings :: instance () -> get_setting ( AMPCompatSettingsGroup :: FOOTNOTES_AMP_COMPATIBILITY_ENABLE [ 'key' ] ) -> get_value ();
self :: $tooltips_enabled = Settings :: instance () -> get_setting ( TooltipsSettingsGroup :: FOOTNOTES_MOUSE_OVER_BOX_ENABLED [ 'key' ] ) -> get_value ();
self :: $alternative_tooltips_enabled = Settings :: instance () -> get_setting ( TooltipsSettingsGroup :: FOOTNOTES_MOUSE_OVER_BOX_ALTERNATIVE [ 'key' ] ) -> get_value ();
self :: $script_mode = Settings :: instance () -> get_setting ( ReferenceContainerSettingsGroup :: FOOTNOTES_REFERENCE_CONTAINER_SCRIPT_MODE [ 'key' ] );
2021-04-26 22:57:04 +01:00
}
/**
* Load the required public - facing dependencies .
*
* Include the following files that provide the public - facing functionality
* of this plugin :
*
2021-05-01 21:34:46 +01:00
* - { @ see Parser } : parses Posts and Pages for footnote shortcodes ; and
* - { @ see Widget\Reference_Container } : defines the Reference Container widget .
2021-04-26 22:57:04 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 2.8 . 0
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
private function load_dependencies () : void {
2021-04-26 22:57:04 +01:00
// TODO: neaten up and document once placements and names are settled.
2021-05-01 21:34:46 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-config.php' ;
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-settings.php' ;
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'includes/class-convert.php' ;
2021-04-26 22:57:04 +01:00
2021-05-01 21:34:46 +01:00
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'public/class-parser.php' ;
require_once plugin_dir_path ( dirname ( __FILE__ ) ) . 'public/widget/class-reference-container.php' ;
2021-04-26 22:57:04 +01:00
2021-05-01 21:34:46 +01:00
$this -> reference_container_widget = new Widget\Reference_Container ( $this -> plugin_name );
2021-04-27 09:54:07 +01:00
2021-05-02 20:46:44 +01:00
$this -> task = new Parser ();
2021-04-26 22:57:04 +01:00
}
2021-08-07 16:45:47 +01:00
2021-04-26 22:57:04 +01:00
/**
* Register the stylesheets for the public - facing side of the site .
*
2021-04-30 18:03:15 +01:00
* Enables enqueuing the formatted individual stylesheets if { @ see PRODUCTION_ENV }
* is `true` .
2021-04-27 08:31:37 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ since 2.5 . 5 Change stylesheet schema .
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
public function enqueue_styles () : void {
2021-04-26 22:57:04 +01:00
if ( PRODUCTION_ENV ) {
// Set tooltip mode for use in stylesheet name.
2021-05-02 20:46:44 +01:00
if ( self :: $tooltips_enabled ) {
2021-04-26 22:57:04 +01:00
2021-05-02 20:46:44 +01:00
if ( self :: $amp_enabled ) {
$tooltip_mode_short = 'ampt' ;
$tooltip_mode_long = 'amp-tooltips' ;
2021-04-26 22:57:04 +01:00
2021-05-02 20:46:44 +01:00
} elseif ( self :: $alternative_tooltips_enabled ) {
$tooltip_mode_short = 'altt' ;
$tooltip_mode_long = 'alternative-tooltips' ;
2021-04-26 22:57:04 +01:00
} else {
2021-05-02 20:46:44 +01:00
$tooltip_mode_short = 'jqtt' ;
$tooltip_mode_long = 'jquery-tooltips' ;
2021-04-26 22:57:04 +01:00
}
} else {
2021-05-02 20:46:44 +01:00
$tooltip_mode_short = 'nott' ;
$tooltip_mode_long = 'no-tooltips' ;
2021-04-26 22:57:04 +01:00
}
// Set basic responsive page layout mode for use in stylesheet name.
2021-05-22 18:16:31 +01:00
$page_layout_option = Settings :: instance () -> get ( FOOTNOTES_PAGE_LAYOUT_SUPPORT );
2021-05-02 20:46:44 +01:00
switch ( $page_layout_option ) {
2021-04-26 22:57:04 +01:00
case 'reference-container' :
2021-05-02 20:46:44 +01:00
$layout_mode = '1' ;
2021-04-26 22:57:04 +01:00
break ;
case 'entry-content' :
2021-05-02 20:46:44 +01:00
$layout_mode = '2' ;
2021-04-26 22:57:04 +01:00
break ;
case 'main-content' :
2021-05-02 20:46:44 +01:00
$layout_mode = '3' ;
2021-04-26 22:57:04 +01:00
break ;
case 'none' :
default :
2021-05-02 20:46:44 +01:00
$layout_mode = '0' ;
2021-04-26 22:57:04 +01:00
break ;
}
// Enqueue the tailored united minified stylesheet.
wp_enqueue_style (
2021-05-02 20:46:44 +01:00
" footnotes- { $tooltip_mode_long } -pagelayout- { $page_layout_option } " ,
plugin_dir_url ( __FILE__ ) . " css/footnotes- { $tooltip_mode_short } brpl { $layout_mode } .min.css " ,
2021-04-26 22:57:04 +01:00
array (),
( PRODUCTION_ENV ) ? $this -> version : filemtime (
plugin_dir_path (
2021-05-02 19:33:29 +01:00
__FILE__
2021-05-02 20:46:44 +01:00
) . " css/footnotes- { $tooltip_mode_short } brpl { $layout_mode } .min.css "
2021-04-26 22:57:04 +01:00
),
'all'
);
2021-05-02 19:19:46 +01:00
} else {
2021-05-02 19:33:29 +01:00
foreach ( array ( 'amp-tooltips' , 'common' , 'layout-entry-content' , 'layout-main-content' , 'layout-reference-container' , 'tooltips' , 'tooltips-alternative' ) as $val ) {
2021-05-02 19:19:46 +01:00
wp_enqueue_style (
" footnotes- $val " ,
plugin_dir_url ( __FILE__ ) . " css/dev- $val .css " ,
array (),
filemtime (
plugin_dir_path (
2021-05-02 19:33:29 +01:00
__FILE__
2021-05-02 19:19:46 +01:00
) . " css/dev- $val .css "
),
'all'
);
}
2021-04-26 22:57:04 +01:00
}
}
/**
* Register the JavaScript for the public - facing side of the site .
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ since 2.0 . 0 Add jQueryUI dependency .
* @ since 2.1 . 2 Add jQuery Tools dependency .
* @ since 2.5 . 6 Add jQuery dependency .
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-26 22:57:04 +01:00
*/
2021-05-02 19:33:29 +01:00
public function enqueue_scripts () : void {
2021-04-30 18:03:15 +01:00
/*
2021-04-26 22:57:04 +01:00
* Enqueues the jQuery library registered by WordPress .
*
* As jQuery is also used for animated scrolling , it was loaded by default .
* The function `wp_enqueue_script()` avoids loading the same library multiple times .
* After adding the alternative reference container , jQuery has become optional ,
* but still enabled by default .
*/
2021-05-02 20:46:44 +01:00
if ( ! self :: $amp_enabled ) {
2021-04-26 22:57:04 +01:00
2021-05-02 20:46:44 +01:00
if ( 'jquery' === self :: $script_mode || ( self :: $tooltips_enabled && ! self :: $alternative_tooltips_enabled ) ) {
2021-04-26 22:57:04 +01:00
wp_enqueue_script ( 'jquery' );
}
2021-05-02 20:46:44 +01:00
if ( self :: $tooltips_enabled && ! self :: $alternative_tooltips_enabled ) {
2021-04-30 18:03:15 +01:00
/*
2021-04-26 22:57:04 +01:00
* Enqueues the jQuery Tools library shipped with the plugin .
*
2021-04-30 18:03:15 +01:00
* Redacted `jQuery.browser` , completed minification ;
2021-04-26 22:57:04 +01:00
* see full header in `public/js/jquery.tools.js` .
2021-04-30 18:03:15 +01:00
* No ‘ - js’ in the handle , is appended automatically .
2021-04-26 22:57:04 +01:00
* Deferring to the footer breaks jQuery tooltip display .
*/
wp_enqueue_script ( $this -> plugin_name , plugin_dir_url ( __FILE__ ) . 'js/jquery.tools' . ( ( PRODUCTION_ENV ) ? '.min' : '' ) . '.js' , array (), '1.2.7.redacted.2' , false );
2021-04-30 18:03:15 +01:00
/*
2021-04-26 22:57:04 +01:00
* Enqueues some jQuery UI libraries registered by WordPress .
*
* If alternative tooltips are enabled , these libraries are not needed .
*/
wp_enqueue_script ( 'jquery-ui-core' );
wp_enqueue_script ( 'jquery-ui-widget' );
wp_enqueue_script ( 'jquery-ui-position' );
wp_enqueue_script ( 'jquery-ui-tooltip' );
}
}
}
2021-04-27 09:54:07 +01:00
2021-04-27 09:31:08 +01:00
/**
* Register the widget ( s ) for the public - facing side of the site .
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
2021-05-22 18:16:31 +01:00
* @ since 2.8 . 0 Moved from { @ see Footnotes } to { @ see General } .
2021-04-27 09:31:08 +01:00
*/
2021-05-02 19:33:29 +01:00
public function register_widgets () : void {
2021-04-27 09:31:08 +01:00
register_widget ( $this -> reference_container_widget );
}
2021-04-26 22:57:04 +01:00
}