Skip to content

Commit ba18a97

Browse files
committed
updater process updated, textdomain called on init hook
1 parent bf07611 commit ba18a97

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

cbxphpfpdf.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ function __construct()
5050

5151
add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 2);
5252
add_action( 'admin_notices', [ $this, 'activation_error_display' ] );
53+
54+
add_action( 'init', [ $this, 'load_plugin_textdomain' ]);
55+
56+
add_filter( 'pre_set_site_transient_update_plugins', [
57+
$this,
58+
'pre_set_site_transient_update_plugins'
59+
] );
60+
add_filter( 'plugins_api', [ $this, 'plugin_info' ], 10, 3 );
5361
}
5462

5563
/**
@@ -156,6 +164,116 @@ public function plugin_row_meta($links, $file)
156164
return $links;
157165
}
158166

167+
/**
168+
* Load textdomain
169+
*
170+
* @since 1.0.0
171+
*/
172+
public function load_plugin_textdomain() {
173+
load_plugin_textdomain( 'cbxphpfpdf', false, CBXPHPFPDF_ROOT_PATH . 'languages/' );
174+
}//end method load_plugin_textdomain
175+
176+
/**
177+
* Custom update checker implemented
178+
*
179+
* @param $transient
180+
*
181+
* @return mixed
182+
*/
183+
public function pre_set_site_transient_update_plugins( $transient ) {
184+
// Ensure the transient is set
185+
if ( empty( $transient->checked ) ) {
186+
return $transient;
187+
}
188+
189+
$plugin_slug = 'cbxphpfpdf';
190+
$plugin_file = 'cbxphpfpdf/cbxphpfpdf.php';
191+
192+
if ( isset( $transient->response[ $plugin_file ] ) ) {
193+
return $transient;
194+
}
195+
196+
if ( ! function_exists( 'get_plugins' ) ) {
197+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
198+
}
199+
200+
$url = 'https://comforthrm.com/product_updates.json'; // Replace with your remote JSON file URL
201+
202+
// Fetch the remote JSON file
203+
$response = wp_remote_get( $url );
204+
205+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
206+
return $transient;
207+
}
208+
209+
$data = json_decode( wp_remote_retrieve_body( $response ), true );// Set true for associative array, false for object
210+
211+
212+
if ( ! isset( $data['cbxphpfpdf'] ) ) {
213+
return $transient;
214+
}
215+
216+
$remote_data = $data['cbxphpfpdf'];
217+
218+
$plugin_url = isset( $remote_data['url'] ) ? $remote_data['url'] : '';
219+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
220+
221+
$remote_version = isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '';
222+
223+
if ( $remote_version != '' && version_compare( $remote_version, $transient->checked[ $plugin_file ], '>' ) ) {
224+
$transient->response[ $plugin_file ] = (object) [
225+
'slug' => $plugin_slug,
226+
'new_version' => $remote_version,
227+
'url' => $plugin_url,
228+
'package' => $package_url, // Link to the new version
229+
];
230+
}
231+
232+
return $transient;
233+
}//end method pre_set_site_transient_update_plugins
234+
235+
public function plugin_info( $res, $action, $args ) {
236+
// Plugin slug
237+
$plugin_slug = 'cbxphpfpdf'; // Replace with your plugin slug
238+
239+
// Ensure we're checking the correct plugin
240+
if ( $action !== 'plugin_information' || $args->slug !== $plugin_slug ) {
241+
return $res;
242+
}
243+
244+
// Fetch detailed plugin information
245+
$response = wp_remote_get( 'https://comforthrm.com/product_updates.json' ); // Replace with your API URL
246+
247+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
248+
return $res;
249+
}
250+
251+
$data = json_decode( wp_remote_retrieve_body( $response ), true );
252+
if ( ! isset( $data[ $plugin_slug ] ) ) {
253+
return $res;
254+
}
255+
256+
$remote_data = $data[ $plugin_slug ];
257+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
258+
259+
// Build the plugin info response
260+
return (object) [
261+
'name' => isset( $remote_data['name'] ) ? sanitize_text_field( $remote_data['name'] ) : 'CBX PHPFPDF Library',
262+
'slug' => $plugin_slug,
263+
'version' => isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '',
264+
'author' => isset( $remote_data['author'] ) ? sanitize_text_field( $remote_data['author'] ) : '',
265+
'homepage' => isset( $remote_data['url'] ) ? $remote_data['url'] : '',
266+
'requires' => isset( $remote_data['requires'] ) ? sanitize_text_field( $remote_data['requires'] ) : '',
267+
'tested' => isset( $remote_data['tested'] ) ? sanitize_text_field( $remote_data['tested'] ) : '',
268+
'download_link' => $package_url,
269+
'sections' => [
270+
'description' => isset( $remote_data['description'] ) ? wp_kses_post( $remote_data['description'] ) : '',
271+
'changelog' => isset( $remote_data['changelog'] ) ? wp_kses_post( $remote_data['changelog'] ) : '',
272+
],
273+
];
274+
275+
}//end method plugin_info
276+
159277
}//end method CBXPhpFpdf
160278

161279
/**

0 commit comments

Comments
 (0)