independent-analytics
Last commit date
IAWP
2 years ago
dist
2 years ago
freemius
2 years ago
img
2 years ago
languages
2 years ago
sql
2 years ago
vendor
2 years ago
views
2 years ago
iawp-bootstrap.php
2 years ago
iawp.php
2 years ago
readme.txt
2 years ago
iawp-bootstrap.php
278 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED; |
| 4 | |
| 5 | use IAWP_SCOPED\eftec\bladeone\BladeOne ; |
| 6 | use IAWP_SCOPED\IAWP\Dashboard_Options ; |
| 7 | use IAWP_SCOPED\IAWP\Date_Range\Exact_Date_Range ; |
| 8 | use IAWP_SCOPED\IAWP\Env ; |
| 9 | use IAWP_SCOPED\IAWP\Geo_Database_Background_Job ; |
| 10 | use IAWP_SCOPED\IAWP\Independent_Analytics ; |
| 11 | use IAWP_SCOPED\IAWP\Interrupt ; |
| 12 | use IAWP_SCOPED\IAWP\Migrations ; |
| 13 | use IAWP_SCOPED\IAWP\Public_API\Analytics ; |
| 14 | use IAWP_SCOPED\IAWP\Public_API\Singular_Analytics ; |
| 15 | use IAWP_SCOPED\IAWP\WP_Option_Cache_Bust ; |
| 16 | \define( 'IAWP_DIRECTORY', \rtrim( \plugin_dir_path( __FILE__ ), \DIRECTORY_SEPARATOR ) ); |
| 17 | \define( 'IAWP_URL', \rtrim( \plugin_dir_url( __FILE__ ), '/' ) ); |
| 18 | \define( 'IAWP_VERSION', '2.2.0' ); |
| 19 | \define( 'IAWP_SCOPED\\IAWP_DATABASE_VERSION', '27' ); |
| 20 | \define( 'IAWP_LANGUAGES_DIRECTORY', \dirname( \plugin_basename( __FILE__ ) ) . '/languages' ); |
| 21 | \define( 'IAWP_SCOPED\\IAWP_PLUGIN_FILE', __DIR__ . '/iawp.php' ); |
| 22 | |
| 23 | if ( \file_exists( \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ) ) ) { |
| 24 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 25 | } else { |
| 26 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/autoload.php' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param $log |
| 31 | * |
| 32 | * @return void |
| 33 | * @internal |
| 34 | */ |
| 35 | function iawp_log( $log ) : void |
| 36 | { |
| 37 | if ( \WP_DEBUG === \true && \WP_DEBUG_LOG === \true ) { |
| 38 | |
| 39 | if ( \is_array( $log ) || \is_object( $log ) ) { |
| 40 | \error_log( \print_r( $log, \true ) ); |
| 41 | } else { |
| 42 | \error_log( $log ); |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** @internal */ |
| 49 | function iawp_path_to( string $path ) : string |
| 50 | { |
| 51 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 52 | return \implode( \DIRECTORY_SEPARATOR, [ \IAWP_DIRECTORY, $path ] ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * add_filter('iawp_temp_directory_path', function ($value) { |
| 57 | * return '/Users/andrew/site/wp-content/uploads/iawp'; |
| 58 | * }); |
| 59 | * |
| 60 | * @param string $path |
| 61 | * |
| 62 | * @return string |
| 63 | * @throws Exception |
| 64 | * @internal |
| 65 | */ |
| 66 | function iawp_temp_path_to( string $path ) : string |
| 67 | { |
| 68 | $temp_directory = \apply_filters( 'iawp_temp_directory_path', 'temp' ); |
| 69 | $path = \rtrim( $path, \DIRECTORY_SEPARATOR ); |
| 70 | if ( $temp_directory === 'temp' ) { |
| 71 | return \IAWP_SCOPED\iawp_path_to( \implode( \DIRECTORY_SEPARATOR, [ $temp_directory, $path ] ) ); |
| 72 | } |
| 73 | $temp_directory = \rtrim( $temp_directory, \DIRECTORY_SEPARATOR ); |
| 74 | if ( !\is_writable( $temp_directory ) ) { |
| 75 | \wp_mkdir_p( $temp_directory ); |
| 76 | } |
| 77 | // Separate condition to see if wp_mkdir_p call fixed the issue |
| 78 | if ( !\is_writable( $temp_directory ) ) { |
| 79 | throw new \Exception( 'You have provided and missing or non-writable directory for the iawp_temp_directory_path filter: ' . $temp_directory ); |
| 80 | } |
| 81 | return \implode( \DIRECTORY_SEPARATOR, [ $temp_directory, $path ] ); |
| 82 | } |
| 83 | |
| 84 | /** @internal */ |
| 85 | function iawp_url_to( string $path ) : string |
| 86 | { |
| 87 | $path = \trim( $path, '/' ); |
| 88 | return \implode( '/', [ \IAWP_URL, $path ] ); |
| 89 | } |
| 90 | |
| 91 | /** @internal */ |
| 92 | function iawp_upload_path_to( string $path ) : string |
| 93 | { |
| 94 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 95 | return \implode( \DIRECTORY_SEPARATOR, [ \wp_upload_dir()['basedir'], $path ] ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Determines if the user is running a licensed pro version |
| 100 | * |
| 101 | * @return bool |
| 102 | * @internal |
| 103 | */ |
| 104 | function iawp_is_pro() : bool |
| 105 | { |
| 106 | return \false; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Determines if the user is running a free version or an unlicensed pro version |
| 111 | * @return bool |
| 112 | * @internal |
| 113 | */ |
| 114 | function iawp_is_free() : bool |
| 115 | { |
| 116 | return !\IAWP_SCOPED\iawp_is_pro(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Determines if a pro user has WooCommerce activated |
| 121 | * @return bool |
| 122 | * @internal |
| 123 | */ |
| 124 | function iawp_using_woocommerce() : bool |
| 125 | { |
| 126 | global $wpdb ; |
| 127 | if ( \IAWP_SCOPED\iawp_is_free() ) { |
| 128 | return \false; |
| 129 | } |
| 130 | $class_missing = \class_exists( '\\WooCommerce' ) === \false; |
| 131 | if ( $class_missing ) { |
| 132 | return \false; |
| 133 | } |
| 134 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 135 | $order_stats_table = $wpdb->get_row( $wpdb->prepare( ' |
| 136 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 137 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 138 | ', $wpdb->dbname, $table_name ) ); |
| 139 | if ( \is_null( $order_stats_table ) ) { |
| 140 | return \false; |
| 141 | } |
| 142 | return \true; |
| 143 | } |
| 144 | |
| 145 | /** @internal */ |
| 146 | function iawp_dashboard_url( array $query_arguments = [] ) : string |
| 147 | { |
| 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_blade() : BladeOne |
| 156 | { |
| 157 | if ( !\file_exists( \IAWP_SCOPED\iawp_temp_path_to( 'template-cache' ) ) ) { |
| 158 | \wp_mkdir_p( \IAWP_SCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 159 | } |
| 160 | $blade = new BladeOne( \IAWP_SCOPED\iawp_path_to( 'views' ), \IAWP_SCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 161 | $blade->share( 'env', new Env() ); |
| 162 | return $blade; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * iawp_singular_analytics('60', new DateTime('-3 days'), new DateTime()); |
| 167 | * |
| 168 | * @param string|int $singular_id |
| 169 | * @param DateTime $from |
| 170 | * @param DateTime $to |
| 171 | * |
| 172 | * @return Singular_Analytics|null |
| 173 | * @internal |
| 174 | */ |
| 175 | function iawp_singular_analytics( $singular_id, \DateTime $from, \DateTime $to ) : ?Singular_Analytics |
| 176 | { |
| 177 | $date_range = new Exact_Date_Range( $from, $to ); |
| 178 | return Singular_Analytics::for( $singular_id, $date_range ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * iawp_analytics(new DateTime('-3 days'), new DateTime()); |
| 183 | * |
| 184 | * @param DateTime $from |
| 185 | * @param DateTime $to |
| 186 | * |
| 187 | * @return Analytics |
| 188 | * @internal |
| 189 | */ |
| 190 | function iawp_analytics( \DateTime $from, \DateTime $to ) : Analytics |
| 191 | { |
| 192 | $date_range = new Exact_Date_Range( $from, $to ); |
| 193 | return Analytics::for( $date_range ); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | if ( !\extension_loaded( 'pdo' ) || !\extension_loaded( 'pdo_mysql' ) ) { |
| 198 | $interrupt = new Interrupt( 'interrupt.pdo', \false ); |
| 199 | $interrupt->render(); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | global $wpdb ; |
| 204 | |
| 205 | if ( \strlen( $wpdb->prefix ) > 25 ) { |
| 206 | $interrupt = new Interrupt( 'interrupt.database-prefix-too-long', \false ); |
| 207 | $interrupt->render( [ |
| 208 | 'prefix' => $wpdb->prefix, |
| 209 | 'length' => \strlen( $wpdb->prefix ), |
| 210 | ] ); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | if ( Migrations\Migrations::is_database_ahead_of_plugin() ) { |
| 216 | $interrupt = new Interrupt( 'interrupt.database-ahead-of-plugin', \false ); |
| 217 | $interrupt->render(); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 222 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 223 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 224 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 225 | /** @internal */ |
| 226 | function iawp() |
| 227 | { |
| 228 | return Independent_Analytics::getInstance(); |
| 229 | } |
| 230 | |
| 231 | \IAWP_SCOPED\iawp(); |
| 232 | \register_activation_hook( \IAWP_SCOPED\IAWP_PLUGIN_FILE, function () { |
| 233 | \wp_mkdir_p( \IAWP_SCOPED\iawp_temp_path_to( 'template-cache' ) ); |
| 234 | \wp_mkdir_p( \IAWP_SCOPED\iawp_temp_path_to( 'device-data-cache' ) ); |
| 235 | |
| 236 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 237 | // If there is no database installed, run migration on current process |
| 238 | Migrations\Migrations::create_or_migrate(); |
| 239 | } else { |
| 240 | // If there is a database, run migration in a background process |
| 241 | Migrations\Migration_Job::maybe_dispatch(); |
| 242 | } |
| 243 | |
| 244 | Geo_Database_Background_Job::maybe_dispatch(); |
| 245 | \update_option( 'iawp_need_clear_cache', \true ); |
| 246 | \IAWP_SCOPED\iawp()->cron_manager->schedule_refresh_salt(); |
| 247 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 248 | \IAWP_SCOPED\iawp()->email_reports->schedule_email_report(); |
| 249 | } |
| 250 | } ); |
| 251 | \register_deactivation_hook( \IAWP_SCOPED\IAWP_PLUGIN_FILE, function () { |
| 252 | \IAWP_SCOPED\iawp()->cron_manager->unschedule_daily_salt_refresh(); |
| 253 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 254 | \IAWP_SCOPED\iawp()->email_reports->unschedule_email_report(); |
| 255 | } |
| 256 | \wp_delete_file( \trailingslashit( \WPMU_PLUGIN_DIR ) . 'iawp-performance-boost.php' ); |
| 257 | \delete_option( 'iawp_must_use_directory_not_writable' ); |
| 258 | } ); |
| 259 | /* |
| 260 | * The admin_init hook will fire when the dashboard is loaded or an admin ajax request is made |
| 261 | */ |
| 262 | \add_action( 'admin_init', function () { |
| 263 | Migrations\Migrations::handle_migration_18_error(); |
| 264 | Migrations\Migrations::handle_migration_22_error(); |
| 265 | $options = new Dashboard_Options(); |
| 266 | $options->maybe_redirect(); |
| 267 | new Migrations\Migration_Job(); |
| 268 | |
| 269 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 270 | // If there is no database installed, run migration on current process |
| 271 | Migrations\Migrations::create_or_migrate(); |
| 272 | } else { |
| 273 | // If there is a database, run migration in a background process |
| 274 | Migrations\Migration_Job::maybe_dispatch(); |
| 275 | } |
| 276 | |
| 277 | Geo_Database_Background_Job::maybe_dispatch(); |
| 278 | } ); |