Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Increase compatibility with Elementor #59

Open
ur-krostitzer opened this issue May 14, 2024 · 6 comments
Open

Feature Request: Increase compatibility with Elementor #59

ur-krostitzer opened this issue May 14, 2024 · 6 comments

Comments

@ur-krostitzer
Copy link

Hi there,

We are currently testing your Plugin's free version and I have to say - We are impressed!
We consider buying the premium lifetime license but there is one requirement missing in the current state of the plugin - compatibility with the Elementor Pro Posts Widget.

More specifically the custom excerpt feature is not compatible with the excerpt displayed in the Posts widget. If I understand correctly you are storing the excerpt in the main queries WP_Query object in the excerpt / post_excerpt attribute which should be output upon invoking the_excerpt. Elementor does it the recommended way though still it does not work.

Maybe I am missing something and I am unsure if this is the right place for such an issue as it may possibly be caused by Elementor (I have opened an issue on the Elementor Github as well https://github.com/orgs/elementor/discussions/27316).

If any further information is required on the topic I am happy to oblige so feel free to discuss this matter at any time.

Thanks for your time.

@msaari
Copy link
Owner

msaari commented May 14, 2024

You're not the first one to ask for this, and I've once been through the process of trying to figure out how this works. We couldn't figure it out then, even with Elementor support. Considering how unpleasant dealing with the Elementor support was, I'm not terribly thrilled to go down that path again. I don't have Elementor Pro and even if I had, I have no idea where in their code to start digging for this. And even if I could find out how to fix this, there's no guarantee Elementor will implement that. So, unfortunately, this seems like a lose-lose case for me.

If Elementor can't find the excerpt from $post->post_excerpt – i.e., it uses a different instance of the post object than the one with the Relevanssi changes — I wonder if you could use a filter hook to generate the excerpts for the posts on the fly.

@ur-krostitzer
Copy link
Author

Thanks for the swift response!

I stumbled upon similar requests but they were a couple years old so I figured asking again won't hurt. I can imagine that working with Elementor Support is not really pleasant at times. Personally I dug through a significant amount of the Elementor and Elementor Pro Source Code. Still I cannot precisely think of a solution for this.

Elementor has its' own implementation of retrieving the post excerpt which you can find in /modules/dynamic-tags/tags/post-excerpt.php which looks like this:

	public function should_get_excerpt_from_post_content( $settings ) {
		return 'yes' === $settings['apply_to_post_content'];
	}

	public function is_post_excerpt_valid( $settings, $post ) {
		if ( ! $post ) {
			return false;
		}

		if ( empty( $post->post_excerpt ) && ! $this->should_get_excerpt_from_post_content( $settings ) ) {
			return false;
		}

		if ( empty( $post->post_excerpt ) && empty( $post->post_content ) && $this->should_get_excerpt_from_post_content( $settings ) ) {
			return false;
		}

		if ( empty( $post->post_excerpt ) && empty( $post->post_content ) ) {
			return false;
		}

		return true;
	}

	public function get_post_excerpt( $settings, $post ) {
		$post_excerpt = $post->post_excerpt ?? '';

		if ( empty( $post_excerpt ) && ! empty( $post->post_content ) && $this->should_get_excerpt_from_post_content( $settings ) ) {
			$post_excerpt = apply_filters( 'the_excerpt', get_the_excerpt( $post ) );
		}

		return $post_excerpt;
	}

The posts widget's excerpt rendering comes from /modules/posts/skins/skin-base.php and looks like this:

	protected function render_excerpt() {
		add_filter( 'excerpt_more', [ $this, 'filter_excerpt_more' ], 20 );
		add_filter( 'excerpt_length', [ $this, 'filter_excerpt_length' ], 20 );

		if ( ! $this->get_instance_value( 'show_excerpt' ) ) {
			return;
		}

		add_filter( 'excerpt_more', [ $this, 'filter_excerpt_more' ], 20 );
		add_filter( 'excerpt_length', [ $this, 'filter_excerpt_length' ], 20 );

		?>
		<div class="elementor-post__excerpt">
			<?php
			global $post;
			$apply_to_custom_excerpt = $this->get_instance_value( 'apply_to_custom_excerpt' );

			// Force the manually-generated Excerpt length as well if the user chose to enable 'apply_to_custom_excerpt'.
			if ( 'yes' === $apply_to_custom_excerpt && ! empty( $post->post_excerpt ) ) {
				$max_length = (int) $this->get_instance_value( 'excerpt_length' );
				$excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );
				$excerpt = ProUtils::trim_words( $excerpt, $max_length );
				echo wp_kses_post( $excerpt );
			} else {
				the_excerpt();
			}
			?>
		</div>
		<?php

		remove_filter( 'excerpt_length', [ $this, 'filter_excerpt_length' ], 20 );
		remove_filter( 'excerpt_more', [ $this, 'filter_excerpt_more' ], 20 );
	}

I have thought of some hook to trigger the Relevanssi excerpt generation right before rendering it but I suspect that the Elementor Widget uses another Query object than the main or is_search Query Object. Haven't tested though.

Looking at that maybe you can think of something that might work?

@msaari
Copy link
Owner

msaari commented May 15, 2024

It doesn't matter which query object is used. Relevanssi can generate the excerpts if you have the search term (which you can get from $_REQUEST['s'] if nothing else works) and the post object.

If you look at the $post parameter in get_post_excerpt(), is it the one from Relevanssi? The easiest way to check is to see if $post->relevance_score is set or not.

@ur-krostitzer
Copy link
Author

Interestingly, if I enable my Elementor search template, the post_excerpt is the default WP post_excerpt. If I disable it then the Relevanssi post excerpt is assigned to the key.

@msaari
Copy link
Owner

msaari commented May 16, 2024

That makes sense. When the Elementor search template is enabled, Elementor doesn't use the post object Relevanssi has prepared but instead loads the post object from wp_posts. Where does that happen in Elementor, I have no idea based on the code I've seen here.

@ur-krostitzer
Copy link
Author

I'm going to do some research and test some stuff and will be reporting back to you with what I find in a few days. In the meantime I have found another small issue in the Plugin which I will open another issue on today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants