| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Loader. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! class_exists( 'Cartflows_Nps_Survey' ) ) : |
| 13 |
|
| 14 |
/** |
| 15 |
* Cartflows Nps Survey Loader |
| 16 |
* |
| 17 |
* @since 2.1.3 |
| 18 |
*/ |
| 19 |
class Cartflows_Nps_Survey { |
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @since 2.1.3 |
| 24 |
* @var (Object) Cartflows_Nps_Survey |
| 25 |
*/ |
| 26 |
private static $instance = null; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get Instance |
| 30 |
* |
| 31 |
* @since 2.1.3 |
| 32 |
* |
| 33 |
* @return object Class object. |
| 34 |
*/ |
| 35 |
public static function get_instance() { |
| 36 |
if ( ! isset( self::$instance ) ) { |
| 37 |
self::$instance = new self(); |
| 38 |
} |
| 39 |
|
| 40 |
return self::$instance; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @since 2.1.3 |
| 47 |
*/ |
| 48 |
private function __construct() { |
| 49 |
$this->version_check(); |
| 50 |
add_action( 'init', array( $this, 'load' ), 999 ); |
| 51 |
add_filter( 'nps_survey_api_endpoint', array( $this, 'update_webhook_url' ), 10, 2 ); |
| 52 |
add_filter( 'nps_survey_post_data', array( $this, 'mark_nps_dismissed_on_submit' ), 10, 1 ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Version Check |
| 57 |
* |
| 58 |
* @since 2.1.3 |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function version_check() { |
| 62 |
|
| 63 |
$file = realpath( dirname( __FILE__ ) . '/nps-survey/version.json' ); |
| 64 |
|
| 65 |
// Is file exist? |
| 66 |
if ( is_file( $file ) ) { |
| 67 |
|
| 68 |
$file_data = json_decode( file_get_contents( $file ), true ); |
| 69 |
|
| 70 |
global $nps_survey_version, $nps_survey_init; |
| 71 |
|
| 72 |
$path = realpath( dirname( __FILE__ ) . '/nps-survey/nps-survey.php' ); |
| 73 |
|
| 74 |
$version = isset( $file_data['nps-survey'] ) ? $file_data['nps-survey'] : 0; |
| 75 |
|
| 76 |
if ( null === $nps_survey_version ) { |
| 77 |
$nps_survey_version = '1.0.0'; |
| 78 |
} |
| 79 |
|
| 80 |
// Compare versions. |
| 81 |
if ( version_compare( $version, $nps_survey_version, '>=' ) ) { |
| 82 |
$nps_survey_version = $version; |
| 83 |
$nps_survey_init = $path; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Load latest plugin |
| 90 |
* |
| 91 |
* @since 2.1.3 |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function load() { |
| 95 |
|
| 96 |
global $nps_survey_version, $nps_survey_init; |
| 97 |
if ( is_file( realpath( $nps_survey_init ) ) ) { |
| 98 |
include_once realpath( $nps_survey_init ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update the NPS survey webhook URL. |
| 104 |
* |
| 105 |
* @param string $url Webhook URL. |
| 106 |
* @param array<mixed> $post_data NPS survey post data. |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function update_webhook_url( $url, $post_data ) { |
| 111 |
if ( isset( $post_data['plugin_slug'] ) && 'cartflows' === $post_data['plugin_slug'] ) { |
| 112 |
// Return the NPS webhook URL if the plugin slug is CartFlows. |
| 113 |
return CARTFLOWS_NPS_WEBHOOK_URL; |
| 114 |
} |
| 115 |
|
| 116 |
return $url; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Lock the NPS dismissal state as soon as a submission is initiated. |
| 121 |
* The bundled library only persists `dismiss_permanently` on an HTTP 200 |
| 122 |
* response from the webhook. Ottokit can return 202/204, leaving the |
| 123 |
* notice re-displayable and causing the same user to resubmit repeatedly. |
| 124 |
* |
| 125 |
* @param array<mixed> $post_data NPS survey post data. |
| 126 |
* @return array<mixed> |
| 127 |
*/ |
| 128 |
public function mark_nps_dismissed_on_submit( $post_data ) { |
| 129 |
if ( ! isset( $post_data['plugin_slug'] ) || 'cartflows' !== $post_data['plugin_slug'] ) { |
| 130 |
return $post_data; |
| 131 |
} |
| 132 |
|
| 133 |
update_option( |
| 134 |
'nps-survey-cartflows', |
| 135 |
array( |
| 136 |
'dismiss_count' => 0, |
| 137 |
'dismiss_permanently' => true, |
| 138 |
'dismiss_step' => 'submitted', |
| 139 |
), |
| 140 |
false |
| 141 |
); |
| 142 |
|
| 143 |
return $post_data; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Kicking this off by calling 'get_instance()' method |
| 150 |
*/ |
| 151 |
Cartflows_Nps_Survey::get_instance(); |
| 152 |
|
| 153 |
endif; |
| 154 |
|