feat: Implement Siti Stock Plugin for WooCommerce integration
- Added core plugin structure with main class Siti_Stock_Plugin. - Implemented settings management through Siti_Stock_Settings. - Developed admin interface for settings configuration via Siti_Stock_Admin. - Created inventory management with external stock handling in Siti_Stock_Inventory_Manager. - Integrated synchronization service to fetch and apply stock updates from external API in Siti_Stock_Sync_Service. - Added custom product data store to manage combined stock values in Siti_Stock_Product_Data_Store. - Registered hooks for admin menus, settings, and synchronization processes. - Implemented REST API endpoint for triggering stock sync. - Added cron scheduling for automatic stock synchronization. - Included localization support for Dutch language.
This commit is contained in:
202
SitiWebUpdater.php
Normal file
202
SitiWebUpdater.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
class SitiWebUpdater {
|
||||
|
||||
private $file;
|
||||
|
||||
private $plugin;
|
||||
|
||||
private $basename;
|
||||
|
||||
private $active;
|
||||
|
||||
private $username;
|
||||
|
||||
private $repository;
|
||||
|
||||
private $authorize_token;
|
||||
|
||||
private $github_response;
|
||||
|
||||
public function __construct( $file ) {
|
||||
|
||||
$this->file = $file;
|
||||
|
||||
$this->set_plugin_properties();
|
||||
add_action( 'admin_init', array( $this, 'set_plugin_properties' ) );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set_plugin_properties() {
|
||||
if ( ! function_exists( 'get_plugin_data' ) ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
|
||||
$this->plugin = get_plugin_data( $this->file );
|
||||
$this->basename = plugin_basename( $this->file );
|
||||
$this->active = is_plugin_active( $this->basename );
|
||||
}
|
||||
|
||||
public function set_username( $username ) {
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
public function set_repository( $repository ) {
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function authorize( $token ) {
|
||||
$this->authorize_token = $token;
|
||||
}
|
||||
|
||||
private function get_repository_info() {
|
||||
if ( is_null( $this->github_response ) ) { // Do we have a response?
|
||||
$args = array();
|
||||
$request_uri = sprintf( 'https://api.github.com/repos/%s/%s/releases', $this->username, $this->repository ); // Build URI
|
||||
|
||||
$args = array();
|
||||
|
||||
if( $this->authorize_token ) { // Is there an access token?
|
||||
$args['headers']['Authorization'] = "bearer {$this->authorize_token}"; // Set the headers
|
||||
}
|
||||
|
||||
$response = json_decode( wp_remote_retrieve_body( wp_remote_get( $request_uri, $args ) ), true ); // Get JSON and parse it
|
||||
|
||||
if( is_array( $response ) ) { // If it is an array
|
||||
$response = current( $response ); // Get the first item
|
||||
}
|
||||
|
||||
$this->github_response = $response; // Set it to our property
|
||||
}
|
||||
}
|
||||
|
||||
private function get_latest_version_from_response() {
|
||||
if ( empty( $this->github_response['tag_name'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ltrim( $this->github_response['tag_name'], 'vV' );
|
||||
}
|
||||
|
||||
public function initialize() {
|
||||
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_transient' ), 10, 1 );
|
||||
add_filter( 'plugins_api', array( $this, 'plugin_popup' ), 10, 3);
|
||||
add_filter( 'upgrader_post_install', array( $this, 'after_install' ), 10, 3 );
|
||||
|
||||
// Add Authorization Token to download_package
|
||||
add_filter( 'upgrader_pre_download',
|
||||
function() {
|
||||
add_filter( 'http_request_args', [ $this, 'download_package' ], 15, 2 );
|
||||
return false; // upgrader_pre_download filter default return value.
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function modify_transient( $transient ) {
|
||||
|
||||
if( property_exists( $transient, 'checked') ) { // Check if transient has a checked property
|
||||
|
||||
if( $checked = $transient->checked ) { // Did Wordpress check for updates?
|
||||
|
||||
$this->get_repository_info(); // Get the repo info
|
||||
|
||||
$latest_version = $this->get_latest_version_from_response();
|
||||
|
||||
if ( null === $latest_version ) {
|
||||
return $transient;
|
||||
}
|
||||
|
||||
$out_of_date = version_compare( $latest_version, $checked[ $this->basename ], 'gt' ); // Check if we're out of date
|
||||
|
||||
if( $out_of_date ) {
|
||||
|
||||
$new_files = $this->github_response['zipball_url']; // Get the ZIP
|
||||
|
||||
$slug = current( explode('/', $this->basename ) ); // Create valid slug
|
||||
|
||||
$plugin = array( // setup our plugin info
|
||||
'url' => $this->plugin["PluginURI"],
|
||||
'slug' => $slug,
|
||||
'package' => $new_files,
|
||||
'new_version' => $latest_version
|
||||
);
|
||||
|
||||
$transient->response[$this->basename] = (object) $plugin; // Return it in response
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $transient; // Return filtered transient
|
||||
}
|
||||
|
||||
public function plugin_popup( $result, $action, $args ) {
|
||||
|
||||
if( ! empty( $args->slug ) ) { // If there is a slug
|
||||
|
||||
if( $args->slug == current( explode( '/' , $this->basename ) ) ) { // And it's our slug
|
||||
|
||||
$this->get_repository_info(); // Get our repo info
|
||||
$latest_version = $this->get_latest_version_from_response();
|
||||
|
||||
if ( null === $latest_version ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Set it to an array
|
||||
$plugin = array(
|
||||
'name' => $this->plugin["Name"],
|
||||
'slug' => $this->basename,
|
||||
'requires' => '5.1',
|
||||
'tested' => '6.4.3',
|
||||
'rating' => '100.0',
|
||||
'num_ratings' => '10823',
|
||||
'downloaded' => '14249',
|
||||
'added' => '2016-01-05',
|
||||
'version' => $latest_version,
|
||||
'author' => $this->plugin["AuthorName"],
|
||||
'author_profile' => $this->plugin["AuthorURI"],
|
||||
'last_updated' => $this->github_response['published_at'],
|
||||
'homepage' => $this->plugin["PluginURI"],
|
||||
'short_description' => $this->plugin["Description"],
|
||||
'sections' => array(
|
||||
'Description' => $this->plugin["Description"],
|
||||
'Updates' => $this->github_response['body'],
|
||||
),
|
||||
'download_link' => $this->github_response['zipball_url']
|
||||
);
|
||||
|
||||
return (object) $plugin; // Return the data
|
||||
}
|
||||
|
||||
}
|
||||
return $result; // Otherwise return default
|
||||
}
|
||||
|
||||
public function download_package( $args, $url ) {
|
||||
|
||||
if ( null !== $args['filename'] ) {
|
||||
if( $this->authorize_token ) {
|
||||
$args = array_merge( $args, array( "headers" => array( "Authorization" => "token {$this->authorize_token}" ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
remove_filter( 'http_request_args', [ $this, 'download_package' ] );
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
public function after_install( $response, $hook_extra, $result ) {
|
||||
global $wp_filesystem; // Get global FS object
|
||||
|
||||
$install_directory = plugin_dir_path( $this->file ); // Our plugin directory
|
||||
$wp_filesystem->move( $result['destination'], $install_directory ); // Move files to the plugin dir
|
||||
$result['destination'] = $install_directory; // Set the destination for the rest of the stack
|
||||
|
||||
if ( $this->active ) { // If it was active
|
||||
activate_plugin( $this->basename ); // Reactivate
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user