Skip to content

Commit 4772870

Browse files
committed
updater process updated
1 parent 8a9c182 commit 4772870

File tree

3 files changed

+133
-223
lines changed

3 files changed

+133
-223
lines changed

includes/Helper.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Cbx\Careertoolkit;
4+
5+
/**
6+
* Class Helper
7+
* @package Cbx\Careertoolkit
8+
* @since 1.0.0
9+
*/
10+
class Helper {
11+
12+
/**
13+
* Load textdomain
14+
*
15+
* @since 1.0.0
16+
*/
17+
public function load_plugin_textdomain() {
18+
load_plugin_textdomain( 'cbxcareertoolkit', false, CBXCAREER_TOOLKIT_ROOT_PATH . 'languages/' );
19+
}//end method load_plugin_textdomain
20+
21+
/**
22+
* Custom update checker implemented
23+
*
24+
* @param $transient
25+
*
26+
* @return mixed
27+
*/
28+
public function pre_set_site_transient_update_plugins( $transient ) {
29+
// Ensure the transient is set
30+
if ( empty( $transient->checked ) ) {
31+
return $transient;
32+
}
33+
34+
$plugin_slug = 'cbxcareertoolkit';
35+
$plugin_file = 'cbxcareertoolkit/cbxcareertoolkit.php';
36+
37+
if ( isset( $transient->response[ $plugin_file ] ) ) {
38+
return $transient;
39+
}
40+
41+
if ( ! function_exists( 'get_plugins' ) ) {
42+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
43+
}
44+
45+
$url = 'https://comforthrm.com/product_updates.json'; // Replace with your remote JSON file URL
46+
47+
// Fetch the remote JSON file
48+
$response = wp_remote_get( $url );
49+
50+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
51+
return $transient;
52+
}
53+
54+
$data = json_decode( wp_remote_retrieve_body( $response ), true );// Set true for associative array, false for object
55+
56+
57+
if ( ! isset( $data['cbxcareertoolkit'] ) ) {
58+
return $transient;
59+
}
60+
61+
$remote_data = $data['cbxcareertoolkit'];
62+
63+
$plugin_url = isset( $remote_data['url'] ) ? $remote_data['url'] : '';
64+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
65+
66+
$remote_version = isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '';
67+
68+
if ( $remote_version != '' && version_compare( $remote_version, $transient->checked[ $plugin_file ], '>' ) ) {
69+
$transient->response[ $plugin_file ] = (object) [
70+
'slug' => $plugin_slug,
71+
'new_version' => $remote_version,
72+
'url' => $plugin_url,
73+
'package' => $package_url, // Link to the new version
74+
];
75+
}
76+
77+
return $transient;
78+
}//end method pre_set_site_transient_update_plugins
79+
80+
public function plugin_info( $res, $action, $args ) {
81+
// Plugin slug
82+
$plugin_slug = 'cbxcareertoolkit'; // Replace with your plugin slug
83+
84+
// Ensure we're checking the correct plugin
85+
if ( $action !== 'plugin_information' || $args->slug !== $plugin_slug ) {
86+
return $res;
87+
}
88+
89+
// Fetch detailed plugin information
90+
$response = wp_remote_get( 'https://comforthrm.com/product_updates.json' ); // Replace with your API URL
91+
92+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
93+
return $res;
94+
}
95+
96+
$data = json_decode( wp_remote_retrieve_body( $response ), true );
97+
if ( ! isset( $data[ $plugin_slug ] ) ) {
98+
return $res;
99+
}
100+
101+
$remote_data = $data[ $plugin_slug ];
102+
$package_url = isset( $remote_data['api_url'] ) ? $remote_data['api_url'] : false;
103+
104+
// Build the plugin info response
105+
return (object) [
106+
'name' => isset( $remote_data['name'] ) ? sanitize_text_field( $remote_data['name'] ) : 'CBX Careertoolkit',
107+
'slug' => $plugin_slug,
108+
'version' => isset( $remote_data['new_version'] ) ? sanitize_text_field( $remote_data['new_version'] ) : '',
109+
'author' => isset( $remote_data['author'] ) ? sanitize_text_field( $remote_data['author'] ) : '',
110+
'homepage' => isset( $remote_data['url'] ) ? $remote_data['url'] : '',
111+
'requires' => isset( $remote_data['requires'] ) ? sanitize_text_field( $remote_data['requires'] ) : '',
112+
'tested' => isset( $remote_data['tested'] ) ? sanitize_text_field( $remote_data['tested'] ) : '',
113+
'download_link' => $package_url,
114+
'sections' => [
115+
'description' => isset( $remote_data['description'] ) ? wp_kses_post( $remote_data['description'] ) : '',
116+
'changelog' => isset( $remote_data['changelog'] ) ? wp_kses_post( $remote_data['changelog'] ) : '',
117+
],
118+
];
119+
120+
}//end method plugin_info
121+
}//end class Helper

includes/Hooks.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
use Cbx\Careertoolkit\Factories\Job\DummyJobGenerate;
77
use Cbx\Careertoolkit\Factories\Misc\EasyPluginChecker;
88

9-
use Cbx\Careertoolkit\PDUpdater;
10-
119
class Hooks {
1210

1311
public function __construct() {
1412
$this->init_commands();
15-
//$this->update_checker();
13+
$this->update_checker();
14+
15+
$helper = new Helper();
16+
17+
add_filter( 'init', [ $helper, 'load_plugin_textdomain' ]);
1618
}
1719

1820
public function init_commands() {
@@ -24,18 +26,18 @@ public function init_commands() {
2426
}//end method init_commands
2527

2628
/**
27-
* Plugin update checker from github (https://github.com/codeboxrcodehub/cbxcareertoolkit)
29+
* Plugin updater hooks
2830
*
2931
* @return void
3032
*/
3133
public function update_checker() {
3234

33-
$github_token = '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';
35+
$helper = new Helper();
3436

35-
$updater = new PDUpdater( CBXCAREER_TOOLKIT_ROOT_PATH . 'cbxcareertoolkit.php' );
36-
$updater->set_username( 'codeboxrcodehub' );
37-
$updater->set_repository( 'cbxcareertoolkit' );
38-
$updater->authorize( $github_token );
39-
$updater->initialize();
37+
add_filter( 'pre_set_site_transient_update_plugins', [
38+
$helper,
39+
'pre_set_site_transient_update_plugins'
40+
] );
41+
add_filter( 'plugins_api', [ $helper, 'plugin_info' ], 10, 3 );
4042
}//end method update_checker
4143
}//end class Hooks

includes/PDUpdater.php

Lines changed: 0 additions & 213 deletions
This file was deleted.

0 commit comments

Comments
 (0)