independent-analytics
Last commit date
IAWP
3 years ago
dist
3 years ago
freemius
3 years ago
img
3 years ago
languages
3 years ago
sql
3 years ago
templates
3 years ago
vendor
3 years ago
iawp-bootstrap.php
3 years ago
iawp.php
3 years ago
readme.txt
3 years ago
iawp-bootstrap.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Geo_Database_Background_Job ; |
| 6 | use IAWP_SCOPED\IAWP\Independent_Analytics ; |
| 7 | use IAWP_SCOPED\IAWP\Migrations ; |
| 8 | use IAWP_SCOPED\IAWP\WP_Option_Cache_Bust ; |
| 9 | \define( 'IAWP_DIRECTORY', \rtrim( \plugin_dir_path( __FILE__ ), \DIRECTORY_SEPARATOR ) ); |
| 10 | \define( 'IAWP_URL', \rtrim( \plugin_dir_url( __FILE__ ), '/' ) ); |
| 11 | \define( 'IAWP_VERSION', '1.28.3' ); |
| 12 | \define( 'IAWP_LANGUAGES_DIRECTORY', \dirname( \plugin_basename( __FILE__ ) ) . '/languages' ); |
| 13 | |
| 14 | if ( \file_exists( \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ) ) ) { |
| 15 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 16 | } else { |
| 17 | require_once \IAWP_SCOPED\iawp_path_to( 'vendor/autoload.php' ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param $log |
| 22 | * |
| 23 | * @return void |
| 24 | */ |
| 25 | function iawp_log( $log ) : void |
| 26 | { |
| 27 | if ( \WP_DEBUG === \true && \WP_DEBUG_LOG === \true ) { |
| 28 | |
| 29 | if ( \is_array( $log ) || \is_object( $log ) ) { |
| 30 | \error_log( \print_r( $log, \true ) ); |
| 31 | } else { |
| 32 | \error_log( $log ); |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param $path |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | function iawp_path_to( $path ) : string |
| 44 | { |
| 45 | $path = \trim( $path, \DIRECTORY_SEPARATOR ); |
| 46 | return \implode( \DIRECTORY_SEPARATOR, [ \IAWP_DIRECTORY, $path ] ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param $path |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | function iawp_url_to( $path ) : string |
| 55 | { |
| 56 | $path = \trim( $path, '/' ); |
| 57 | return \implode( '/', [ \IAWP_URL, $path ] ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Provide defaults for arguments provided as an associated array |
| 62 | * |
| 63 | * @param array $args |
| 64 | * @param array $defaults |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | function iawp_default_args( array $args = array(), array $defaults = array() ) : array |
| 69 | { |
| 70 | $args = \array_filter( $args, function ( $value ) { |
| 71 | // Remove key/value pairs where value is null |
| 72 | return $value !== null; |
| 73 | } ); |
| 74 | $args = \array_intersect_key( $args, $defaults ); |
| 75 | return \array_replace( $defaults, $args ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Determines if the user is running a licensed pro version |
| 80 | * |
| 81 | * @return bool |
| 82 | */ |
| 83 | function iawp_is_pro() : bool |
| 84 | { |
| 85 | return \false; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Determines if the user is running a free version or an unlicensed pro version |
| 90 | * @return bool |
| 91 | */ |
| 92 | function iawp_is_free() : bool |
| 93 | { |
| 94 | return !\IAWP_SCOPED\iawp_is_pro(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Determines if a pro user has WooCommerce activated |
| 99 | * @return bool |
| 100 | */ |
| 101 | function iawp_using_woocommerce() : bool |
| 102 | { |
| 103 | global $wpdb ; |
| 104 | if ( \IAWP_SCOPED\iawp_is_free() ) { |
| 105 | return \false; |
| 106 | } |
| 107 | $class_missing = \class_exists( '\\WooCommerce' ) === \false; |
| 108 | if ( $class_missing ) { |
| 109 | return \false; |
| 110 | } |
| 111 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 112 | $order_stats_table = $wpdb->get_row( $wpdb->prepare( ' |
| 113 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 114 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 115 | ', $wpdb->dbname, $table_name ) ); |
| 116 | if ( \is_null( $order_stats_table ) ) { |
| 117 | return \false; |
| 118 | } |
| 119 | return \true; |
| 120 | } |
| 121 | |
| 122 | function iawp_dashboard_url() |
| 123 | { |
| 124 | return \add_query_arg( [ |
| 125 | 'page' => 'independent-analytics', |
| 126 | ], \admin_url( 'admin.php' ) ); |
| 127 | } |
| 128 | |
| 129 | function iawp() |
| 130 | { |
| 131 | return Independent_Analytics::getInstance(); |
| 132 | } |
| 133 | |
| 134 | \IAWP_SCOPED\iawp(); |
| 135 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 136 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 137 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 138 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 139 | \register_activation_hook( __DIR__ . '/iawp.php', function () { |
| 140 | |
| 141 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 142 | // If there is no database installed, run migration on current process |
| 143 | Migrations\Migration::create_or_migrate(); |
| 144 | } else { |
| 145 | // If there is a database, run migration in a background process |
| 146 | Migrations\Migration_Job::maybe_dispatch(); |
| 147 | } |
| 148 | |
| 149 | Geo_Database_Background_Job::maybe_dispatch(); |
| 150 | \update_option( 'iawp_need_clear_cache', \true ); |
| 151 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 152 | \IAWP_SCOPED\iawp()->email_reports->schedule_email_report(); |
| 153 | } |
| 154 | } ); |
| 155 | \register_deactivation_hook( __DIR__ . '/iawp.php', function () { |
| 156 | if ( \IAWP_SCOPED\iawp_is_pro() ) { |
| 157 | \IAWP_SCOPED\iawp()->email_reports->unschedule_email_report(); |
| 158 | } |
| 159 | } ); |
| 160 | /* |
| 161 | * The admin_init hook will fire when the dashboard is loaded or an admin ajax request is made |
| 162 | */ |
| 163 | \add_action( 'admin_init', function () { |
| 164 | new Migrations\Migration_Job(); |
| 165 | |
| 166 | if ( \get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 167 | // If there is no database installed, run migration on current process |
| 168 | Migrations\Migration::create_or_migrate(); |
| 169 | } else { |
| 170 | // If there is a database, run migration in a background process |
| 171 | Migrations\Migration_Job::maybe_dispatch(); |
| 172 | } |
| 173 | |
| 174 | Geo_Database_Background_Job::maybe_dispatch(); |
| 175 | } ); |