independent-analytics
Last commit date
IAWP
3 months ago
dist
3 months ago
freemius
3 months ago
img
6 months ago
javascript-source
3 months ago
languages
3 months ago
vendor
3 months ago
views
3 months ago
iawp-bootstrap.php
3 months ago
iawp-click-endpoint.php
1 year ago
iawp-complianz.php
3 months ago
iawp.php
3 months ago
readme.txt
3 months ago
iawp-bootstrap.php
395 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED; |
| 4 | |
| 5 | use IAWP\ActivationLifecycle; |
| 6 | use IAWP\Click_Tracking\Click_Processing_Job; |
| 7 | use IAWP\Click_Tracking\Config_File_Manager; |
| 8 | use IAWP\ComplianzIntegration; |
| 9 | use IAWP\Custom_WordPress_Columns\Views_Column; |
| 10 | use IAWP\Dashboard_Options; |
| 11 | use IAWP\Database; |
| 12 | use IAWP\Date_Range\Exact_Date_Range; |
| 13 | use IAWP\Ecommerce\SureCart_Event_Sync_Job; |
| 14 | use IAWP\Ecommerce\SureCart_Store; |
| 15 | use IAWP\Env; |
| 16 | use IAWP\FetchFaviconsJob; |
| 17 | use IAWP\Geo_Database_Health_Check_Job; |
| 18 | use IAWP\Independent_Analytics; |
| 19 | use IAWP\Interrupt; |
| 20 | use IAWP\Journey\JourneyStatisticsJob; |
| 21 | use IAWP\MainWP; |
| 22 | use IAWP\Migration_Fixer_Job; |
| 23 | use IAWP\Migrations; |
| 24 | use IAWP\Overview\Module_Refresh_Job; |
| 25 | use IAWP\Overview\Modules\Module; |
| 26 | use IAWP\Patch; |
| 27 | use IAWP\Public_API\Analytics; |
| 28 | use IAWP\Public_API\Singular_Analytics; |
| 29 | use IAWP\Public_API\Top_Posts; |
| 30 | use IAWP\Report_Finder; |
| 31 | use IAWP\Utils\BladeOne; |
| 32 | use IAWP\WP_Option_Cache_Bust; |
| 33 | \define( 'IAWP_DIRECTORY', \rtrim( \plugin_dir_path( __FILE__ ), \DIRECTORY_SEPARATOR ) ); |
| 34 | \define( 'IAWP_URL', \rtrim( \plugin_dir_url( __FILE__ ), '/' ) ); |
| 35 | \define( 'IAWP_VERSION', '2.14.8' ); |
| 36 | \define( 'IAWP_DATABASE_VERSION', '52' ); |
| 37 | \define( 'IAWP_LANGUAGES_DIRECTORY', \dirname( \plugin_basename( __FILE__ ) ) . '/languages' ); |
| 38 | \define( 'IAWP_PLUGIN_FILE', __DIR__ . '/iawp.php' ); |
| 39 | if ( \file_exists( \IAWPSCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ) ) ) { |
| 40 | require_once \IAWPSCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 41 | } else { |
| 42 | require_once \IAWPSCOPED\iawp_path_to( 'vendor/autoload.php' ); |
| 43 | } |
| 44 | /** |
| 45 | * @param $log |
| 46 | * |
| 47 | * @return void |
| 48 | * @internal |
| 49 | */ |
| 50 | function iawp_log( $log ) : void { |
| 51 | if ( \WP_DEBUG === \true && \WP_DEBUG_LOG === \true ) { |
| 52 | if ( \is_array( $log ) || \is_object( $log ) ) { |
| 53 | \error_log( \print_r( $log, \true ) ); |
| 54 | } else { |
| 55 | \error_log( $log ); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** @internal */ |
| 61 | function iawp_path_to( string $path ) : string { |
| 62 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 63 | return \implode( \DIRECTORY_SEPARATOR, [\IAWP_DIRECTORY, $path] ); |
| 64 | } |
| 65 | |
| 66 | /** @internal */ |
| 67 | function iawp_url_to( string $path ) : string { |
| 68 | $path = \trim( $path, '/' ); |
| 69 | return \implode( '/', [\IAWP_URL, $path] ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * add_filter('iawp_temp_directory_path', function ($value) { |
| 74 | * return '/Users/andrew/site/wp-content/uploads/iawp'; |
| 75 | * }); |
| 76 | * |
| 77 | * @param string $path |
| 78 | * |
| 79 | * @return string |
| 80 | * @throws Exception |
| 81 | * @internal |
| 82 | */ |
| 83 | function iawp_temp_path_to( string $path ) : string { |
| 84 | $temp_directory = ( \defined( 'IAWP_TEMP_DIR' ) ? \IAWP_TEMP_DIR : \apply_filters( 'iawp_temp_directory_path', 'temp' ) ); |
| 85 | $path = \rtrim( $path, \DIRECTORY_SEPARATOR ); |
| 86 | if ( $temp_directory === 'temp' ) { |
| 87 | return \IAWPSCOPED\iawp_path_to( \implode( \DIRECTORY_SEPARATOR, [$temp_directory, $path] ) ); |
| 88 | } |
| 89 | $temp_directory = \rtrim( $temp_directory, \DIRECTORY_SEPARATOR ); |
| 90 | if ( !\is_writable( $temp_directory ) ) { |
| 91 | \wp_mkdir_p( $temp_directory ); |
| 92 | // Separate condition to see if wp_mkdir_p call fixed the issue |
| 93 | if ( !\is_writable( $temp_directory ) ) { |
| 94 | throw new \Exception('The temp directory set with the iawp_temp_directory_path filter is missing or is not writable: ' . $temp_directory); |
| 95 | } |
| 96 | } |
| 97 | return \implode( \DIRECTORY_SEPARATOR, [$temp_directory, $path] ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param string $path |
| 102 | * @param bool $prefer_parent_site_upload_path If it's a multisite installation, use the parent sites upload folder |
| 103 | * @return string |
| 104 | * @internal |
| 105 | */ |
| 106 | function iawp_upload_path_to( string $path, bool $prefer_parent_site_upload_path = \false ) : string { |
| 107 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 108 | $upload_directory = \wp_upload_dir()['basedir']; |
| 109 | if ( $prefer_parent_site_upload_path && \is_multisite() ) { |
| 110 | $site = \get_site(); |
| 111 | if ( $site !== null ) { |
| 112 | \switch_to_blog( \intval( $site->site_id ) ); |
| 113 | $upload_directory = \wp_upload_dir()['basedir']; |
| 114 | \restore_current_blog(); |
| 115 | } |
| 116 | } |
| 117 | return \implode( \DIRECTORY_SEPARATOR, [$upload_directory, $path] ); |
| 118 | } |
| 119 | |
| 120 | /** @internal */ |
| 121 | function iawp_upload_url_to( string $path ) : string { |
| 122 | $upload_url = \wp_upload_dir()['baseurl']; |
| 123 | $path = \trim( $path, '/' ); |
| 124 | return \implode( '/', [$upload_url, $path] ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Determines if the user is running a licensed pro version |
| 129 | * |
| 130 | * @return bool |
| 131 | * @internal |
| 132 | */ |
| 133 | function iawp_is_pro() : bool { |
| 134 | return \false; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Determines if the user is running a free version or an unlicensed pro version |
| 139 | * @return bool |
| 140 | * @internal |
| 141 | */ |
| 142 | function iawp_is_free() : bool { |
| 143 | return !\IAWPSCOPED\iawp_is_pro(); |
| 144 | } |
| 145 | |
| 146 | /** @internal */ |
| 147 | function iawp_dashboard_url( array $query_arguments = [] ) : string { |
| 148 | $default_query_arguments = [ |
| 149 | 'page' => 'independent-analytics', |
| 150 | ]; |
| 151 | return \add_query_arg( \array_merge( $default_query_arguments, $query_arguments ), \admin_url( 'admin.php' ) ); |
| 152 | } |
| 153 | |
| 154 | /** @internal */ |
| 155 | function iawp_render( string $view, array $variables = [] ) : string { |
| 156 | static $blade = null; |
| 157 | if ( $blade === null ) { |
| 158 | if ( !\file_exists( \IAWPSCOPED\iawp_temp_path_to( 'template-cache' ) ) ) { |
| 159 | \wp_mkdir_p( \IAWPSCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 160 | } |
| 161 | $blade = BladeOne::create(); |
| 162 | $blade->share( 'env', new Env() ); |
| 163 | $blade->share( 'is_pro', \IAWPSCOPED\iawp_is_pro() ); |
| 164 | $blade->share( 'security', new \IAWP\Utils\Security() ); |
| 165 | } |
| 166 | return $blade->run( $view, $variables ); |
| 167 | } |
| 168 | |
| 169 | /** @internal */ |
| 170 | function iawp_icon( string $icon ) : string { |
| 171 | try { |
| 172 | return \IAWPSCOPED\iawp_render( 'icons.plugins.' . $icon ); |
| 173 | } catch ( \Throwable $e ) { |
| 174 | return ''; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get the currently installed database version |
| 180 | * |
| 181 | * @return int |
| 182 | * @internal |
| 183 | */ |
| 184 | function iawp_db_version() : int { |
| 185 | return \intval( \get_option( 'iawp_db_version', '0' ) ); |
| 186 | } |
| 187 | |
| 188 | /** @internal */ |
| 189 | function iawp_intify( $value ) { |
| 190 | if ( \is_string( $value ) && \ctype_digit( $value ) ) { |
| 191 | return \intval( $value ); |
| 192 | } |
| 193 | return $value; |
| 194 | } |
| 195 | |
| 196 | /** @internal */ |
| 197 | function iawp_custom_log( $line ) { |
| 198 | try { |
| 199 | $filePath = \IAWPSCOPED\iawp_path_to( 'public-custom-log.txt' ); |
| 200 | // Normalize line endings |
| 201 | $line = \rtrim( $line, "\r\n" ); |
| 202 | // Append the line |
| 203 | \file_put_contents( $filePath, $line . \PHP_EOL, \FILE_APPEND ); |
| 204 | } catch ( \Throwable $e ) { |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * iawp_singular_analytics('60', new DateTime('-3 days'), new DateTime()); |
| 210 | * |
| 211 | * @param string|int $singular_id |
| 212 | * @param DateTime $from |
| 213 | * @param DateTime $to |
| 214 | * |
| 215 | * @return Singular_Analytics|null |
| 216 | * @internal |
| 217 | */ |
| 218 | function iawp_singular_analytics( $singular_id, \DateTime $from, \DateTime $to ) : ?Singular_Analytics { |
| 219 | $date_range = new Exact_Date_Range($from, $to); |
| 220 | return Singular_Analytics::for( $singular_id, $date_range ); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * iawp_top_posts(); |
| 225 | * |
| 226 | * Retrieves top posts with customizable options. |
| 227 | * |
| 228 | * @param array{ |
| 229 | * post_type: string, // Default: 'post' |
| 230 | * category: ?int, // Default: null |
| 231 | * limit: int, // Default: 10 |
| 232 | * from: \DateTimeInterface, // Default: 30 days ago |
| 233 | * to: \DateTimeInterface, // Default: today |
| 234 | * sort_by: string, // Default: 'views' - supports 'views', 'visitors', or 'sessions' |
| 235 | * } $options |
| 236 | * |
| 237 | * @return array |
| 238 | * @internal |
| 239 | */ |
| 240 | function iawp_top_posts( array $options = [] ) : array { |
| 241 | return ( new Top_Posts($options) )->get(); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * iawp_analytics(new DateTime('-3 days'), new DateTime()); |
| 246 | * |
| 247 | * @param DateTime $from |
| 248 | * @param DateTime $to |
| 249 | * |
| 250 | * @return Analytics |
| 251 | * @internal |
| 252 | */ |
| 253 | function iawp_analytics( \DateTime $from, \DateTime $to ) : Analytics { |
| 254 | $date_range = new Exact_Date_Range($from, $to); |
| 255 | return Analytics::for( $date_range ); |
| 256 | } |
| 257 | |
| 258 | if ( !\extension_loaded( 'pdo' ) || !\extension_loaded( 'pdo_mysql' ) ) { |
| 259 | $interrupt = new Interrupt('interrupt.pdo'); |
| 260 | $interrupt->render(); |
| 261 | return; |
| 262 | } |
| 263 | if ( \IAWPSCOPED\iawp_db_version() === 0 && !Database::has_correct_database_privileges() ) { |
| 264 | $interrupt = new Interrupt('interrupt.missing-database-permissions'); |
| 265 | $interrupt->render( [ |
| 266 | 'missing_privileges' => Database::missing_database_privileges(), |
| 267 | ] ); |
| 268 | return; |
| 269 | } |
| 270 | global $wpdb; |
| 271 | if ( \strlen( $wpdb->prefix ) > 25 ) { |
| 272 | $interrupt = new Interrupt('interrupt.database-prefix-too-long'); |
| 273 | $interrupt->render( [ |
| 274 | 'prefix' => $wpdb->prefix, |
| 275 | 'length' => \strlen( $wpdb->prefix ), |
| 276 | ] ); |
| 277 | return; |
| 278 | } |
| 279 | if ( Migrations\Migrations::is_database_ahead_of_plugin() ) { |
| 280 | $interrupt = new Interrupt('interrupt.database-ahead-of-plugin'); |
| 281 | $interrupt->render(); |
| 282 | return; |
| 283 | } |
| 284 | if ( \get_option( 'iawp_missing_tables' ) === '1' ) { |
| 285 | if ( \IAWPSCOPED\iawp_db_version() === 0 ) { |
| 286 | \delete_option( 'iawp_missing_tables' ); |
| 287 | } else { |
| 288 | $interrupt = new Interrupt('interrupt.missing-database-tables'); |
| 289 | $interrupt->render(); |
| 290 | return; |
| 291 | } |
| 292 | } |
| 293 | // These can be updated in background jobs. Always get the actual value from the database. |
| 294 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 295 | WP_Option_Cache_Bust::register( 'iawp_should_refresh_modules' ); |
| 296 | WP_Option_Cache_Bust::register( 'iawp_migration_started_at' ); |
| 297 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 298 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 299 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 300 | WP_Option_Cache_Bust::register( 'iawp_overview_modules' ); |
| 301 | WP_Option_Cache_Bust::register( 'iawp_modules_refreshed_at' ); |
| 302 | WP_Option_Cache_Bust::register( 'iawp_default_modules_added' ); |
| 303 | /** @internal */ |
| 304 | function iawp() { |
| 305 | return Independent_Analytics::getInstance(); |
| 306 | } |
| 307 | |
| 308 | \IAWPSCOPED\iawp(); |
| 309 | // This fires when either the free or pro plugin versions are activated. This does not fire |
| 310 | // when transitioning from free-to-pro or pro-to-free via Freemius. The hooks below |
| 311 | // take care of that. |
| 312 | // |
| 313 | // What's up with the transient option iawp_activation_ran? The goal is to prevent |
| 314 | \register_activation_hook( \IAWP_PLUGIN_FILE, function () { |
| 315 | \set_transient( 'iawp_activation_ran', \true, 60 ); |
| 316 | ActivationLifecycle::handle_activation(); |
| 317 | } ); |
| 318 | // This fires when transitioning from pro-to-free via Freemius. In that case, register_activation_hook |
| 319 | // will not fire. This hook is essential to make sure the free activation code runs. |
| 320 | \IAWP_FS()->add_action( 'after_free_version_reactivation', function () { |
| 321 | if ( \get_transient( 'iawp_activation_ran' ) ) { |
| 322 | return; |
| 323 | } |
| 324 | \set_transient( 'iawp_activation_ran', \true, 60 ); |
| 325 | ActivationLifecycle::handle_activation(); |
| 326 | } ); |
| 327 | // This fires when transitioning from free-to-pro via Freemius. In that case, register_activation_hook |
| 328 | // will not fire. This hook is essential to make sure the pro activation code runs. |
| 329 | \IAWP_FS()->add_action( 'after_premium_version_activation', function () { |
| 330 | if ( \get_transient( 'iawp_activation_ran' ) ) { |
| 331 | return; |
| 332 | } |
| 333 | \set_transient( 'iawp_activation_ran', \true, 60 ); |
| 334 | ActivationLifecycle::handle_activation(); |
| 335 | } ); |
| 336 | \register_deactivation_hook( \IAWP_PLUGIN_FILE, function () { |
| 337 | \delete_transient( 'iawp_activation_ran' ); |
| 338 | ActivationLifecycle::handle_deactivation(); |
| 339 | } ); |
| 340 | // This fires for the original version of the plugin and not the updated version of the plugin |
| 341 | \add_action( 'upgrader_process_complete', function () { |
| 342 | // Trigger the click processing cron job so no clicks are lost |
| 343 | \do_action( 'iawp_click_processing' ); |
| 344 | } ); |
| 345 | \add_action( 'init', function () { |
| 346 | Config_File_Manager::ensure(); |
| 347 | ( new Geo_Database_Health_Check_Job() )->schedule(); |
| 348 | ( new Migration_Fixer_Job() )->schedule(); |
| 349 | ( new FetchFaviconsJob() )->schedule(); |
| 350 | if ( \IAWPSCOPED\iawp_is_pro() ) { |
| 351 | ( new Click_Processing_Job() )->schedule(); |
| 352 | ( new Module_Refresh_Job() )->schedule(); |
| 353 | ( new JourneyStatisticsJob() )->schedule(); |
| 354 | } |
| 355 | if ( \IAWPSCOPED\iawp()->is_surecart_support_enabled() ) { |
| 356 | ( new SureCart_Event_Sync_Job() )->schedule(); |
| 357 | } |
| 358 | if ( \IAWPSCOPED\iawp()->is_surecart_support_enabled() && \in_array( \get_option( 'iawp_surecart_currency_code' ), ['', \false] ) ) { |
| 359 | SureCart_Store::cache_currency_code(); |
| 360 | } |
| 361 | } ); |
| 362 | \add_action( 'admin_init', function () { |
| 363 | Migrations\Migrations::handle_migration_18_error(); |
| 364 | Migrations\Migrations::handle_migration_22_error(); |
| 365 | Migrations\Migrations::handle_migration_29_error(); |
| 366 | Migrations\Migrations::handle_migration_45_collation_error(); |
| 367 | Migrations\Migrations::handle_migration_46_error(); |
| 368 | Patch::patch_2_6_2_incorrect_email_report_schedule(); |
| 369 | Patch::patch_2_8_7_potential_duplicates(); |
| 370 | Config_File_Manager::ensure(); |
| 371 | $options = Dashboard_Options::getInstance(); |
| 372 | $options->maybe_redirect(); |
| 373 | new Migrations\Migration_Job(); |
| 374 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 375 | // If there is no database installed, run migration on current process |
| 376 | Migrations\Migrations::create_or_migrate(); |
| 377 | } else { |
| 378 | // If there is a database, run migration in a background process |
| 379 | Migrations\Migration_Job::maybe_dispatch(); |
| 380 | } |
| 381 | if ( \get_option( 'iawp_should_refresh_modules', '0' ) === '1' ) { |
| 382 | \update_option( 'iawp_should_refresh_modules', '0', \true ); |
| 383 | Module::refresh_all_modules(); |
| 384 | } |
| 385 | if ( \get_option( 'iawp_has_added_journey_default_reports', '0' ) === '0' ) { |
| 386 | \update_option( 'iawp_has_added_journey_default_reports', '1', \true ); |
| 387 | $report_finder = new Report_Finder(); |
| 388 | if ( empty( $report_finder->get_saved_reports_for_type( 'journeys' ) ) ) { |
| 389 | Report_Finder::insert_default_user_journey_reports(); |
| 390 | } |
| 391 | } |
| 392 | } ); |
| 393 | Views_Column::initialize(); |
| 394 | MainWP::initialize(); |
| 395 | ComplianzIntegration::initialize(); |