NpsSurveyServiceProvider.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Integrations\NpsSurvey; |
| 4 | |
| 5 | use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 6 | |
| 7 | /** |
| 8 | * Provides the NPS Survey service provider. |
| 9 | */ |
| 10 | class NpsSurveyServiceProvider implements ServiceProviderInterface { |
| 11 | /** |
| 12 | * Whether the NPS survey version check has already been performed. |
| 13 | * |
| 14 | * @var bool |
| 15 | */ |
| 16 | private static bool $version_checked = false; |
| 17 | |
| 18 | /** |
| 19 | * Register all dependencies in the IoC container. |
| 20 | * |
| 21 | * @param \Pimple\Container $container Service container. |
| 22 | * @return void |
| 23 | */ |
| 24 | public function register( $container ) { |
| 25 | $container['surecart.nps.survey.notice'] = function () { |
| 26 | return new NpsSurveyNotice(); |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * {@inheritDoc} |
| 32 | * |
| 33 | * @param \Pimple\Container $container Service Container. |
| 34 | */ |
| 35 | public function bootstrap( $container ) { |
| 36 | if ( ! $container['surecart.account']->is_connected ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $this->versionCheck(); |
| 41 | |
| 42 | if ( did_action( 'init' ) ) { |
| 43 | $this->loadNpsSurveyLibrary(); |
| 44 | } else { |
| 45 | add_action( 'init', [ $this, 'loadNpsSurveyLibrary' ], 999 ); |
| 46 | } |
| 47 | |
| 48 | // Load the NPS survey notice on all admin pages, but it will only display on SureCart admin pages. |
| 49 | \SureCart::resolve( 'surecart.nps.survey.notice' )->bootstrap(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register SureCart's NPS survey version in the global version auction. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | private function versionCheck(): void { |
| 58 | if ( self::$version_checked ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $nps_lib_path = SURECART_VENDOR_DIR . '/brainstormforce/nps-survey'; |
| 63 | |
| 64 | $file = realpath( $nps_lib_path . '/version.json' ); |
| 65 | if ( ! $file || ! is_file( $file ) ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $contents = file_get_contents( $file ); |
| 70 | if ( false === $contents ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $file_data = json_decode( $contents, true ); |
| 75 | |
| 76 | global $nps_survey_version, $nps_survey_init; |
| 77 | |
| 78 | $path = realpath( $nps_lib_path . '/nps-survey.php' ); |
| 79 | $version = $file_data['nps-survey'] ?? 0; |
| 80 | |
| 81 | if ( null === $nps_survey_version ) { |
| 82 | $nps_survey_version = '0.0.0'; |
| 83 | } |
| 84 | |
| 85 | if ( version_compare( $version, $nps_survey_version, '>=' ) ) { |
| 86 | $nps_survey_version = $version; |
| 87 | $nps_survey_init = $path; |
| 88 | } |
| 89 | |
| 90 | self::$version_checked = true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Load the winning NPS survey library file. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | public function loadNpsSurveyLibrary(): void { |
| 99 | global $nps_survey_init; |
| 100 | |
| 101 | if ( $nps_survey_init && is_file( $nps_survey_init ) ) { |
| 102 | include_once $nps_survey_init; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 |