mesh icon indicating copy to clipboard operation
mesh copied to clipboard

Expected filters/actions?

Open strarsis opened this issue 7 years ago • 10 comments
trafficstars

Does the Mesh plugin expect specific filters/actions for rendering the markup? In a sage9 based one pager theme, for each page the blade render method is called, but Mesh plugin is not adding the markup. Must some specific filters/actions be called explicitly?

strarsis avatar Jun 11 '18 18:06 strarsis

By default Mesh will hook into "loop_end" (which is a default hook within pages). However this may not work if your home page is ONLY displaying the "blog" within your Website General Settings vs defining a "Front page"

Below is an example of what happens within the hook for loop_end

if ( ! $wp_query->is_main_query() ) {
			return;
		}

		if ( ! is_singular() ) {
			return;
		}

		// Do not show content on password protected posts.
		if ( post_password_required() ) {
			return;
		}

		$sections = mesh_display_sections( $wp_query->post->ID, false );

		echo apply_filters( 'mesh_loop_end', $sections, $wp_query->post->ID ); // WPCS: XSS ok, sanitization ok.

We also apply out own mesh_loop_end hook that you can use to filter further. If your theme doesn't utilize the "The Loop" You could also use mesh_display_sections( $yourPostId, true );

aaronware avatar Jun 11 '18 19:06 aaronware

@aaronware:

do_action('loop_end');
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function is_main_query() on string in /srv/www/web/app/plugins/mesh/class.mesh.php on line 911

Passing $post results in same error.

strarsis avatar Jun 12 '18 22:06 strarsis

Did you make a change to Mesh and received that error?

aaronware avatar Jun 12 '18 22:06 aaronware

like you wouldn't call do_action('loop_end'); directly. That do)action('loop_end') is called by WordPress core when any loop finishes. The check in mesh is to make sure it only tried to add mesh sections after loop_end of the main query.

aaronware avatar Jun 12 '18 22:06 aaronware

No changes had been made to Mesh plugin, I removed its plugin folder and reinstalled it to be sure.

Can I trigger the loop_end action manually (each time a new page is rendered) so Mesh knows that it should add the sections?

strarsis avatar Jun 12 '18 22:06 strarsis

I haven't ever built anything using Sage9 before. Are you using the laravel blade templates or the defaults? I'm not sure why it wouldn't find the_loop or have access to general hooks from WordPress.

aaronware avatar Jun 12 '18 22:06 aaronware

@aaronware: It works with a single page with sage9 (laravel blade is used internally, yes). But when I want to loop over many pages (one pager with multiple pages on front page), I have to notify Mesh plugin that it now has to render the sections for each page.

This code is used for adding one pager support to a sage9 theme: https://github.com/strarsis/sage9-onepager-lib/blob/master/Controls.php#L127

I can use mesh_display_sections( $post->ID, false ); for rendering the mesh sections, but this will also change the global $post variable to current page, interfering with the original $post.

strarsis avatar Jun 12 '18 22:06 strarsis

Ahh ok. Can you send me an example of the code you are using? Or is that something you can just do from the admin of the theme once you generate it?

aaronware avatar Jun 12 '18 22:06 aaronware

@aaronware: Sure! https://github.com/strarsis/sage9-onepager-lib/blob/mesh-templ/Controls.php#L127 The end variable is used for inserting the mesh sections correctly into a page section, otherwise they would be inserted below the page container element. The issue with the code is that calling mesh_display_sections also seem to change $post from the page to be looped to front page each time. Because of this the content of front page is shown and then the Mesh sections.

strarsis avatar Jun 12 '18 22:06 strarsis

@aaronware: As a workaround it is possible to reset the $post to previous state by retrieving it again:

			$post = get_post( get_theme_mod( 'panel_' . $id ) );
			setup_postdata( $post );
			set_query_var( 'panel', $id );


			$template_data = array(
				'end' => mesh_display_sections( $post->ID, false )
			);

			// reset $post
			$post = get_post( get_theme_mod( 'panel_' . $id ) );
			setup_postdata( $post );
			set_query_var( 'panel', $id );

			echo \App\template(  'single-panels', $template_data  );


			wp_reset_postdata();

However, this is a workaround, ideally the $post is not changed or a different way is found for notifying Mesh plugin – and possibly other plugins that use these hooks for rendering.

strarsis avatar Jun 13 '18 11:06 strarsis