|
46 | 46 | */ |
47 | 47 | class CBXPhpSpreadSheet { |
48 | 48 | public function __construct() { |
| 49 | + |
49 | 50 | // Load text domain |
50 | | - load_plugin_textdomain( 'cbxphpspreadsheet', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
51 | | - |
| 51 | + add_action( 'init', [ $this, 'load_plugin_textdomain' ]); |
| 52 | + |
52 | 53 | // Add custom row meta links |
53 | 54 | add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 ); |
54 | 55 | add_action( 'admin_notices', [ $this, 'activation_error_display' ] ); |
| 56 | + |
| 57 | + add_filter( 'pre_set_site_transient_update_plugins', [ |
| 58 | + $this, |
| 59 | + 'pre_set_site_transient_update_plugins' |
| 60 | + ] ); |
| 61 | + add_filter( 'plugins_api', [ $this, 'plugin_info' ], 10, 3 ); |
55 | 62 | }//end constructor |
56 | 63 |
|
57 | 64 | /** |
@@ -158,6 +165,116 @@ public function plugin_row_meta( $links, $file ) { |
158 | 165 |
|
159 | 166 | return $links; |
160 | 167 | } |
| 168 | + |
| 169 | + /** |
| 170 | + * Load textdomain |
| 171 | + * |
| 172 | + * @since 1.0.0 |
| 173 | + */ |
| 174 | + public function load_plugin_textdomain() { |
| 175 | + load_plugin_textdomain( 'cbxphpspreadsheet', false, CBXPHPSPREADSHEET_ROOT_PATH . 'languages/' ); |
| 176 | + }//end method load_plugin_textdomain |
| 177 | + |
| 178 | + /** |
| 179 | + * Custom update checker implemented |
| 180 | + * |
| 181 | + * @param $transient |
| 182 | + * |
| 183 | + * @return mixed |
| 184 | + */ |
| 185 | + public function pre_set_site_transient_update_plugins( $transient ) { |
| 186 | + // Ensure the transient is set |
| 187 | + if ( empty( $transient->checked ) ) { |
| 188 | + return $transient; |
| 189 | + } |
| 190 | + |
| 191 | + $plugin_slug = 'cbxphpspreadsheet'; |
| 192 | + $plugin_file = 'cbxphpspreadsheet/cbxphpspreadsheet.php'; |
| 193 | + |
| 194 | + if ( isset( $transient->response[ $plugin_file ] ) ) { |
| 195 | + return $transient; |
| 196 | + } |
| 197 | + |
| 198 | + if ( ! function_exists( 'get_plugins' ) ) { |
| 199 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 200 | + } |
| 201 | + |
| 202 | + $url = 'https://comforthrm.com/product_updates.json'; // Replace with your remote JSON file URL |
| 203 | + |
| 204 | + // Fetch the remote JSON file |
| 205 | + $response = wp_remote_get( $url ); |
| 206 | + |
| 207 | + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { |
| 208 | + return $transient; |
| 209 | + } |
| 210 | + |
| 211 | + $data = json_decode( wp_remote_retrieve_body( $response ), true );// Set true for associative array, false for object |
| 212 | + |
| 213 | + |
| 214 | + if ( ! isset( $data['cbxphpspreadsheet'] ) ) { |
| 215 | + return $transient; |
| 216 | + } |
| 217 | + |
| 218 | + $remote_data = $data['cbxphpspreadsheet']; |
| 219 | + |
| 220 | + $plugin_url = isset( $remote_data['url'] ) ? $remote_data['url'] : ''; |
| 221 | + $package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false; |
| 222 | + |
| 223 | + $remote_version = isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : ''; |
| 224 | + |
| 225 | + if ( $remote_version != '' && version_compare( $remote_version, $transient->checked[ $plugin_file ], '>' ) ) { |
| 226 | + $transient->response[ $plugin_file ] = (object) [ |
| 227 | + 'slug' => $plugin_slug, |
| 228 | + 'new_version' => $remote_version, |
| 229 | + 'url' => $plugin_url, |
| 230 | + 'package' => $package_url, // Link to the new version |
| 231 | + ]; |
| 232 | + } |
| 233 | + |
| 234 | + return $transient; |
| 235 | + }//end method pre_set_site_transient_update_plugins |
| 236 | + |
| 237 | + public function plugin_info( $res, $action, $args ) { |
| 238 | + // Plugin slug |
| 239 | + $plugin_slug = 'cbxphpspreadsheet'; // Replace with your plugin slug |
| 240 | + |
| 241 | + // Ensure we're checking the correct plugin |
| 242 | + if ( $action !== 'plugin_information' || $args->slug !== $plugin_slug ) { |
| 243 | + return $res; |
| 244 | + } |
| 245 | + |
| 246 | + // Fetch detailed plugin information |
| 247 | + $response = wp_remote_get( 'https://comforthrm.com/product_updates.json' ); // Replace with your API URL |
| 248 | + |
| 249 | + if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) { |
| 250 | + return $res; |
| 251 | + } |
| 252 | + |
| 253 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 254 | + if ( ! isset( $data[ $plugin_slug ] ) ) { |
| 255 | + return $res; |
| 256 | + } |
| 257 | + |
| 258 | + $remote_data = $data[ $plugin_slug ]; |
| 259 | + $package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false; |
| 260 | + |
| 261 | + // Build the plugin info response |
| 262 | + return (object) [ |
| 263 | + 'name' => isset( $remote_data['name'] ) ? sanitize_text_field( $remote_data['name'] ) : 'CBX PhpSpreadSheet Library', |
| 264 | + 'slug' => $plugin_slug, |
| 265 | + 'version' => isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '', |
| 266 | + 'author' => isset( $remote_data['author'] ) ? sanitize_text_field( $remote_data['author'] ) : '', |
| 267 | + 'homepage' => isset( $remote_data['url'] ) ? $remote_data['url'] : '', |
| 268 | + 'requires' => isset( $remote_data['requires'] ) ? sanitize_text_field( $remote_data['requires'] ) : '', |
| 269 | + 'tested' => isset( $remote_data['tested'] ) ? sanitize_text_field( $remote_data['tested'] ) : '', |
| 270 | + 'download_link' => $package_url, |
| 271 | + 'sections' => [ |
| 272 | + 'description' => isset( $remote_data['description'] ) ? wp_kses_post( $remote_data['description'] ) : '', |
| 273 | + 'changelog' => isset( $remote_data['changelog'] ) ? wp_kses_post( $remote_data['changelog'] ) : '', |
| 274 | + ], |
| 275 | + ]; |
| 276 | + |
| 277 | + }//end method plugin_info |
161 | 278 | }//end class CBXPhpSpreadSheet |
162 | 279 |
|
163 | 280 | /** |
|
0 commit comments