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