Announcements.php
6 months ago
Licenser.php
1 month ago
Logger.php
6 months ago
Notification.php
6 months ago
Rollback.php
6 months ago
ScriptLoader.php
6 months ago
UninstallFeedback.php
6 months ago
Updater.php
1 month ago
Logger.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The logger model class for ThemeGrill SDK |
| 4 | * |
| 5 | * @package ThemeGrillSDK |
| 6 | * @subpackage Modules |
| 7 | * @copyright Copyright (c) 2025, ThemeGrill |
| 8 | * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace ThemeGrillSDK\Modules; |
| 13 | |
| 14 | use ThemeGrillSDK\Common\AbstractModule; |
| 15 | use ThemeGrillSDK\Loader; |
| 16 | use ThemeGrillSDK\Product; |
| 17 | |
| 18 | // Exit if accessed directly. |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Logger module for ThemeGrill SDK. |
| 25 | */ |
| 26 | class Logger extends AbstractModule { |
| 27 | /** |
| 28 | * Endpoint where to collect logs. |
| 29 | */ |
| 30 | const TRACKING_ENDPOINT = 'https://api.themegrill.com/tracking/log'; |
| 31 | |
| 32 | /** |
| 33 | * Check if we should load the module for this product. |
| 34 | * |
| 35 | * @param Product $product Product to load the module for. |
| 36 | * |
| 37 | * @return bool Should we load ? |
| 38 | */ |
| 39 | public function can_load( $product ) { |
| 40 | return apply_filters( $product->get_slug() . '_sdk_enable_logger', true ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Load module logic. |
| 45 | * |
| 46 | * @param Product $product Product to load. |
| 47 | * |
| 48 | * @return Logger Module object. |
| 49 | */ |
| 50 | public function load( $product ) { |
| 51 | $this->product = $product; |
| 52 | add_action( 'wp_loaded', array( $this, 'setup_actions' ) ); |
| 53 | add_action( 'admin_init', array( $this, 'setup_notification' ) ); |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Setup notification on admin. |
| 59 | */ |
| 60 | public function setup_notification() { |
| 61 | if ( $this->is_logger_active() ) { |
| 62 | return; |
| 63 | } |
| 64 | add_filter( 'themegrill_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Setup tracking actions. |
| 69 | */ |
| 70 | public function setup_actions() { |
| 71 | if ( ! $this->is_logger_active() ) { |
| 72 | return; |
| 73 | } |
| 74 | $action_key = $this->product->get_key() . '_log_activity'; |
| 75 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 76 | wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 77 | } |
| 78 | add_action( $action_key, array( $this, 'send_log' ) ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Check if the logger is active. |
| 83 | * |
| 84 | * @return bool Is logger active? |
| 85 | */ |
| 86 | private function is_logger_active() { |
| 87 | $default = 'no'; |
| 88 | |
| 89 | if ( ! $this->product->is_wordpress_available() ) { |
| 90 | $default = 'yes'; |
| 91 | } else { |
| 92 | $all_products = Loader::get_products(); |
| 93 | foreach ( $all_products as $product ) { |
| 94 | if ( $product->requires_license() ) { |
| 95 | $default = 'yes'; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 102 | } |
| 103 | /** |
| 104 | * Add notification to queue. |
| 105 | * |
| 106 | * @param array $all_notifications Previous notification. |
| 107 | * |
| 108 | * @return array All notifications. |
| 109 | */ |
| 110 | public function add_notification( $all_notifications ) { |
| 111 | |
| 112 | $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); |
| 113 | |
| 114 | $message = str_replace( |
| 115 | array( '{product}' ), |
| 116 | $this->product->get_friendly_name(), |
| 117 | $message |
| 118 | ); |
| 119 | $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', Loader::$labels['logger']['cta_y'] ); |
| 120 | $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', Loader::$labels['logger']['cta_n'] ); |
| 121 | |
| 122 | $all_notifications[] = [ |
| 123 | 'id' => $this->product->get_key() . '_logger_flag', |
| 124 | 'message' => $message, |
| 125 | 'ctas' => [ |
| 126 | 'confirm' => [ |
| 127 | 'link' => '#', |
| 128 | 'text' => $button_submit, |
| 129 | ], |
| 130 | 'cancel' => [ |
| 131 | 'link' => '#', |
| 132 | 'text' => $button_cancel, |
| 133 | ], |
| 134 | ], |
| 135 | ]; |
| 136 | |
| 137 | return $all_notifications; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Send the statistics to the api endpoint. |
| 142 | */ |
| 143 | public function send_log() { |
| 144 | $environment = array(); |
| 145 | $theme = wp_get_theme(); |
| 146 | $environment['theme'] = array(); |
| 147 | $environment['theme']['name'] = $theme->get( 'Name' ); |
| 148 | $environment['theme']['author'] = $theme->get( 'Author' ); |
| 149 | $environment['theme']['parent'] = $theme->parent() !== false ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' ); |
| 150 | $environment['plugins'] = get_option( 'active_plugins' ); |
| 151 | global $wp_version; |
| 152 | wp_remote_post( |
| 153 | self::TRACKING_ENDPOINT, |
| 154 | array( |
| 155 | 'method' => 'POST', |
| 156 | 'timeout' => 3, |
| 157 | 'redirection' => 5, |
| 158 | 'body' => wp_json_encode( |
| 159 | array( |
| 160 | 'site' => get_site_url(), |
| 161 | 'slug' => $this->product->get_slug(), |
| 162 | 'version' => $this->product->get_version(), |
| 163 | 'wp_version' => $wp_version, |
| 164 | 'install_time' => $this->product->get_install_time(), |
| 165 | 'locale' => get_locale(), |
| 166 | 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), |
| 167 | 'environment' => $environment, |
| 168 | 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), |
| 169 | ) |
| 170 | ), |
| 171 | 'headers' => array( |
| 172 | 'Content-Type' => 'application/json', |
| 173 | 'User-Agent' => 'ThemeGrillSDK', |
| 174 | ), |
| 175 | ) |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 |