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

Set PHP default timezone during processors processing #179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions includes/class-civicrm-caldera-forms-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class CiviCRM_Caldera_Forms_Forms {
*/
protected $transient_id;

/**
* @since 1.0.5
* @access protected
* @var string the timezone string before changing
*/
protected $original_timezone;

/**
* Initialises this object.
*
Expand Down Expand Up @@ -59,6 +66,10 @@ public function register_hooks() {
add_filter( 'caldera_forms_submit_get_form', [ $this, 'set_form_transient' ] );
add_action( 'caldera_forms_submit_complete', [ $this, 'delete_form_transient' ] );

// set PHP default timezone for CiviCRM and reset for WordPress after processors done
add_action( 'caldera_forms_submit_start_processors', [ $this, 'set_timezone_wrapper' ], 10, 3 );
add_action( 'caldera_forms_submit_complete', [ $this, 'reset_timezone_wrapper' ], 10, 4 );

// add CiviCRM panel
if ( in_array( 'CiviContribute', $this->plugin->processors->enabled_components ) )
add_filter( 'caldera_forms_get_panel_extensions', [ $this, 'add_civicrm_tab' ], 10 );
Expand Down Expand Up @@ -109,6 +120,41 @@ public function delete_form_transient() {
return $this->plugin->transient->delete();
}

/**
* Set timezone to WP timezone
*
* @access public
* @since 1.0.5
*/
public function set_timezone_wrapper($form, $referrer, $process_id) {
Civi::Dispatcher()->addListener(\Civi\API\Events::PREPARE, [ $this, 'wrap_timezone' ], -100);
}

/**
* Reset timezone
*
* @access public
* @since 1.0.5
*/
public function reset_timezone_wrapper($form, $referrer, $process_id, $entryid) {
Civi::Dispatcher()->removeListener(\Civi\API\Events::PREPARE, [ $this, 'wrap_timezone' ]);
}

public function wrap_timezone($event) {
$event->wrapApi( [ $this, 'api_timezone_wrapper' ] );
}

public function api_timezone_wrapper($apiRequest, $callsame) {
$original_timezone = date_default_timezone_get();
date_default_timezone_set( wp_timezone_string() );

$result = $callsame($apiRequest);

date_default_timezone_set( $original_timezone );

return $result;
}

/**
* Transient structure.
*
Expand Down