Skip Navigation

[Resolved] cannot use php class in View shortcode

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 1 year, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2512703
Screenshot 2022-12-10 at 1.26.27 PM.png
Screenshot 2022-12-10 at 1.24.26 PM.png

Dear Sir/Madam,

I have a PHP class to manage the membership details, I have a View to display the membership details, I can get all members' custom fields into the View, but there is error if I call the PHP class in a shortcode. I need this class to calculate the status of the membership. Please advise it is true I can use PHP class in shortcode? In my class, I did use wp_reset_postdata() to reset the current $post variable.

Below please find the code of my class, shortcode and view.

PHP class

class WPMembership {

	public function __construct($user_id) {
		global $post;
		$args = array(
			'numberposts' => 1,
			'post_type'  => 'member',
			'p'     => $user_id
		);
		$posts = get_posts($args);
		$post = $posts[0];
		setup_postdata( $post );
        
		$this->termination_date = types_render_field('termination-date', array('output' => 'raw'));

		..... (omitted)		

		wp_reset_postdata();
	}
	public function membershipStatus() {
		$todayVal = strtotime(date('Y-m-d'));
		if (($this->termination_date > 0) && ($this->termination_date < $todayVal)) {
		    return 'invalid';
		}
		if ($this->membershipEndDateVal >= $todayVal) {
			return 'effective';
		} else {
			return 'finished';
		}
	}	

Function to be called from View as shortcode

function ebz_get_membership_status( $atts ) {
		$n = new WPMembership($atts['pid']);
		$rtn = strtolower($n->membershipStatus());
		return $rtn;
}
add_shortcode('get_membership_status', 'ebz_get_membership_status');

Content in Loop Editor

        <wpv-loop>
            <tr><td align="center">[wpv-post-id]</td><td align="center">[wpv-post-title] ([wpv-post-author])</td><td align="center">[types field='contact'][/types]</td><td align="center">[types field='member-register-date' style='text' format='d-M-Y'][/types]</td><td align="center">[types field='uprise-first-bill-date' style='text' format='d-M-Y'][/types]</td><td align="center">[types field='uprise-next-bill-date' style='text' format='d-M-Y'][/types]</td><td align="center">[types field='subscription-period'][/types]</td><td align="center"><i class="fa fa-circle [get_membership_status pid='[wpv-post-id]']" aria-hidden="true"></i></td></tr>
        </wpv-loop> 

I also attached two result from View, the one without all date is the shortcode called the class.

#2513739

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know that do you see any error? Can you please activate the debug log and check what error do you see?

More info:
- https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#php-debugging

#2514707
Screenshot 2022-12-13 at 11.15.07 PM.png

Dear Minesh,

I got below error from debug.log

[13-Dec-2022 14:56:15 UTC] PHP Warning:  strtotime() expects parameter 2 to be int, string given in /home/u249343135/domains/alivefitness.hk/public_html/staging/wp-content/plugins/uprisepay-payment/includes/class-wp-membership.php on line 59
[13-Dec-2022 14:56:15 UTC] PHP Warning:  strtotime() expects parameter 2 to be int, string given in /home/u249343135/domains/alivefitness.hk/public_html/staging/wp-content/plugins/uprisepay-payment/includes/class-wp-membership.php on line 66

It is related to the custom date field, there is a custom field uprise-first-bill-date and uprise-next-bill-date with date format but without default value, I need to calculate the uprise-next-bill-date according to the uprise-first-bill-date but if customer has not subscribed my service, the uprise-first-bill-date is empty because not yet assigned the date to this field, below is the code I calculate the uprise-next-bill-date, some lines are omitted.

		$this->first_bill_date = types_render_field('uprise-first-bill-date', array('output' => 'raw'));
		$this->next_bill_date = types_render_field('uprise-next-bill-date', array('output' => 'raw'));
		$this->membershipStartDateVal = $this->first_bill_date;
		$this->firstBillDateVal = $this->first_bill_date;
		
		$firstBillDateVal = $this->first_bill_date;
		$nextBillDateVal = $this->next_bill_date;
		$this->membershipEndDateVal = strtotime('1 year - 1 day', $this->membershipStartDateVal);        <----  line 59 
		
		if ( $todayVal < $firstBillDateVal) {
			$this->nextBillDateVal = $firstBillDateVal;
		} elseif ($todayVal == $firstBillDateVal) {
		    $this->nextBillDateVal = strtotime('+1 month -1 day', $firstBillDateVal);
		} elseif ($todayVal > $firstBillDateVal) {
			$nextBillDay = strtotime('-1 day', $firstBillDateVal);                                            <---- line 66
			$text = sprintf('Y-m-%s', intval(date('d', $nextBillDay)));
			$nextBillDateVal = strtotime(date($text, strtotime(date('Y-m-d'))));
			$this->nextBillDateVal = $nextBillDateVal;
			if ($nextBillDateVal < $todayVal) {
				$this->nextBillDateVal = strtotime('+1 month', $nextBillDateVal);	
			} else {
				$this->nextBillDateVal = $nextBillDateVal;
			}
		}

Attached is the result of the output, please advise how I can manage the date calculate if date field has no value?

#2515487

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - errors you shared is actually notices and that does not effect the output.

If any of your field may have empty value, what if you wrap that code with if condition and make sure that it should run only when those date fields have values.

This is a pure custom code which is beyond the scope of our support policy. If you have something not working as expected with Toolset API or function please let us know and we are happy to help you further.

#2515895

Dear Minesh,

Yes I know but I need to know that what is the return if the date field has not value, is it null, '' or others then I can manage. What is the data type to this empty date field or I should default it as 1970-01-01 to avoid any error?

#2515931
Screenshot 2022-12-15 at 1.31.32 PM.png
Screenshot 2022-12-15 at 1.31.21 PM.png

Dear Minesh,

I did try to change the bill date as 1970-01-01 but it doesn't save the record, it show empty, or it is saved as zero and display empty if zero?

#2515949

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should not set any date. If there date field is not set it will return you empty.

So, you should check with if condition about !empty() or you can use:

if($date_field_value != '') {