Skip to content

Commit b04867b

Browse files
committed
updater process implemented
1 parent 7264c81 commit b04867b

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
lines changed

includes/CBXTaxonomyHelper.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,104 @@ public static function active_plugin() {
6868
new CBXWPMigrationsTable( $texonomy_migration_files, CBXTAXONOMY_PLUGIN_NAME );
6969
}//end method active plugin
7070

71+
/**
72+
* Custom update checker implemented
73+
*
74+
* @param $transient
75+
*
76+
* @return mixed
77+
*/
78+
public function pre_set_site_transient_update_plugins( $transient ) {
79+
// Ensure the transient is set
80+
if ( empty( $transient->checked ) ) {
81+
return $transient;
82+
}
83+
84+
$plugin_slug = 'cbxtaxonomy';
85+
$plugin_file = 'cbxtaxonomy/cbxtaxonomy.php';
86+
87+
if ( isset( $transient->response[ $plugin_file ] ) ) {
88+
return $transient;
89+
}
90+
91+
if ( ! function_exists( 'get_plugins' ) ) {
92+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
93+
}
94+
95+
$url = 'https://comforthrm.com/product_updates.json'; // Replace with your remote JSON file URL
96+
97+
// Fetch the remote JSON file
98+
$response = wp_remote_get( $url );
99+
100+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
101+
return $transient;
102+
}
103+
104+
$data = json_decode( wp_remote_retrieve_body( $response ), true );// Set true for associative array, false for object
105+
106+
107+
if ( ! isset( $data['cbxtaxonomy'] ) ) {
108+
return $transient;
109+
}
110+
111+
$remote_data = $data['cbxtaxonomy'];
112+
113+
$plugin_url = isset( $remote_data['url'] ) ? $remote_data['url'] : '';
114+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
115+
116+
$remote_version = isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '';
117+
118+
if ( $remote_version != '' && version_compare( $remote_version, $transient->checked[ $plugin_file ], '>' ) ) {
119+
$transient->response[ $plugin_file ] = (object) [
120+
'slug' => $plugin_slug,
121+
'new_version' => $remote_version,
122+
'url' => $plugin_url,
123+
'package' => $package_url, // Link to the new version
124+
];
125+
}
126+
127+
return $transient;
128+
}//end method pre_set_site_transient_update_plugins
129+
130+
public function plugin_info( $res, $action, $args ) {
131+
// Plugin slug
132+
$plugin_slug = 'cbxtaxonomy'; // Replace with your plugin slug
133+
134+
// Ensure we're checking the correct plugin
135+
if ( $action !== 'plugin_information' || $args->slug !== $plugin_slug ) {
136+
return $res;
137+
}
138+
139+
// Fetch detailed plugin information
140+
$response = wp_remote_get( 'https://comforthrm.com/product_updates.json' ); // Replace with your API URL
141+
142+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
143+
return $res;
144+
}
145+
146+
$data = json_decode( wp_remote_retrieve_body( $response ), true );
147+
if ( ! isset( $data[ $plugin_slug ] ) ) {
148+
return $res;
149+
}
150+
151+
$remote_data = $data[ $plugin_slug ];
152+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
153+
154+
// Build the plugin info response
155+
return (object) [
156+
'name' => isset( $remote_data['name'] ) ? sanitize_text_field( $remote_data['name'] ) : 'CBX Texonomy',
157+
'slug' => $plugin_slug,
158+
'version' => isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '',
159+
'author' => isset( $remote_data['author'] ) ? sanitize_text_field( $remote_data['author'] ) : '',
160+
'homepage' => isset( $remote_data['url'] ) ? $remote_data['url'] : '',
161+
'requires' => isset( $remote_data['requires'] ) ? sanitize_text_field( $remote_data['requires'] ) : '',
162+
'tested' => isset( $remote_data['tested'] ) ? sanitize_text_field( $remote_data['tested'] ) : '',
163+
'download_link' => $package_url,
164+
'sections' => [
165+
'description' => isset( $remote_data['description'] ) ? wp_kses_post( $remote_data['description'] ) : '',
166+
'changelog' => isset( $remote_data['changelog'] ) ? wp_kses_post( $remote_data['changelog'] ) : '',
167+
],
168+
];
169+
170+
}//end method plugin_info
71171
}//end class CBXTaxonomyHelper

includes/Hooks.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
<?php
2-
32
namespace Cbx\Taxonomy;
43

5-
use Cbx\Taxonomy\PDUpdater;
4+
use Cbx\Taxonomy\CBXTaxonomyHelper;
65

76
class Hooks {
87
public function __construct() {
9-
$this->update_checker();
10-
}
8+
$helper = new CBXTaxonomyHelper();
119

12-
public function update_checker() {
13-
$updater = new PDUpdater( CBXTAXONOMY_ROOT_PATH . 'cbxtaxonomy.php' );
14-
$updater->set_username( 'codeboxrcodehub' );
15-
$updater->set_repository( 'cbxtaxonomy' );
16-
$updater->authorize( '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' );
17-
$updater->initialize();
18-
}//end method update_checker
10+
add_filter( 'pre_set_site_transient_update_plugins', [
11+
$helper,
12+
'pre_set_site_transient_update_plugins'
13+
] );
14+
add_filter( 'plugins_api', [ $helper, 'plugin_info' ], 10, 3 );
15+
}
1916
}//end class Hooks

0 commit comments

Comments
 (0)