Skip to content

Commit

Permalink
Plugins execution fully OOP, layout fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcvignoli committed Mar 17, 2024
1 parent 292cfa3 commit 6c465b2
Show file tree
Hide file tree
Showing 34 changed files with 931 additions and 829 deletions.
2 changes: 2 additions & 0 deletions dist/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
v.4.0.3
* [feature] Thumbnail pictures in taxonomy pages will be quicker to be displayed (using lazy loading, thumbs for the thumb images preciseley)
* [bug] When reseting options in General options, no confirmation message was displayed.
* [bug] Style of links to movie popups was not grey, had no more the small picture on the lef
* [bug] Update wasn't working (Casting now to string in class update the option 'imdbHowManyUpdates', which doesn't actually make sense but works)
* [technical] Gutenberg block registration is now done in more modern way (using register_block_type() and block.json). Splitted "add imdb link" and "open search" into two blocks. Widget is also registered that way.
* [technical] Due to Gutenberg block limitation, a class is now added in <span data-lum_movie_maker=""></span>. The plugin remains fully compatible with the span without class. (Edited regexes in class movie to take into account the possiblity that spans can include a class)
* [technical] Plugins loading is fully OOP
* [technical] Bot banning status changed from 403 to 400, which is more appropriate

v.4.0.2
Expand Down
2 changes: 1 addition & 1 deletion dist/assets/css/lumiere.min.css

Large diffs are not rendered by default.

99 changes: 19 additions & 80 deletions dist/class/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
}

use Lumiere\Admin\Cache_Tools;
use Lumiere\Tools\Plugins_Detect;
use Lumiere\Tools\Settings_Global;
use Lumiere\Plugins\Amp;
use Lumiere\Plugins\Logger;
use Lumiere\Plugins\Polylang;
use Lumiere\Updates;

/**
Expand Down Expand Up @@ -72,7 +69,8 @@ public function __construct () {
add_action( 'init', [ $this, 'lumiere_register_assets' ], 0 );

// Execute javascripts and styles.
add_action( 'wp_enqueue_scripts', [ $this, 'lumiere_frontpage_execute_assets' ], 0 );
add_action( 'wp_enqueue_scripts', [ $this, 'lumiere_frontpage_execute_assets' ], 9 );
add_action( 'wp_enqueue_scripts', [ $this, 'lumiere_frontpage_execute_assets_priority' ], 0 );

// Register Gutenberg blocks.
add_action( 'init', [ $this, 'lumiere_register_gutenberg_blocks' ] );
Expand All @@ -84,21 +82,6 @@ public function __construct () {
add_action( 'init', fn() => Tools\Ban_Bots::lumiere_static_start(), 0 );
}

// AMP remove headers if AMP is active.
add_action(
'wp',
function(): void {
$plugins_detect_class = new Plugins_Detect();
if (
count( $plugins_detect_class->plugins_class ) > 0
&& in_array( 'AMP', $plugins_detect_class->plugins_class, true )
) {
$amp_class = new Amp();
$amp_class->lumiere_amp_remove_header();
}
}
);

/**
* Updates & Crons. Must be free of any conditions.
*/
Expand Down Expand Up @@ -155,22 +138,6 @@ public function lumiere_register_assets(): void {
$this->config_class->lumiere_version
);

// Register OceanWP theme fixes for popups only
wp_register_style(
'lumiere_style_oceanwpfixes_popups',
$this->config_class->lumiere_css_dir . 'lumiere-subpages-oceanwpfixes.min.css',
[],
$this->config_class->lumiere_version
);

// Register OceanWP theme fixes for all pages but popups
wp_register_style(
'lumiere_style_oceanwpfixes_general',
$this->config_class->lumiere_css_dir . 'lumiere-extrapages-oceanwpfixes.min.css',
[],
$this->config_class->lumiere_version
);

}

/**
Expand All @@ -188,7 +155,7 @@ public function lumiere_register_gutenberg_blocks(): void {
}

/**
* Add the stylesheet & javascript to frontpage.
* Execute Frontpage stylesheets & javascripts.
*/
public function lumiere_frontpage_execute_assets(): void {

Expand All @@ -200,43 +167,33 @@ public function lumiere_frontpage_execute_assets(): void {
wp_enqueue_style( 'lumiere_style_main' );
}

// OceanWP template css fix.
// Enqueues lumiere.css only if using oceanwp template.
// Popups.
if (
( 0 === stripos( get_template_directory_uri(), esc_url( site_url() . '/wp-content/themes/oceanwp' ) ) )
&&
( str_contains( $_SERVER['REQUEST_URI'] ?? '', site_url( '', 'relative' ) . $this->config_class->lumiere_urlstring ) )
) {

wp_enqueue_style( 'lumiere_style_oceanwpfixes_popups' );

// All other cases.
} elseif ( 0 === stripos( get_template_directory_uri(), esc_url( site_url() . '/wp-content/themes/oceanwp' ) ) ) {

wp_enqueue_style( 'lumiere_style_oceanwpfixes_general' );

}

wp_enqueue_script( 'lumiere_hide_show' );

if ( wp_script_is( 'lumiere_scripts', 'enqueued' ) ) {
return;
}

wp_enqueue_script( 'lumiere_scripts' );

/**
* Pass variables to javascript lumiere_scripts.js.
* These variables contains popup sizes, color, paths, etc.
*/
wp_add_inline_script(
'lumiere_scripts',
$this->wrap_lumiere_script(),
'before'
$this->config_class->lumiere_scripts_vars,
);
}

/**
* Execute lumiere_scripts Frontpage javascript.
* This must be run in 0 priority, otherwhise wp_add_inline_script() in lumiere_frontpage_execute_assets() doesn't get the vars
* @since 4.0.3
*/
public function lumiere_frontpage_execute_assets_priority(): void {

if ( wp_script_is( 'lumiere_scripts', 'enqueued' ) ) {
return;
}

wp_enqueue_script( 'lumiere_scripts' );

}

/**
* Run on lumiere WordPress manual upgrade
*
Expand Down Expand Up @@ -371,23 +328,5 @@ public function lumiere_on_deactivation(): void {

$this->logger->log()->info( '[Lumiere][coreClass][deactivation] Lumière deactivated' );
}

/**
* Wrap the lumiere script
* Currenty replaces the home_url() in popups with pll_home_url for use with Polylang
* The $lumiere_scripts_vars is a var in class Settings that can't be changed (executed too early)
*/
private function wrap_lumiere_script(): string {

$polylang_class = new Polylang();
$final_lumiere_script =
$polylang_class->polylang_is_active() === true
? $polylang_class->rewrite_string_with_polylang_url(
$this->config_class->lumiere_scripts_vars,
$this->imdb_admin_values['imdburlpopups']
)
: $this->config_class->lumiere_scripts_vars;
return $final_lumiere_script;
}
}

34 changes: 18 additions & 16 deletions dist/class/frontend/class-movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Movie {
*
* @var bool $movie_run_once
*/
private bool $movie_run_once = false;
private bool $movie_run_once;

/**
* Name of the class
Expand All @@ -53,6 +53,8 @@ public function __construct() {
// Construct Frontend trait.
$this->__constructFrontend( self::CLASS_NAME );

$this->movie_run_once = false;

// Parse the content to add the movies.
add_filter( 'the_content', [ $this, 'lumiere_parse_spans' ] );

Expand All @@ -77,23 +79,23 @@ public function __construct() {
*/
public function lumiere_show( ?array $imdb_id_or_title_outside = null ): string {

/**
* Start PluginsDetect class
* Is instanciated only if not instanciated already
* Use lumiere_set_plugins_array() in trait to set $plugins_in_use var in trait
*/
if ( count( $this->plugins_in_use ) === 0 ) {
$this->lumiere_set_plugins_array();
}

// Show log for link maker and plugin detect
if ( $this->movie_run_once === false ) {

/**
* Start PluginsDetect class
* Is instanciated only if not instanciated already
* Use lumiere_set_plugins_array() in trait to set $plugins_active_names var in trait
*/
if ( count( $this->plugins_active_names ) === 0 ) {
$this->activate_plugins();
}

// Log the current link maker
$this->logger->log()->debug( '[Lumiere][' . self::CLASS_NAME . '] Using the link maker class: ' . str_replace( 'Lumiere\Link_Makers\\', '', get_class( $this->link_maker ) ) );

// Log PluginsDetect, $this->plugins_in_use in trait
$this->logger->log()->debug( '[Lumiere][' . self::CLASS_NAME . '] The following plugins compatible with Lumière! are in use: [' . join( ', ', $this->plugins_in_use ) . ']' );
// Log PluginsDetect, $this->plugins_classes_active in trait
$this->logger->log()->debug( '[Lumiere][' . self::CLASS_NAME . '] The following plugins compatible with Lumière! are in use: [' . join( ', ', $this->plugins_active_names ) . ']' );
$this->logger->log()->debug( '[Lumiere][' . self::CLASS_NAME . '] Calling IMDbPHP class.' );

// Set the trigger to true so this is not called again.
Expand Down Expand Up @@ -267,7 +269,7 @@ public function parse_lumiere_tag_transform_id( $atts, ?string $content ): strin
}

/**
* Replace <span class="lumiere_link_maker"></span> with links
* Replace <span class="lumiere_link_maker"(anything)?></span> with links
*
* @param null|string $text parsed data
* @since 4.0.3 Added the possibility to have some text after the data with [^>]*
Expand Down Expand Up @@ -301,7 +303,7 @@ private function lumiere_link_finder( array $correspondances ): string {

$correspondances = $correspondances[0];
$pattern = '~<span data-lum_link_maker="popup"[^>]*>(.+?)<\/span>~i'; // identical to $pattern in lumiere_link_popup_maker().
$result = preg_match( $pattern, $correspondances[0], $matches );
$result = preg_match( $pattern, $correspondances, $matches );
return $result > 0 ? $this->link_maker->lumiere_popup_film_link( $matches ) : $correspondances;
}

Expand All @@ -318,7 +320,7 @@ private function lumiere_link_finder_oldway( array $correspondances ): string {

$correspondances = $correspondances[0];
$pattern = '~<!--imdb-->(.*?)<!--\/imdb-->~i'; // identical to $pattern in lumiere_link_popup_maker().
$result = preg_match( $pattern, $correspondances[0], $matches );
$result = preg_match( $pattern, $correspondances, $matches );
return $result > 0 ? $this->link_maker->lumiere_popup_film_link( $matches ) : $correspondances;
}

Expand All @@ -331,7 +333,7 @@ private function lumiere_link_finder_oldway( array $correspondances ): string {
* @param string|null $filmid
* @param string|null $external set to 'external' for use from outside
*/
public function lumiere_external_call ( ?string $moviename = null, ?string $filmid = null, ?string $external = null ): string {
public function lumiere_external_call( ?string $moviename = null, ?string $filmid = null, ?string $external = null ): string {

$imdb_id_or_title = [];

Expand Down
4 changes: 2 additions & 2 deletions dist/class/frontend/link_makers/class-abstract-link-maker.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ protected function lumiere_imdburl_to_popupurl_abstract ( string $text, int $win
$popup_link_person = '';
$popup_link_movie = '';

switch ( intval( $window_type ) ) {
switch ( $window_type ) {
case 0: // Build modal window popups.
$popup_link_person = '<a class="modal_window_people ' . $specific_class . '" data-modal_window_people="${4}" title="' . esc_html__( 'open a new window with IMDb informations', 'lumiere-movies' ) . '">${6}</a>';
$popup_link_movie = '<a class="modal_window_film ' . $specific_class . '" data-modal_window_filmid="${4}" title="' . esc_html__( 'open a new window with IMDb informations', 'lumiere-movies' ) . '">${6}</a>';
Expand Down Expand Up @@ -661,7 +661,7 @@ protected function lumiere_popup_film_link_abstract ( array $link_parsed, ?strin
// AMP & No Link modal
} elseif ( $window_type === 2 ) {

$txt = '<a class="link-imdblt-classicfilm" href="' . $this->config_class->lumiere_urlpopupsfilms . '?film=' . $title_attr . '" title="' . esc_html__( 'No Links', 'lumiere-movies' ) . '">' . $title_esc . '</a>';
$txt = '<a class="link_classic_film" href="' . $this->config_class->lumiere_urlpopupsfilms . '?film=' . $title_attr . '" title="' . esc_html__( 'No Links', 'lumiere-movies' ) . '">' . $title_esc . '</a>';

}

Expand Down
54 changes: 34 additions & 20 deletions dist/class/frontend/trait-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,40 @@
wp_die( esc_html__( 'Lumière Movies: You can not call directly this page', 'lumiere-movies' ) );
}

use Lumiere\Tools\Plugins_Detect;
use Lumiere\Plugins\Plugins_Start;
use Lumiere\Tools\Utils;
use Lumiere\Tools\Settings_Global;
use Lumiere\Link_Makers\Link_Factory;
use Lumiere\Plugins\Logger;
use Lumiere\Plugins\Imdbphp;
use Lumiere\Plugins\Polylang;

/**
* Frontend trait
* Popups, movies are using this trait
* Popups, movies and taxonomy use this trait
* Allow to use the logger, function utilities, and settings
* @phpstan-import-type PLUGINS_AVAILABLE from \Lumiere\Plugins\Plugins_Detect
*/
trait Main {

// Global settings trait.
use Settings_Global;

/**
* \Lumière\Plugins class
* Array of plugins in use
* Name of the plugins active
*
* @since 3.7
* @var array<int, string>
* @phpstan-ignore-next-line PHPStan complains that var is not defined for some contexts
* @since 4.0.3
* @var array<string>
*/
public array $plugins_active_names = [];

/**
* Classes that have been activated
*
* @since 4.0.3
* @var array<string, object> $plugins_classes_active
* @ phpstan-var array<string, PLUGINS_AVAILABLE> $plugins_classes_active
*/
public array $plugins_in_use = [];
public array $plugins_classes_active = [];

/**
* Class for building links, i.e. Highslide
Expand Down Expand Up @@ -83,7 +90,10 @@ trait Main {
*/
public function __construct( string $logger_name = 'unknownOrigin', bool $screen_output = true ) {

// Get Global Settings class properties.
/**
* Get Global Settings class properties.
* Create the properties needed
*/
$this->get_settings_class();
$this->get_db_options();

Expand Down Expand Up @@ -118,21 +128,20 @@ public function __construct( string $logger_name = 'unknownOrigin', bool $screen
*/
public function lumiere_log_plugins(): void {

$this->logger->log()->debug( '[Lumiere] The following plugins compatible with Lumière! are in use: [' . join( ', ', $this->plugins_in_use ) . ' ]' );
$this->logger->log()->debug( '[Lumiere] The following plugins compatible with Lumière! are in use: [' . join( ', ', $this->plugins_active_names ) . ' ]' );

}

/**
* Determine list of plugins active in array
* Build the PluginsDetect class and fill $this->plugins_in_use with the array of plugins in use
* Build list of active plugins and send them in properties
*
* @since 3.7
* @since 4.0.3
*/
public function lumiere_set_plugins_array(): void {

$plugins = new Plugins_Detect();
$this->plugins_in_use = $plugins->plugins_class;
public function activate_plugins(): void {

$plugins = new Plugins_Start();
$this->plugins_active_names = $plugins->plugins_active_names;
$this->plugins_classes_active = $plugins->plugins_classes_active;
}

/**
Expand Down Expand Up @@ -205,9 +214,14 @@ public function lumiere_remove_link ( string $text ): string {
* @return string The URL compatible with Polylang
*/
public function lumiere_url_check_polylang_rewrite ( string $url ): string {

$final_url = null;
$polylang_class = new Polylang();
if ( $polylang_class->polylang_is_active() === true ) {
/* testing if really needed
if ( count($this->plugins_active_names) === 0 ) {
$this->lumiere_set_plugins_array();
}
*/
if ( in_array( 'polylang', $this->plugins_active_names, true ) ) {
$replace_url = str_replace( home_url(), trim( pll_home_url(), '/' ), $url );
$final_url = trim( $replace_url, '/' );
}
Expand Down
Loading

0 comments on commit 6c465b2

Please sign in to comment.