Skip to content

Commit

Permalink
Merge pull request #161 from elightup/fix-woo
Browse files Browse the repository at this point in the history
Fix Divi & WooCommerce issues
  • Loading branch information
rilwis authored Oct 17, 2024
2 parents 85fe3ca + 58c0d20 commit 380d55c
Show file tree
Hide file tree
Showing 12 changed files with 1,829 additions and 1,769 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
},
"dependencies": {
"@elightup/form": "^1.0.6",
"react-paginate": "^8.1.3",
"react-select": "^5.8.0",
"react-tabs": "^3.2.1",
"react-paginate": "^8.2.0",
"react-select": "^5.8.1",
"react-tabs": "^3.2.3",
"slugify": "^1.6.6",
"swr": "^1.3.0"
},
"devDependencies": {
"@wordpress/scripts": "^29.0.0",
"sass": "^1.51.0"
"@wordpress/scripts": "^27.9.0",
"sass": "^1.80.1"
}
}
3,414 changes: 1,734 additions & 1,680 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Integrations/AffiliateWP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function setup() {
public function process() {
// Do not generate meta description from the affiliate area page's content, because it contains forms.
if ( is_page( affwp_get_affiliate_area_page_id() ) ) {
add_filter( 'slim_seo_meta_description_generated', '__return_empty_string' );
add_filter( 'slim_seo_post_content', '__return_empty_string' );
}

add_filter( 'slim_seo_skipped_shortcodes', [ $this, 'skip_shortcodes' ] );
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/Bricks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function is_active(): bool {
}

public function setup(): void {
add_filter( 'slim_seo_meta_description_generated', [ $this, 'description' ], 10, 2 );
add_filter( 'slim_seo_post_content', [ $this, 'filter_content' ], 10, 2 );

add_filter( 'bricks/frontend/disable_opengraph', '__return_true' );
add_filter( 'bricks/frontend/disable_seo', '__return_true' );
Expand All @@ -19,11 +19,11 @@ public function setup(): void {
add_filter( 'slim_seo_taxonomies', [ $this, 'remove_taxonomies' ] );
}

public function description( string $description, WP_Post $post ): string {
return $this->get_post_content( $post ) ?? $description;
public function filter_content( string $post_content, WP_Post $post ): string {
return $this->get_builder_content( $post ) ?? $post_content;
}

private function get_post_content( WP_Post $post ): ?string {
private function get_builder_content( WP_Post $post ): ?string {
// Get from the post first, then from the template.
$data = get_post_meta( $post->ID, BRICKS_DB_PAGE_CONTENT, true );
if ( empty( $data ) ) {
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/Divi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public function is_active(): bool {
public function setup(): void {
add_filter( 'slim_seo_post_types', [ $this, 'remove_post_types' ] );
add_filter( 'slim_seo_taxonomies', [ $this, 'remove_taxonomies' ] );
add_filter( 'slim_seo_meta_description_generated', [ $this, 'description' ], 10, 2 );
add_filter( 'slim_seo_post_content', [ $this, 'filter_content' ], 10, 2 );
}

public function description( string $description, WP_Post $post ): string {
return $description ?? $description;
public function filter_content( string $post_content, WP_Post $post ): string {
return $this->get_builder_content( $post ) ?? $post_content;
}

private function get_post_content( WP_Post $post ): ?string {
private function get_builder_content( WP_Post $post ): ?string {
// If the post is built with Divi, then strips all shortcodes, but keep the content.
if ( get_post_meta( $post->ID, '_et_builder_version', true ) ) {
return preg_replace( '~\[/?[^\]]+?/?\]~s', '', $post->post_content );
Expand Down
20 changes: 6 additions & 14 deletions src/Integrations/LifterLMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@ public function is_active(): bool {
return class_exists( 'LifterLMS' );
}

public function setup() {
public function setup(): void {
add_action( 'template_redirect', [ $this, 'process' ] );
}

public function process() {
add_filter( 'slim_seo_meta_description', [ $this, 'strip_shortcodes' ] );
public function process(): void {
if ( $this->is_skipped_page() ) {
add_filter( 'slim_seo_post_content', '__return_empty_string' );
}
}

/**
* Strip all shortcodes for some LifterLMS pages since they do some logic like setting errors in the session.
* Processing these shortcodes might break LifterLMS (like clearing notices in the session).
*
* @see https://github.com/gocodebox/lifterlms/issues/1181
*/
public function strip_shortcodes( $description ) {
return $this->is_disabled_context() ? strip_shortcodes( $description ) : $description;
}

private function is_disabled_context() {
private function is_skipped_page(): bool {
$pages = [ 'checkout', 'myaccount' ];
$pages = array_map( 'llms_get_page_id', $pages );
return is_page( $pages );
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/Oxygen.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public function is_active(): bool {
}

public function setup(): void {
add_filter( 'slim_seo_meta_description_generated', [ $this, 'description' ], 10, 2 );
add_filter( 'slim_seo_post_content', [ $this, 'filter_content' ], 10, 2 );
add_filter( 'slim_seo_skipped_shortcodes', [ $this, 'skip_shortcodes' ] );
add_filter( 'slim_seo_post_types', [ $this, 'remove_post_types' ] );
}

public function description( string $description, WP_Post $post ): string {
return $this->get_post_content( $post ) ?: $description;
public function filter_content( string $post_content, WP_Post $post ): string {
return $this->get_builder_content( $post ) ?: $post_content;
}

public function get_post_content( WP_Post $post ): string {
public function get_builder_content( WP_Post $post ): string {
// In builder mode.
if ( defined( 'SHOW_CT_BUILDER' ) ) {
return '';
Expand Down
26 changes: 8 additions & 18 deletions src/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public function is_active(): bool {
return class_exists( 'WooCommerce' );
}


public function setup() {
public function setup(): void {
add_action( 'template_redirect', [ $this, 'process' ] );
}

public function process() {
public function process(): void {
add_filter( 'slim_seo_breadcrumbs_args', [ $this, 'change_breadcrumbs_taxonomy' ] );
add_filter( 'slim_seo_meta_description', [ $this, 'strip_shortcodes' ] );

if ( $this->is_skipped_page() ) {
add_filter( 'slim_seo_post_content', '__return_empty_string' );
}

if ( is_singular( 'product' ) ) {
$this->add_pinterest_pins();
Expand All @@ -34,20 +36,8 @@ public function change_breadcrumbs_taxonomy( array $args ): array {
return $args;
}

/**
* Strip all shortcodes for WooCommerce pages since they do some logic like setting errors in the session.
* Processing these shortcodes might break WooCommerce (like clearing notices in the session).
*
* @see https://wordpress.org/support/topic/woocommerce-notices-are-not-showing-after-activating-your-plugin/
*/
public function strip_shortcodes( $description ) {
return $this->no_shortcodes_page() ? strip_shortcodes( $description ) : $description;
}

private function no_shortcodes_page() {
$pages = [ 'cart', 'checkout', 'myaccount' ];
$pages = array_map( 'wc_get_page_id', $pages );
return is_page( $pages );
private function is_skipped_page(): bool {
return is_cart() || is_checkout() || is_account_page();
}

private function add_pinterest_pins() {
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/ZionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public function is_active(): bool {

public function setup(): void {
add_filter( 'slim_seo_post_types', [ $this, 'remove_post_types' ] );
add_filter( 'slim_seo_meta_description_generated', [ $this, 'description' ], 10, 2 );
add_filter( 'slim_seo_post_content', [ $this, 'filter_content' ], 10, 2 );
}

public function description( string $description, WP_Post $post ): string {
return $this->get_post_content( $post ) ?? $description;
public function filter_content( string $post_content, WP_Post $post ): string {
return $this->get_builder_content( $post ) ?? $post_content;
}

private function get_post_content( WP_Post $post ): ?string {
private function get_builder_content( WP_Post $post ): ?string {
$post_instance = \ZionBuilder\Plugin::$instance->post_manager->get_post_instance( $post->ID );

if ( ! $post_instance || $post_instance->is_password_protected() || ! $post_instance->is_built_with_zion() ) {
Expand Down
2 changes: 1 addition & 1 deletion src/MetaTags/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private function get_post_data(): array {
if ( empty( $post ) ) {
return [];
}
$post_content = apply_filters( 'slim_seo_meta_description_generated', $post->post_content, $post );
$post_content = apply_filters( 'slim_seo_post_content', $post->post_content, $post );

$post_tax = [];
$taxonomies = Helper::get_taxonomies();
Expand Down
90 changes: 57 additions & 33 deletions src/MetaTags/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,60 @@

use SlimTwig\Renderer;
use SlimSEO\Helpers\Arr;
use WP_Block_Type_Registry;

class Helper {
private static $allowed_shortcodes = [];
private static $allowed_blocks = [];

private static $ran = false;

public static function normalize( $text ): string {
global $shortcode_tags;

// Get list of allowed shortcodes and blocks only once.
if ( ! self::$ran ) {
self::set_allowed_shortcodes();
self::set_allowed_blocks();
}

// Parse shortcodes. Works with posts that have shortcodes in the content (using page builders like Divi).
$shortcodes_bak = $shortcode_tags;
$shortcode_tags = self::$allowed_shortcodes;
$text = do_shortcode( $text );
$shortcode_tags = $shortcodes_bak; // Revert the global shortcodes registry.
$text = strip_shortcodes( $text ); // Strip all non-parsed shortcodes.

// Render blocks.
add_filter( 'pre_render_block', [ __CLASS__, 'maybe_skip_block' ], 10, 2 );
$text = do_blocks( $text );
remove_filter( 'pre_render_block', [ __CLASS__, 'maybe_skip_block' ] );

// Replace HTML tags with spaces.
$text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
$text = preg_replace( '@<[^>]*?>@s', ' ', $text );

// Remove lonely separator
$separator = apply_filters( 'document_title_separator', '-' ); // phpcs:ignore
$text = trim( $text );
$text = trim( $text, $separator );

// Remove extra white spaces.
$text = preg_replace( '/\s+/', ' ', $text );
$text = trim( $text );

self::$ran = true;

return $text;
}

public static function maybe_skip_block( ?string $pre_render, array $block ): ?string {
return empty( $block['blockName'] ) || in_array( $block['blockName'], self::$allowed_blocks, true ) ? $pre_render : '';
}

private static function set_allowed_shortcodes(): void {
global $shortcode_tags;

/**
* Some shortcodes (like HappyForms) inject <link> or other assets to the page.
* do_shortcode here will parse the <link> and then remove it, which might break the style/JS.
Expand All @@ -31,40 +80,13 @@ public static function normalize( $text ): string {
'fe_sort',
] );

$shortcodes_bak = $shortcode_tags;

// @codingStandardsIgnoreLine.
$shortcode_tags = array_diff_key( $shortcode_tags, array_flip( $skipped_shortcodes ) );
$text = do_shortcode( $text ); // Parse shortcodes. Works with posts that have shortcodes in the content (using page builders like Divi).

// @codingStandardsIgnoreLine.
$shortcode_tags = $shortcodes_bak; // Revert the global shortcodes registry.
$text = strip_shortcodes( $text ); // Strip all non-parsed shortcodes.

// Render blocks.
if ( function_exists( 'do_blocks' ) ) {
add_filter( 'pre_render_block', [ __CLASS__, 'maybe_skip_block' ], 10, 2 );
$text = do_blocks( $text );
remove_filter( 'pre_render_block', [ __CLASS__, 'maybe_skip_block' ] );
}

// Replace HTML tags with spaces.
$text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
$text = preg_replace( '@<[^>]*?>@s', ' ', $text );

// Remove lonely separator
$separator = apply_filters( 'document_title_separator', '-' ); // phpcs:ignore
$text = trim( $text );
$text = trim( $text, $separator );

// Remove extra white spaces.
$text = preg_replace( '/\s+/', ' ', $text );
$text = trim( $text );

return $text;
self::$allowed_shortcodes = array_diff_key( $shortcode_tags, array_flip( $skipped_shortcodes ) );
self::$allowed_shortcodes = apply_filters( 'slim_seo_allowed_shortcodes', self::$allowed_shortcodes );
}

public static function maybe_skip_block( ?string $output, array $block ): ?string {
private static function set_allowed_blocks(): void {
$block_types = array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() );

$skipped_blocks = apply_filters( 'slim_seo_skipped_blocks', [
'core/query',
'core/code',
Expand All @@ -73,7 +95,9 @@ public static function maybe_skip_block( ?string $output, array $block ): ?strin
'ninja-forms/form',
'mailpoet/subscription-form-block',
] );
return in_array( $block['blockName'], $skipped_blocks, true ) ? '' : $output;

self::$allowed_blocks = array_diff( $block_types, $skipped_blocks );
self::$allowed_blocks = apply_filters( 'slim_seo_allowed_blocks', self::$allowed_blocks );
}

public static function get_taxonomies() {
Expand Down
2 changes: 1 addition & 1 deletion src/MetaTags/Settings/Preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function render_post_description( WP_REST_Request $request ): array {
$text = (string) $request->get_param( 'text' ); // Manual entered meta description
$excerpt = (string) $request->get_param( 'excerpt' ); // Live excerpt
$content = (string) $request->get_param( 'content' ); // Live content
$content = apply_filters( 'slim_seo_meta_description_generated', $content, get_post( $id ) );
$content = apply_filters( 'slim_seo_post_content', $content, get_post( $id ) );

$data = [];
$data['post'] = array_filter( [
Expand Down

0 comments on commit 380d55c

Please sign in to comment.