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
183 lines
| 1 | <?php |
| 2 | |
| 3 | use IAWP\Geo_Database ; |
| 4 | use IAWP\Independent_Analytics ; |
| 5 | use IAWP\Migrations ; |
| 6 | use IAWP\Utils\Request ; |
| 7 | use IAWP\WP_Option_Cache_Bust ; |
| 8 | use IAWP\Query ; |
| 9 | use IAWP\Models\Visitor ; |
| 10 | define( 'IAWP_DIRECTORY', rtrim( plugin_dir_path( __FILE__ ), DIRECTORY_SEPARATOR ) ); |
| 11 | define( 'IAWP_URL', rtrim( plugin_dir_url( __FILE__ ), '/' ) ); |
| 12 | define( 'IAWP_VERSION', '1.19.1' ); |
| 13 | define( 'IAWP_LANGUAGES_DIRECTORY', dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 14 | // Load scoped composer autoloader |
| 15 | require_once iawp_path_to( 'vendor/scoper-autoload.php' ); |
| 16 | /** |
| 17 | * @param $log |
| 18 | * @return void |
| 19 | */ |
| 20 | function iawp_log( $log ) : void |
| 21 | { |
| 22 | if ( WP_DEBUG === true && WP_DEBUG_LOG === true ) { |
| 23 | |
| 24 | if ( is_array( $log ) || is_object( $log ) ) { |
| 25 | error_log( print_r( $log, true ) ); |
| 26 | } else { |
| 27 | error_log( $log ); |
| 28 | } |
| 29 | |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param $path |
| 35 | * @return string |
| 36 | */ |
| 37 | function iawp_path_to( $path ) : string |
| 38 | { |
| 39 | $path = trim( $path, DIRECTORY_SEPARATOR ); |
| 40 | return implode( DIRECTORY_SEPARATOR, [ IAWP_DIRECTORY, $path ] ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param $path |
| 45 | * @return string |
| 46 | */ |
| 47 | function iawp_url_to( $path ) : string |
| 48 | { |
| 49 | $path = trim( $path, '/' ); |
| 50 | return implode( '/', [ IAWP_URL, $path ] ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Provide defaults for arguments provided as an associated array |
| 55 | * @param array $args |
| 56 | * @param array $defaults |
| 57 | * @return array |
| 58 | */ |
| 59 | function iawp_default_args( array $args = array(), array $defaults = array() ) : array |
| 60 | { |
| 61 | $args = array_filter( $args, function ( $value ) { |
| 62 | // Remove key/value pairs where value is null |
| 63 | return $value !== null; |
| 64 | } ); |
| 65 | $args = array_intersect_key( $args, $defaults ); |
| 66 | return array_replace( $defaults, $args ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Determines if the user is running a licensed pro version |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | function iawp_is_pro() : bool |
| 75 | { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Determines if the user is running a free version or an unlicensed pro version |
| 81 | * @return bool |
| 82 | */ |
| 83 | function iawp_is_free() : bool |
| 84 | { |
| 85 | return !iawp_is_pro(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Determines if a pro user has WooCommerce activated |
| 90 | * @return bool |
| 91 | */ |
| 92 | function iawp_using_woocommerce() : bool |
| 93 | { |
| 94 | global $wpdb ; |
| 95 | if ( iawp_is_free() ) { |
| 96 | return false; |
| 97 | } |
| 98 | $class_missing = class_exists( 'WooCommerce' ) === false; |
| 99 | if ( $class_missing ) { |
| 100 | return false; |
| 101 | } |
| 102 | $table_name = $wpdb->prefix . 'wc_order_stats'; |
| 103 | $order_stats_table = $wpdb->get_row( $wpdb->prepare( ' |
| 104 | SELECT * FROM INFORMATION_SCHEMA.TABLES |
| 105 | WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s |
| 106 | ', DB_NAME, $table_name ) ); |
| 107 | if ( is_null( $order_stats_table ) ) { |
| 108 | return false; |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | function iawp() |
| 114 | { |
| 115 | return Independent_Analytics::getInstance(); |
| 116 | } |
| 117 | |
| 118 | iawp(); |
| 119 | WP_Option_Cache_Bust::register( 'iawp_is_migrating' ); |
| 120 | WP_Option_Cache_Bust::register( 'iawp_is_database_downloading' ); |
| 121 | WP_Option_Cache_Bust::register( 'iawp_db_version' ); |
| 122 | WP_Option_Cache_Bust::register( 'iawp_geo_database_version' ); |
| 123 | register_activation_hook( __DIR__ . '/iawp.php', function () { |
| 124 | |
| 125 | if ( get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 126 | // If there is no database installed, run migration on current process |
| 127 | Migrations\Migration::create_or_migrate(); |
| 128 | } else { |
| 129 | // If there is a database, run migration in a background process |
| 130 | Migrations\Migration_Job::maybe_dispatch(); |
| 131 | } |
| 132 | |
| 133 | $geo_database = new Geo_Database(); |
| 134 | $geo_database->maybe_dispatch_download_job(); |
| 135 | update_option( 'iawp_need_clear_cache', true ); |
| 136 | if ( iawp_is_pro() ) { |
| 137 | iawp()->email_reports->schedule_email_report(); |
| 138 | } |
| 139 | } ); |
| 140 | register_deactivation_hook( __DIR__ . '/iawp.php', function () { |
| 141 | if ( iawp_is_pro() ) { |
| 142 | iawp()->email_reports->unschedule_email_report(); |
| 143 | } |
| 144 | } ); |
| 145 | /* |
| 146 | * The admin_init hook will fire when the dashboard is loaded or an admin ajax request is made |
| 147 | */ |
| 148 | add_action( 'admin_init', function () { |
| 149 | new Migrations\Migration_Job(); |
| 150 | |
| 151 | if ( get_option( 'iawp_db_version', '0' ) === '0' ) { |
| 152 | // If there is no database installed, run migration on current process |
| 153 | Migrations\Migration::create_or_migrate(); |
| 154 | } else { |
| 155 | // If there is a database, run migration in a background process |
| 156 | Migrations\Migration_Job::maybe_dispatch(); |
| 157 | } |
| 158 | |
| 159 | $geo_database = new Geo_Database(); |
| 160 | $geo_database->maybe_dispatch_download_job(); |
| 161 | } ); |
| 162 | // Since WC 4.3.0 |
| 163 | add_action( 'woocommerce_checkout_order_created', function ( $order ) { |
| 164 | global $wpdb ; |
| 165 | $views_table = Query::get_table_name( Query::VIEWS ); |
| 166 | $sessions_table = Query::get_table_name( Query::SESSIONS ); |
| 167 | $wc_orders_table = Query::get_table_name( Query::WC_ORDERS ); |
| 168 | $order_id = $order->id; |
| 169 | $visitor = new Visitor( Request::ip(), Request::user_agent() ); |
| 170 | $most_recent_view = $wpdb->get_row( $wpdb->prepare( "SELECT views.id as id FROM {$views_table} AS views LEFT JOIN {$sessions_table} AS sessions ON sessions.session_id = views.session_id WHERE sessions.visitor_id = %s ORDER BY views.viewed_at DESC LIMIT 1", $visitor->id() ) ); |
| 171 | if ( is_null( $most_recent_view ) ) { |
| 172 | return; |
| 173 | } |
| 174 | $existing_wc_order = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $order_id ) ); |
| 175 | if ( !is_null( $existing_wc_order ) ) { |
| 176 | return; |
| 177 | } |
| 178 | $wpdb->insert( $wc_orders_table, [ |
| 179 | 'view_id' => $most_recent_view->id, |
| 180 | 'order_id' => $order_id, |
| 181 | 'created_at' => ( new \DateTime() )->format( 'Y-m-d H:i:s' ), |
| 182 | ] ); |
| 183 | } ); |