Custom Extension issue

Hello!

I’m creating a custom plugin for HivePress, and everything is set up, but I’m encountering an error.

Controler:

<?php
/**
 * Listing controller.
 *
 * @package HivePress\Controllers
 */
namespace HivePress\Controllers;

use HivePress\Helpers as hp;
use HivePress\Models;
use HivePress\Forms;
use HivePress\Blocks;
use HivePress\Emails;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Controller class.
 */
final class Controller_Zakladki extends Controller {

	/**
	 * Class constructor.
	 *
	 * @param array $args Controller arguments.
	 */
	public function __construct( $args = [] ) {
		$args = hp\merge_arrays(
			[
				'routes' => [
					'listing_view_doswiadczenie' => [
                        'title'     => esc_html__( 'doswiadczenie', 'doswiadczenie' ),
                        'base'      => 'listing_view_page',
                        'path'      => '/doswiadczenie',
						//Tried Both
						'redirect' => [$this, 'redirect_doswiadczenie_page'],
                     /*   'redirect' => [
							[
								'callback' => [ $this, 'redirect_doswiadczenie_page' ],
								'_order'   => 1,
							],
						],
                        'action'    => [ $this, 'render_doswiadczenie_page' ],
                        'paginated' => true,*/
                    ],
				],
			],
			$args
		);

		parent::__construct( $args );
	}
	
	/**
	 * Redirects listing view page.
	 *
	 * @return mixed
	 */
public function redirect_doswiadczenie_page() {
	the_post();

	$listing = Models\Listing::query()->get_by_id( get_post() );

	$listing->get_images__id();

	// Get vendor.
	$vendor = $listing->get_vendor();

	// Set request context.
	hivepress()->request->set_context( 'listing', $listing );
	hivepress()->request->set_context( 'vendor', $vendor );

	return false;

    }

/**
 * Renders listing feed page.
 *
 * @return string
 */
public function render_doswiadczenie_page() {
	

	
    return ( new Blocks\Template(
        [
            'template' => 'listing_view_doswiadczenie',

            'context'  => [
                'listing' => hivepress()->request->get_context( 'listing' ),
            	'vendor'  => hivepress()->request->get_context( 'vendor' ),
            ],
        ]
    ) )->render();
}
}

The file listing_view_doswiadczenie is a duplicate of listing_view_page with a few changes.

Unfortunately, when trying to access the listing_view_doswiadczenie route, an error occurs:

PHP Fatal error: Uncaught Error: Call to a member function get_images_id() on null in /home/tpmacxnjjm/domains/wyszukiwarka.legal/public_html/wp-content/plugins/hivepress_zakladki_listing/includes/controllers/class-controller-zakladki.php:63\nStack trace:\n#0

I feel like I might have overlooked something small. How can I fetch the current listing before redirect_doswiadczenie_page is triggered?

For future developers, i handled this issue, ussing cookies.

In functions.php, i set cookie, with current listing ID

add_action(
	'hivepress/v1/templates/listing_view_page/blocks',
	function ($blocks, $template){
		$listing = $template->get_context('listing');
		
		$listing->get_id();
		$vendor = $listing->get_vendor();

		$listing = $listing -> get_id();
		$vendor = $vendor -> get_id();

		setcookie('listing_zakladki_page',  (string)$listing, time() + 3600, "/");
		setcookie('vendor_zakladki_page', (string)$vendor, time() + 3600, "/");

		return $blocks;
	},
	1000,
	2
);

Controller reads cookie, and renders new template, using render_ function

public function redirect_doswiadczenie_page() {
	$listingID = $_COOKIE['listing_zakladki_page'];
	
	$listing = Models\Listing::query()->get_by_id((int) $listingID);

	$vendor = $listing ->get_vendor();
	hivepress()->request->set_context( 'listing', $listing );
	hivepress()->request->set_context( 'vendor', $vendor );

	return false;
	}
public function render_doswiadczenie_page() {
	

    return ( new Blocks\Template(
        [
            'template' => 'listing_view_doswiadczenie',

            'context'  => [
				'listing' => hivepress()->request->get_context( 'listing' ),
				'vendor'  => hivepress()->request->get_context( 'vendor' ),
			
            ],
        ]
    ) )->render();
}

GL!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.