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