aiass_get_url(), 'token' => aia_camacam_getToken(), 'client' => get_option( 'aia_camacam_client', '' ), 'locale' => get_user_locale(), ) ); } add_action('admin_menu', 'aia_camacam_aissistant_admin_menu'); function aia_camacam_aissistant_admin_menu() { add_menu_page('Aia', 'Aia', 'manage_options', 'aissistant', 'aia_camacam_admin_page', 'dashicons-admin-generic', 80); } function aiass_initialize($token,$client){ update_option('aia_camacam_token', $token); update_option('aia_camacam_client', $client); } function aiass_plugin_deactivate() { $data = array( 'system' => 'woocommerce', 'home_url' => get_home_url(), 'site_home' => get_option('home'), 'site_url' => get_option('siteurl') ); $url = aiass_get_url().'/api/v1/enroll/deactivate'; return aiass_send_data($url, $data); flush_rewrite_rules(); } function aiass_activate() { if (!class_exists('WooCommerce')) { wp_die('Aia Assistant requires WooCommerce to be installed and active. Please install WooCommerce first.'); } $enrolled = aia_camacam_sendEnrollData(); if ($enrolled instanceof WP_Error) { wp_die('AIA is under repair work at the moment. Coming back in a tick!'); aia_camacam_insert_exception_record($enrolled); }else{ if ( isset( $enrolled['body'] ) ) { $body = json_decode( $enrolled['body'] ); if ( ! is_object( $body ) || empty( $body->client ) || empty( $body->token ) ) { wp_die( 'AIA enrollment response was invalid. Please try again.' ); } $client = sanitize_text_field( $body->client ); $token = sanitize_text_field( $body->token ); aiass_initialize($token,$client); add_rewrite_rule( '^OiJIUzI1NiIsI3RuYW1lIjoiaHR0cHM6Ly90ZXN0c2hvcC5jYW1hY2FtLmV1IiwidGVuYW50SWQiOiIyIiwnR5cCI6IkpXVCJ9/aia/([^/]*)/?', 'index.php?aia_endpoint=$matches[1]', 'top' ); add_rewrite_rule( '^' . $client . '/aia/([^/]*)/?', 'index.php?aia_endpoint=$matches[1]', 'top' ); flush_rewrite_rules(); } } } function aia_camacam_sendEnrollData(){ global $wpdb; $current_user = wp_get_current_user(); $table_name = "aiaerrorlogs"; if ($wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) != $table_name) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, date datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, content text NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } $aiausr = get_user_by('email', 'aia@aissistant.shop'); if( $aiausr){ //well done }else{ $email = 'aia@aissistant.shop'; $password = wp_generate_password(); $userdata = array( 'user_login' => $email, 'user_email' => $email, 'first_name' => 'Aia', 'last_name' => 'Assistant', 'user_pass' => $password, ); $user_id = wp_insert_user($userdata); } $using_hpos = \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); if ($using_hpos) { // Новата схема (HPOS) $table_name = $wpdb->prefix . 'wc_orders'; $date_column = 'date_created_gmt'; $status_column = 'status'; } else { // Старата схема (Legacy) $table_name = $wpdb->prefix . 'posts'; $date_column = 'post_date_gmt'; $status_column = 'post_status'; } $curr_sites = array(); if( is_multisite() ){ $sites = get_sites(['deleted' => 0, 'archived' => 0]); foreach ( $sites as $site ) { switch_to_blog( $site->blog_id ); $curr_sites[] = array( 'blog_id' => $site->blog_id, 'prefix' => $wpdb->prefix, 'domain' => $site->domain . $site->path ); restore_current_blog(); } } $data = array( 'dbprefix' => $wpdb->prefix, 'db_prefix' => $wpdb->prefix, 'system' => 'wooCommerce', 'orders_table' => $table_name, 'date_column' => $date_column, 'status_column' => $status_column, 'is_multisite' => is_multisite(), 'multisites_prefixes' => json_encode($curr_sites), 'home_url' => get_home_url(), 'site_home' => get_option('home'), 'site_url' => get_option('siteurl'), 'woocommerce_email' => get_option('woocommerce_email_from'), 'current_user_id' => get_current_user_id(), 'current_user_email' => $current_user->user_email, 'userLocale' => get_user_locale(), 'siteLocale' => ( is_string( get_option('WPLANG') ) && get_option('WPLANG') !== '' ) ? get_option('WPLANG') : 'en_US', 'shop_locale' => ( is_string( get_option('WPLANG') ) && get_option('WPLANG') !== '' ) ? get_option('WPLANG') : 'en_US', 'blogname' => get_option('blogname'), 'blogdescription' => get_option('blogdescription'), 'admin_email' => get_option('admin_email'), 'users_can_register' => get_option('users_can_register'), 'default_category' => get_option('default_category'), 'timezone_string' => get_option('timezone_string'), 'woocommerce_default_country' => get_option('woocommerce_default_country'), 'woocommerce_currency' => get_option('woocommerce_currency'), 'woocommerce_allowed_countries' => get_option('woocommerce_allowed_countries') ); $url = aiass_get_url().'/api/v1/enroll'; return aiass_send_data($url, $data); }