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