Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a1a40478a7 | |||
| aab0d39ec8 | |||
| 93a42b29bb | |||
| 87c398a7bb | |||
| 22aee0fa64 | |||
| b71af26223 | |||
| d5babe6cae | |||
| e7e757f0cd | |||
| 08551e0013 | |||
| 03fdf89442 | |||
| 97ca8c4015 | |||
| 6ededb139b | |||
| 124f7b4e94 | |||
| de87021339 | |||
| ee43e18b25 |
24
.gitea/workflows/release.yml
Normal file
24
.gitea/workflows/release.yml
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: Build & Release Plugin
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
release_notes:
|
||||||
|
description: "Optionele release-opmerkingen"
|
||||||
|
required: false
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
paths:
|
||||||
|
- 'siti-stock-plugin.php'
|
||||||
|
- 'includes/**'
|
||||||
|
- 'assets/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
uses: roberto/ci-workflows/.gitea/workflows/wp-plugin-release.yml@c6393ed47258d6f040ceeed3994b17b7faa3ef23
|
||||||
|
with:
|
||||||
|
main_file: siti-stock-plugin.php
|
||||||
|
slug: siti-stock-plugin
|
||||||
|
release_body: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.release_notes || '' }}
|
||||||
|
secrets:
|
||||||
|
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
|
||||||
102
.github/workflows/release.yml
vendored
102
.github/workflows/release.yml
vendored
@@ -1,102 +0,0 @@
|
|||||||
name: Build & Release Plugin
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
release_notes:
|
|
||||||
description: 'Optionele release-opmerkingen'
|
|
||||||
required: false
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
paths:
|
|
||||||
- 'siti-stock-plugin.php'
|
|
||||||
- 'includes/**'
|
|
||||||
- 'assets/**'
|
|
||||||
- 'README.md'
|
|
||||||
- '.github/workflows/release.yml'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
release:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Determine plugin version
|
|
||||||
id: meta
|
|
||||||
run: |
|
|
||||||
VERSION=$(grep -E "^\\s*\\*\\s*Version:" -m 1 siti-stock-plugin.php | sed -E 's/.*Version:\\s*//')
|
|
||||||
VERSION=$(echo "$VERSION" | tr -d '\\r')
|
|
||||||
if [ -z "$VERSION" ]; then
|
|
||||||
echo "::error::Kon pluginversie niet bepalen."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
- name: Check if tag exists
|
|
||||||
id: tagcheck
|
|
||||||
run: |
|
|
||||||
TAG="v${{ steps.meta.outputs.version }}"
|
|
||||||
if git rev-parse "$TAG" >/dev/null 2>&1; then
|
|
||||||
echo "exists=true" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "exists=false" >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Tag bestaat al – workflow afronden
|
|
||||||
if: steps.tagcheck.outputs.exists == 'true'
|
|
||||||
run: |
|
|
||||||
echo "Tag v${{ steps.meta.outputs.version }} bestaat al. Release wordt overgeslagen."
|
|
||||||
|
|
||||||
- name: Build distributie-zip
|
|
||||||
if: steps.tagcheck.outputs.exists == 'false'
|
|
||||||
id: package
|
|
||||||
run: |
|
|
||||||
VERSION="${{ steps.meta.outputs.version }}"
|
|
||||||
SLUG="siti-stock-plugin"
|
|
||||||
BUILD_ROOT="$RUNNER_TEMP/build"
|
|
||||||
DEST_DIR="$BUILD_ROOT/$SLUG"
|
|
||||||
mkdir -p "$DEST_DIR"
|
|
||||||
|
|
||||||
rsync -a ./ "$DEST_DIR" \
|
|
||||||
--exclude '.git/' \
|
|
||||||
--exclude '.github/' \
|
|
||||||
--exclude 'docker/' \
|
|
||||||
--exclude 'docs/' \
|
|
||||||
--exclude 'dist/' \
|
|
||||||
--exclude 'docker-compose.yml' \
|
|
||||||
--exclude 'PLAN.md'
|
|
||||||
|
|
||||||
mkdir -p dist
|
|
||||||
ZIP_PATH="dist/${SLUG}-${VERSION}.zip"
|
|
||||||
(cd "$BUILD_ROOT" && zip -r "$GITHUB_WORKSPACE/$ZIP_PATH" "$SLUG")
|
|
||||||
|
|
||||||
echo "asset_path=$ZIP_PATH" >> "$GITHUB_OUTPUT"
|
|
||||||
echo "asset_name=${SLUG}-${VERSION}.zip" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
- name: Stel release-body samen
|
|
||||||
if: steps.tagcheck.outputs.exists == 'false'
|
|
||||||
id: releasebody
|
|
||||||
run: |
|
|
||||||
if [ -n "$RELEASE_NOTES" ]; then
|
|
||||||
echo "text=$RELEASE_NOTES" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "text=Automatische release op basis van versie ${{ steps.meta.outputs.version }}." >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
env:
|
|
||||||
RELEASE_NOTES: ${{ github.event.inputs.release_notes }}
|
|
||||||
|
|
||||||
- name: Maak GitHub release
|
|
||||||
if: steps.tagcheck.outputs.exists == 'false'
|
|
||||||
uses: softprops/action-gh-release@v2
|
|
||||||
with:
|
|
||||||
tag_name: v${{ steps.meta.outputs.version }}
|
|
||||||
name: Siti Stock Plugin v${{ steps.meta.outputs.version }}
|
|
||||||
body: ${{ steps.releasebody.outputs.text }}
|
|
||||||
generate_release_notes: true
|
|
||||||
files: ${{ steps.package.outputs.asset_path }}
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
181
includes/SitiLicenseValidator.php
Normal file
181
includes/SitiLicenseValidator.php
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Siti License Validator
|
||||||
|
*
|
||||||
|
* Validates licenses against the Siti Plugin Repo API at plugins.robert.ooo
|
||||||
|
*/
|
||||||
|
class SitiLicenseValidator {
|
||||||
|
|
||||||
|
private $api_base_url = 'https://plugins.robert.ooo';
|
||||||
|
private $license_key;
|
||||||
|
private $hostname;
|
||||||
|
private $plugin_version;
|
||||||
|
|
||||||
|
public function __construct( $license_key = null, $hostname = null, $plugin_version = null ) {
|
||||||
|
$this->license_key = $license_key ?: get_option( 'siti_license_key' );
|
||||||
|
$this->hostname = $hostname ?: parse_url( home_url(), PHP_URL_HOST );
|
||||||
|
$this->plugin_version = $plugin_version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the license key
|
||||||
|
*/
|
||||||
|
public function set_license_key( $key ) {
|
||||||
|
$this->license_key = $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the API base URL
|
||||||
|
*/
|
||||||
|
public function set_api_base_url( $url ) {
|
||||||
|
$this->api_base_url = rtrim( $url, '/' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the hostname
|
||||||
|
*/
|
||||||
|
public function set_hostname( $hostname ) {
|
||||||
|
$this->hostname = $hostname;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the plugin version for reporting purposes
|
||||||
|
*/
|
||||||
|
public function set_plugin_version( $version ) {
|
||||||
|
$this->plugin_version = $version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify the license
|
||||||
|
*
|
||||||
|
* @return array|WP_Error License data on success, WP_Error on failure
|
||||||
|
*/
|
||||||
|
public function verify() {
|
||||||
|
if ( empty( $this->license_key ) ) {
|
||||||
|
return new WP_Error( 'missing_license', 'Geen licentiecode ingesteld.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( empty( $this->hostname ) ) {
|
||||||
|
return new WP_Error( 'missing_hostname', 'Kon hostname niet bepalen.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = wp_remote_post( $this->api_base_url . '/api/licenses/verify', array(
|
||||||
|
'headers' => array(
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
),
|
||||||
|
'body' => wp_json_encode( array(
|
||||||
|
'key' => $this->license_key,
|
||||||
|
'hostname' => $this->hostname,
|
||||||
|
'currentVersion' => $this->plugin_version,
|
||||||
|
) ),
|
||||||
|
'timeout' => 15,
|
||||||
|
) );
|
||||||
|
|
||||||
|
if ( is_wp_error( $response ) ) {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
$code = wp_remote_retrieve_response_code( $response );
|
||||||
|
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||||
|
|
||||||
|
if ( ! is_array( $body ) ) {
|
||||||
|
return new WP_Error( 'verification_failed', 'Onverwacht antwoord van licentieserver.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$has_payload = isset( $body['license'] ) || isset( $body['pluginVersion'] ) || isset( $body['version'] );
|
||||||
|
|
||||||
|
if ( 200 !== $code ) {
|
||||||
|
if ( $has_payload ) {
|
||||||
|
$this->store_license_payload( $body );
|
||||||
|
}
|
||||||
|
|
||||||
|
$error_message = isset( $body['error'] ) ? $body['error'] : 'Licentie verificatie mislukt.';
|
||||||
|
return new WP_Error( 'verification_failed', $error_message );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( empty( $body['valid'] ) ) {
|
||||||
|
if ( $has_payload ) {
|
||||||
|
$this->store_license_payload( $body );
|
||||||
|
}
|
||||||
|
|
||||||
|
return new WP_Error( 'invalid_license', 'Licentie is ongeldig.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->store_license_payload( $body );
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get stored license data
|
||||||
|
*/
|
||||||
|
public function get_license_data() {
|
||||||
|
return get_option( 'siti_license_data', array() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if license is valid (cached)
|
||||||
|
*/
|
||||||
|
public function is_valid() {
|
||||||
|
$data = $this->get_license_data();
|
||||||
|
return ! empty( $data ) && isset( $data['key'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the latest version from license data
|
||||||
|
*/
|
||||||
|
public function get_latest_version() {
|
||||||
|
$data = $this->get_license_data();
|
||||||
|
return $data['pluginVersion'] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download a specific version
|
||||||
|
*
|
||||||
|
* @param string $version Version to download (e.g., 'v1.2.3' or 'latest')
|
||||||
|
* @return string|WP_Error Download URL or error
|
||||||
|
*/
|
||||||
|
public function get_download_url( $version = 'latest' ) {
|
||||||
|
if ( empty( $this->license_key ) || empty( $this->hostname ) ) {
|
||||||
|
return new WP_Error( 'missing_credentials', 'Licentie of hostname ontbreekt.' );
|
||||||
|
}
|
||||||
|
|
||||||
|
// For now, return the API download endpoint
|
||||||
|
// In practice, you might want to proxy this through WordPress
|
||||||
|
return $this->api_base_url . '/api/licenses/download?key=' . urlencode( $this->license_key ) . '&hostname=' . urlencode( $this->hostname ) . '&version=' . urlencode( $version );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persist payload data locally for UI purposes
|
||||||
|
*/
|
||||||
|
private function store_license_payload( $body ) {
|
||||||
|
$license_data = array();
|
||||||
|
|
||||||
|
if ( isset( $body['license'] ) && is_array( $body['license'] ) ) {
|
||||||
|
$license_data = $body['license'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( empty( $license_data['key'] ) ) {
|
||||||
|
$license_data['key'] = $this->license_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
$version = null;
|
||||||
|
if ( ! empty( $body['pluginVersion'] ) ) {
|
||||||
|
$version = $body['pluginVersion'];
|
||||||
|
} elseif ( ! empty( $body['version'] ) ) {
|
||||||
|
$version = $body['version'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $version ) {
|
||||||
|
$license_data['pluginVersion'] = $version;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( empty( $license_data ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_option( 'siti_license_data', $license_data );
|
||||||
|
update_option( 'siti_license_last_check', current_time( 'mysql' ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
510
includes/SitiWebUpdater2.php
Normal file
510
includes/SitiWebUpdater2.php
Normal file
@@ -0,0 +1,510 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SitiWebUpdater2
|
||||||
|
*
|
||||||
|
* Updates plugins via the Siti Plugin Repo API instead of GitHub
|
||||||
|
* Fully self-contained with built-in license management
|
||||||
|
*/
|
||||||
|
class SitiWebUpdater2
|
||||||
|
{
|
||||||
|
|
||||||
|
private static $instances = array();
|
||||||
|
private static $global_settings_registered = false;
|
||||||
|
|
||||||
|
private $file;
|
||||||
|
private $plugin;
|
||||||
|
private $basename;
|
||||||
|
private $active;
|
||||||
|
private $owner;
|
||||||
|
private $repository;
|
||||||
|
private $license_key;
|
||||||
|
private $api_base_url = 'https://plugins.robert.ooo';
|
||||||
|
private $plugin_response;
|
||||||
|
private $option_prefix;
|
||||||
|
|
||||||
|
public function __construct($file)
|
||||||
|
{
|
||||||
|
$this->file = $file;
|
||||||
|
$this->option_prefix = 'siti_updater_' . sanitize_key(dirname(plugin_basename($this->file))) . '_';
|
||||||
|
$this->set_plugin_properties();
|
||||||
|
|
||||||
|
// Register this instance globally
|
||||||
|
self::$instances[$this->option_prefix] = $this;
|
||||||
|
|
||||||
|
add_action('admin_init', array($this, 'set_plugin_properties'));
|
||||||
|
add_action('admin_notices', array($this, 'admin_notices'));
|
||||||
|
add_action('siti_updater_daily_check_' . $this->option_prefix, array($this, 'daily_license_check'));
|
||||||
|
|
||||||
|
if (!wp_next_scheduled('siti_updater_daily_check_' . $this->option_prefix)) {
|
||||||
|
wp_schedule_event(time(), 'daily', 'siti_updater_daily_check_' . $this->option_prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initialize()
|
||||||
|
{
|
||||||
|
add_filter('pre_set_site_transient_update_plugins', array($this, 'check_for_updates'));
|
||||||
|
add_filter('plugins_api', array($this, 'plugin_info'), 10, 3);
|
||||||
|
|
||||||
|
// Register global settings page only once
|
||||||
|
if (!self::$global_settings_registered) {
|
||||||
|
add_action('admin_menu', array($this, 'add_global_settings_page'));
|
||||||
|
self::$global_settings_registered = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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, false, false);
|
||||||
|
$this->basename = plugin_basename($this->file);
|
||||||
|
$this->active = is_plugin_active($this->basename);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_instances()
|
||||||
|
{
|
||||||
|
return self::$instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_plugin_data()
|
||||||
|
{
|
||||||
|
return $this->plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_owner($owner)
|
||||||
|
{
|
||||||
|
$this->owner = $owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_repository($repository)
|
||||||
|
{
|
||||||
|
$this->repository = $repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_license_key($key)
|
||||||
|
{
|
||||||
|
$sanitized_key = sanitize_text_field((string) $key);
|
||||||
|
$stored_key = get_option($this->option_prefix . 'license_key', '');
|
||||||
|
|
||||||
|
if ($stored_key !== $sanitized_key) {
|
||||||
|
$this->clear_cached_license_data();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->license_key = $sanitized_key;
|
||||||
|
update_option($this->option_prefix . 'license_key', $sanitized_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_license_key()
|
||||||
|
{
|
||||||
|
if (null === $this->license_key) {
|
||||||
|
$this->license_key = get_option($this->option_prefix . 'license_key', '');
|
||||||
|
}
|
||||||
|
return $this->license_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_license_data()
|
||||||
|
{
|
||||||
|
return $this->get_stored_license_data();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set_api_base_url($api_base_url)
|
||||||
|
{
|
||||||
|
$this->api_base_url = rtrim((string) $api_base_url, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function normalize_version($version)
|
||||||
|
{
|
||||||
|
if (!is_string($version)) {
|
||||||
|
return $version;
|
||||||
|
}
|
||||||
|
|
||||||
|
$version = trim($version);
|
||||||
|
if ('' === $version) {
|
||||||
|
return $version;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 === stripos($version, 'v')) {
|
||||||
|
$version = ltrim($version, 'vV');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $version;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_stored_license_data()
|
||||||
|
{
|
||||||
|
$data = get_option($this->option_prefix . 'license_data', array());
|
||||||
|
|
||||||
|
return is_array($data) ? $data : array();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function clear_cached_license_data()
|
||||||
|
{
|
||||||
|
delete_option($this->option_prefix . 'license_data');
|
||||||
|
delete_option($this->option_prefix . 'last_check');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function store_license_data($body)
|
||||||
|
{
|
||||||
|
if (!is_array($body)) {
|
||||||
|
$body = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$license_data = array();
|
||||||
|
|
||||||
|
if (isset($body['license']) && is_array($body['license'])) {
|
||||||
|
$license_data = $body['license'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($license_data['key'])) {
|
||||||
|
$license_data['key'] = $this->get_license_key();
|
||||||
|
}
|
||||||
|
|
||||||
|
$license_data['licenseValid'] = !empty($body['valid']);
|
||||||
|
|
||||||
|
$version = null;
|
||||||
|
if (isset($body['pluginVersion']) && $body['pluginVersion']) {
|
||||||
|
$version = $body['pluginVersion'];
|
||||||
|
} elseif (isset($body['version']) && $body['version']) {
|
||||||
|
$version = $body['version'];
|
||||||
|
} elseif (isset($license_data['pluginVersion']) && $license_data['pluginVersion']) {
|
||||||
|
$version = $license_data['pluginVersion'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($version) {
|
||||||
|
$license_data['pluginVersion'] = $this->normalize_version($version);
|
||||||
|
}
|
||||||
|
|
||||||
|
update_option($this->option_prefix . 'license_data', $license_data);
|
||||||
|
update_option($this->option_prefix . 'last_check', current_time('mysql'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_plugin_info()
|
||||||
|
{
|
||||||
|
if (is_null($this->plugin_response)) {
|
||||||
|
$request_uri = sprintf('%s/api/plugins/%s/%s', $this->api_base_url, $this->owner, $this->repository);
|
||||||
|
|
||||||
|
$response = wp_remote_get($request_uri, array('timeout' => 15));
|
||||||
|
|
||||||
|
if (is_wp_error($response)) {
|
||||||
|
$this->plugin_response = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$code = wp_remote_retrieve_response_code($response);
|
||||||
|
if (200 !== $code) {
|
||||||
|
$this->plugin_response = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = json_decode(wp_remote_retrieve_body($response), true);
|
||||||
|
if (is_array($body) && isset($body['version'])) {
|
||||||
|
$body['version'] = $this->normalize_version($body['version']);
|
||||||
|
}
|
||||||
|
$this->plugin_response = $body;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function check_for_updates($transient)
|
||||||
|
{
|
||||||
|
if (empty($transient->checked)) {
|
||||||
|
return $transient;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->get_plugin_info();
|
||||||
|
|
||||||
|
if (false === $this->plugin_response || empty($this->plugin_response['version'])) {
|
||||||
|
return $transient;
|
||||||
|
}
|
||||||
|
|
||||||
|
$remote_version = $this->normalize_version($this->plugin_response['version']);
|
||||||
|
$current_version = $this->normalize_version($this->plugin['Version']);
|
||||||
|
|
||||||
|
if ($remote_version && version_compare($remote_version, $current_version, '>')) {
|
||||||
|
$download_url = $this->get_download_url('latest');
|
||||||
|
|
||||||
|
if (!is_wp_error($download_url)) {
|
||||||
|
$transient->response[$this->basename] = (object) array(
|
||||||
|
'slug' => dirname($this->basename),
|
||||||
|
'new_version' => $remote_version,
|
||||||
|
'package' => $download_url,
|
||||||
|
'tested' => get_bloginfo('version'),
|
||||||
|
'url' => $this->plugin_response['homepage'] ?? '',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $transient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function plugin_info($result, $action, $args)
|
||||||
|
{
|
||||||
|
if ('plugin_information' !== $action || $args->slug !== dirname($this->basename)) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->get_plugin_info();
|
||||||
|
|
||||||
|
if (false === $this->plugin_response) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (object) array(
|
||||||
|
'name' => $this->plugin_response['name'] ?? $this->plugin['Name'],
|
||||||
|
'slug' => $args->slug,
|
||||||
|
'version' => $this->normalize_version($this->plugin_response['version'] ?? $this->plugin['Version']),
|
||||||
|
'author' => $this->plugin_response['author'] ?? $this->plugin['Author'],
|
||||||
|
'author_profile' => $this->plugin_response['author_url'] ?? $this->plugin['AuthorURI'],
|
||||||
|
'homepage' => $this->plugin_response['homepage'] ?? '',
|
||||||
|
'requires' => $this->plugin_response['requires'] ?? '',
|
||||||
|
'tested' => $this->plugin_response['tested'] ?? get_bloginfo('version'),
|
||||||
|
'downloaded' => $this->plugin_response['downloads'] ?? 0,
|
||||||
|
'last_updated' => $this->plugin_response['last_updated'] ?? '',
|
||||||
|
'sections' => array(
|
||||||
|
'description' => $this->plugin_response['description'] ?? $this->plugin['Description'],
|
||||||
|
'changelog' => $this->plugin_response['changelog'] ?? '',
|
||||||
|
),
|
||||||
|
'download_link' => $this->get_download_url('latest'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function get_download_url($version = 'latest')
|
||||||
|
{
|
||||||
|
$license_key = $this->get_license_key();
|
||||||
|
if (empty($license_key)) {
|
||||||
|
return new WP_Error('no_license', 'Geen licentie ingesteld');
|
||||||
|
}
|
||||||
|
|
||||||
|
$hostname = parse_url(home_url(), PHP_URL_HOST);
|
||||||
|
return $this->api_base_url . '/api/licenses/download?key=' . urlencode($license_key) . '&hostname=' . urlencode($hostname) . '&version=' . urlencode($version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verify_license()
|
||||||
|
{
|
||||||
|
$license_key = $this->get_license_key();
|
||||||
|
if (empty($license_key)) {
|
||||||
|
return new WP_Error('missing_license', 'Geen licentiecode ingesteld.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$hostname = parse_url(home_url(), PHP_URL_HOST);
|
||||||
|
$current_version = isset($this->plugin['Version']) ? $this->plugin['Version'] : null;
|
||||||
|
$response = wp_remote_post($this->api_base_url . '/api/licenses/verify', array(
|
||||||
|
'headers' => array(
|
||||||
|
'Content-Type' => 'application/json',
|
||||||
|
),
|
||||||
|
'body' => wp_json_encode(array(
|
||||||
|
'key' => $license_key,
|
||||||
|
'hostname' => $hostname,
|
||||||
|
'currentVersion' => $current_version,
|
||||||
|
'owner' => $this->owner,
|
||||||
|
'repository' => $this->repository,
|
||||||
|
)),
|
||||||
|
'timeout' => 15,
|
||||||
|
));
|
||||||
|
|
||||||
|
if (is_wp_error($response)) {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
$code = wp_remote_retrieve_response_code($response);
|
||||||
|
$body = json_decode(wp_remote_retrieve_body($response), true);
|
||||||
|
|
||||||
|
if (!is_array($body)) {
|
||||||
|
$this->clear_cached_license_data();
|
||||||
|
return new WP_Error('verification_failed', 'Onverwacht antwoord van licentieserver.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$has_license_payload = isset($body['license']) || isset($body['pluginVersion']) || isset($body['version']);
|
||||||
|
|
||||||
|
if (200 !== $code) {
|
||||||
|
if ($has_license_payload) {
|
||||||
|
$this->store_license_data($body);
|
||||||
|
} else {
|
||||||
|
$this->clear_cached_license_data();
|
||||||
|
}
|
||||||
|
|
||||||
|
$error_message = isset($body['error']) ? $body['error'] : 'Licentie verificatie mislukt.';
|
||||||
|
return new WP_Error('verification_failed', $error_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($body['valid'])) {
|
||||||
|
if ($has_license_payload) {
|
||||||
|
$this->store_license_data($body);
|
||||||
|
} else {
|
||||||
|
$this->clear_cached_license_data();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new WP_Error('invalid_license', 'Licentie is ongeldig.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->store_license_data($body);
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function is_license_valid()
|
||||||
|
{
|
||||||
|
$data = $this->get_license_data();
|
||||||
|
if (empty($data) || empty($data['key'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['licenseValid'])) {
|
||||||
|
return (bool) $data['licenseValid'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_latest_known_version()
|
||||||
|
{
|
||||||
|
$this->get_plugin_info();
|
||||||
|
|
||||||
|
if (!empty($this->plugin_response['version'])) {
|
||||||
|
return $this->normalize_version($this->plugin_response['version']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$license_data = $this->get_license_data();
|
||||||
|
if (isset($license_data['pluginVersion']) && $license_data['pluginVersion']) {
|
||||||
|
return $this->normalize_version($license_data['pluginVersion']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function admin_notices()
|
||||||
|
{
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->is_license_valid()) {
|
||||||
|
$settings_url = admin_url('options-general.php?page=siti-plugin-licenses');
|
||||||
|
echo '<div class="notice notice-error"><p>';
|
||||||
|
printf(
|
||||||
|
esc_html__('%s: Licentie ongeldig of niet ingesteld. Ga naar %s om je licentie in te voeren.', 'siti-updater'),
|
||||||
|
esc_html($this->plugin['Name']),
|
||||||
|
'<a href="' . esc_url($settings_url) . '">' . esc_html__('plugin licenties', 'siti-updater') . '</a>'
|
||||||
|
);
|
||||||
|
echo '</p></div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function daily_license_check()
|
||||||
|
{
|
||||||
|
$result = $this->verify_license();
|
||||||
|
if (is_wp_error($result)) {
|
||||||
|
error_log($this->plugin['Name'] . ' License check failed: ' . $result->get_error_message());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add_global_settings_page()
|
||||||
|
{
|
||||||
|
add_options_page(
|
||||||
|
__('Plugin Licenties', 'siti-updater'),
|
||||||
|
__('Plugin Licenties', 'siti-updater'),
|
||||||
|
'manage_options',
|
||||||
|
'siti-plugin-licenses',
|
||||||
|
array($this, 'render_global_settings_page')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render_global_settings_page()
|
||||||
|
{
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($_POST['siti_updater_save']) && wp_verify_nonce($_POST['_wpnonce'], 'siti_updater_save')) {
|
||||||
|
foreach (self::$instances as $prefix => $instance) {
|
||||||
|
$key = $prefix . 'license_key';
|
||||||
|
if (isset($_POST[$key])) {
|
||||||
|
$instance->set_license_key(sanitize_text_field($_POST[$key]));
|
||||||
|
$result = $instance->verify_license();
|
||||||
|
if (is_wp_error($result)) {
|
||||||
|
$plugin_data = $instance->get_plugin_data();
|
||||||
|
add_settings_error('siti_updater', 'license_error_' . $prefix, $plugin_data['Name'] . ': ' . $result->get_error_message());
|
||||||
|
} else {
|
||||||
|
$plugin_data = $instance->get_plugin_data();
|
||||||
|
add_settings_error('siti_updater', 'license_success_' . $prefix, $plugin_data['Name'] . ': ' . __('Licentie succesvol opgeslagen en geverifieerd.', 'siti-updater'), 'updated');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
<div class="wrap">
|
||||||
|
<h1><?php esc_html_e('Plugin Licenties', 'siti-updater'); ?></h1>
|
||||||
|
<p class="description">
|
||||||
|
<?php esc_html_e('Beheer licentiecodes voor al je plugins die het Siti update systeem gebruiken.', 'siti-updater'); ?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?php settings_errors('siti_updater'); ?>
|
||||||
|
|
||||||
|
<form method="post" action="">
|
||||||
|
<?php wp_nonce_field('siti_updater_save'); ?>
|
||||||
|
<table class="wp-list-table widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php esc_html_e('Plugin', 'siti-updater'); ?></th>
|
||||||
|
<th><?php esc_html_e('Licentiecode', 'siti-updater'); ?></th>
|
||||||
|
<th><?php esc_html_e('Geldig', 'siti-updater'); ?></th>
|
||||||
|
<th><?php esc_html_e('Huidige versie', 'siti-updater'); ?></th>
|
||||||
|
<th><?php esc_html_e('Nieuwste versie', 'siti-updater'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach (self::$instances as $prefix => $instance):
|
||||||
|
$license_data = $instance->get_license_data();
|
||||||
|
$is_valid = $instance->is_license_valid();
|
||||||
|
$plugin_data = $instance->get_plugin_data();
|
||||||
|
$current_version = $plugin_data['Version'];
|
||||||
|
$latest_known = $instance->get_latest_known_version();
|
||||||
|
$latest_version = $latest_known ? $latest_known : (isset($license_data['pluginVersion']) ? $license_data['pluginVersion'] : '-');
|
||||||
|
?>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<strong><?php echo esc_html($plugin_data['Name']); ?></strong>
|
||||||
|
<br>
|
||||||
|
<small><?php echo esc_html($plugin_data['Description']); ?></small>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input type="text" name="<?php echo esc_attr($prefix . 'license_key'); ?>"
|
||||||
|
value="<?php echo esc_attr($instance->get_license_key()); ?>" class="regular-text"
|
||||||
|
placeholder="<?php esc_attr_e('Voer licentiecode in...', 'siti-updater'); ?>" />
|
||||||
|
<p class="description">
|
||||||
|
<?php printf(esc_html__('Licentie voor %s. Verkrijg je code van plugins.robert.ooo', 'siti-updater'), $plugin_data['Name']); ?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span class="license-status <?php echo $is_valid ? 'valid' : 'invalid'; ?>">
|
||||||
|
<?php echo $is_valid ? '<span style="color: green;">✓ ' . esc_html__('Ja', 'siti-updater') . '</span>' : '<span style="color: red;">✗ ' . esc_html__('Nee', 'siti-updater') . '</span>'; ?>
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<?php echo esc_html($current_version); ?>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<?php echo esc_html($latest_version); ?>
|
||||||
|
<?php if ($latest_version && '-' !== $latest_version && version_compare($latest_version, $current_version, '>')): ?>
|
||||||
|
<br><small
|
||||||
|
style="color: orange;"><?php esc_html_e('Update beschikbaar!', 'siti-updater'); ?></small>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<p class="submit">
|
||||||
|
<input type="submit" name="siti_updater_save" class="button button-primary"
|
||||||
|
value="<?php esc_attr_e('Licenties opslaan', 'siti-updater'); ?>" />
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,11 @@ class Siti_Stock_Admin {
|
|||||||
*/
|
*/
|
||||||
private $notices;
|
private $notices;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Siti_Stock_Supplier_Exports
|
||||||
|
*/
|
||||||
|
private $supplier_exports;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cached settings snapshot for rendering.
|
* Cached settings snapshot for rendering.
|
||||||
*
|
*
|
||||||
@@ -35,11 +40,13 @@ class Siti_Stock_Admin {
|
|||||||
* @param Siti_Stock_Settings $settings Settings repository.
|
* @param Siti_Stock_Settings $settings Settings repository.
|
||||||
* @param Siti_Stock_Sync_Controller $sync_controller Sync controller.
|
* @param Siti_Stock_Sync_Controller $sync_controller Sync controller.
|
||||||
* @param Siti_Stock_Admin_Notices $notices Notice handler.
|
* @param Siti_Stock_Admin_Notices $notices Notice handler.
|
||||||
|
* @param Siti_Stock_Supplier_Exports $supplier_exports Supplier exports handler.
|
||||||
*/
|
*/
|
||||||
public function __construct( Siti_Stock_Settings $settings, Siti_Stock_Sync_Controller $sync_controller, Siti_Stock_Admin_Notices $notices ) {
|
public function __construct( Siti_Stock_Settings $settings, Siti_Stock_Sync_Controller $sync_controller, Siti_Stock_Admin_Notices $notices, Siti_Stock_Supplier_Exports $supplier_exports ) {
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->sync_controller = $sync_controller;
|
$this->sync_controller = $sync_controller;
|
||||||
$this->notices = $notices;
|
$this->notices = $notices;
|
||||||
|
$this->supplier_exports = $supplier_exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,6 +167,15 @@ class Siti_Stock_Admin {
|
|||||||
array( $this, 'render_settings_page' ),
|
array( $this, 'render_settings_page' ),
|
||||||
'dashicons-products'
|
'dashicons-products'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_submenu_page(
|
||||||
|
'siti-stock-plugin',
|
||||||
|
__( 'Stock updates', 'siti-stock-plugin' ),
|
||||||
|
__( 'Stock updates', 'siti-stock-plugin' ),
|
||||||
|
'manage_options',
|
||||||
|
'siti-stock-plugin-stock-updates',
|
||||||
|
array( $this, 'render_stock_updates_page' )
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -168,7 +184,12 @@ class Siti_Stock_Admin {
|
|||||||
* @param string $hook Hook suffix.
|
* @param string $hook Hook suffix.
|
||||||
*/
|
*/
|
||||||
public function enqueue_admin_assets( $hook ) {
|
public function enqueue_admin_assets( $hook ) {
|
||||||
if ( 'toplevel_page_siti-stock-plugin' !== $hook ) {
|
$pages = array(
|
||||||
|
'toplevel_page_siti-stock-plugin',
|
||||||
|
'siti-stock-plugin_page_siti-stock-plugin-stock-updates',
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( ! in_array( $hook, $pages, true ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,6 +262,112 @@ class Siti_Stock_Admin {
|
|||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render stock updates admin page.
|
||||||
|
*/
|
||||||
|
public function render_stock_updates_page() {
|
||||||
|
if ( ! current_user_can( 'manage_options' ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$suppliers = $this->supplier_exports->get_supplier_configs();
|
||||||
|
$admin_email = get_option( 'admin_email' );
|
||||||
|
?>
|
||||||
|
<div class="wrap siti-stock-settings">
|
||||||
|
<h1><?php esc_html_e( 'Stock updates', 'siti-stock-plugin' ); ?></h1>
|
||||||
|
<p class="description">
|
||||||
|
<?php esc_html_e( 'Plan automatische e-mails met de bestellingen per leverancier in CSV-formaat.', 'siti-stock-plugin' ); ?>
|
||||||
|
<?php
|
||||||
|
if ( $admin_email ) {
|
||||||
|
printf(
|
||||||
|
/* translators: %s admin email */
|
||||||
|
esc_html__( 'E-mails worden verzonden naar %s.', 'siti-stock-plugin' ),
|
||||||
|
esc_html( $admin_email )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?php if ( empty( $suppliers ) ) : ?>
|
||||||
|
<p><?php esc_html_e( 'Er zijn momenteel geen leveranciers gevonden.', 'siti-stock-plugin' ); ?></p>
|
||||||
|
<?php else : ?>
|
||||||
|
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
||||||
|
<?php wp_nonce_field( 'siti_stock_save_supplier_schedule' ); ?>
|
||||||
|
<input type="hidden" name="action" value="siti_stock_save_supplier_schedule" />
|
||||||
|
|
||||||
|
<table class="widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php esc_html_e( 'Leverancier', 'siti-stock-plugin' ); ?></th>
|
||||||
|
<th><?php esc_html_e( 'Tijd (24u)', 'siti-stock-plugin' ); ?></th>
|
||||||
|
<th><?php esc_html_e( 'Automatisch verzenden', 'siti-stock-plugin' ); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ( $suppliers as $key => $config ) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html( isset( $config['supplier'] ) ? $config['supplier'] : $key ); ?></td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="time"
|
||||||
|
name="<?php echo esc_attr( 'supplier_exports[' . $key . '][time]' ); ?>"
|
||||||
|
value="<?php echo esc_attr( isset( $config['time'] ) ? $config['time'] : '09:00' ); ?>"
|
||||||
|
step="60"
|
||||||
|
class="regular-text"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<label>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="<?php echo esc_attr( 'supplier_exports[' . $key . '][enabled]' ); ?>"
|
||||||
|
value="1"
|
||||||
|
<?php checked( ! empty( $config['enabled'] ) ); ?>
|
||||||
|
/>
|
||||||
|
<?php esc_html_e( 'Activeer e-mail', 'siti-stock-plugin' ); ?>
|
||||||
|
</label>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<?php submit_button( __( 'Instellingen opslaan', 'siti-stock-plugin' ) ); ?>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<h2><?php esc_html_e( 'Handmatig versturen', 'siti-stock-plugin' ); ?></h2>
|
||||||
|
<p class="description"><?php esc_html_e( 'Gebruik deze knoppen om direct een export te versturen, ook als de automatische taak uit staat.', 'siti-stock-plugin' ); ?></p>
|
||||||
|
|
||||||
|
<table class="widefat fixed striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php esc_html_e( 'Leverancier', 'siti-stock-plugin' ); ?></th>
|
||||||
|
<th><?php esc_html_e( 'Geplande tijd', 'siti-stock-plugin' ); ?></th>
|
||||||
|
<th><?php esc_html_e( 'Actie', 'siti-stock-plugin' ); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php foreach ( $suppliers as $key => $config ) : ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo esc_html( isset( $config['supplier'] ) ? $config['supplier'] : $key ); ?></td>
|
||||||
|
<td><?php echo esc_html( isset( $config['time'] ) ? $config['time'] : '00:00' ); ?></td>
|
||||||
|
<td>
|
||||||
|
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
||||||
|
<?php wp_nonce_field( 'siti_stock_send_supplier_export' ); ?>
|
||||||
|
<input type="hidden" name="action" value="siti_stock_send_supplier_export" />
|
||||||
|
<input type="hidden" name="supplier_key" value="<?php echo esc_attr( $key ); ?>" />
|
||||||
|
<?php submit_button( __( 'Verstuur nu', 'siti-stock-plugin' ), 'secondary', 'submit', false ); ?>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a text/password input.
|
* Render a text/password input.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ require_once __DIR__ . '/class-siti-stock-admin.php';
|
|||||||
require_once __DIR__ . '/class-siti-stock-inventory-manager.php';
|
require_once __DIR__ . '/class-siti-stock-inventory-manager.php';
|
||||||
require_once __DIR__ . '/class-siti-stock-sync-service.php';
|
require_once __DIR__ . '/class-siti-stock-sync-service.php';
|
||||||
require_once __DIR__ . '/class-siti-stock-sync-controller.php';
|
require_once __DIR__ . '/class-siti-stock-sync-controller.php';
|
||||||
|
require_once __DIR__ . '/class-siti-stock-supplier-exports.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core plugin bootstrap that wires together all sub-components.
|
* Core plugin bootstrap that wires together all sub-components.
|
||||||
@@ -50,6 +51,11 @@ class Siti_Stock_Plugin {
|
|||||||
*/
|
*/
|
||||||
private $sync_controller;
|
private $sync_controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Siti_Stock_Supplier_Exports
|
||||||
|
*/
|
||||||
|
private $supplier_exports;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get singleton instance.
|
* Get singleton instance.
|
||||||
*
|
*
|
||||||
@@ -80,6 +86,10 @@ class Siti_Stock_Plugin {
|
|||||||
|
|
||||||
$settings_repo = new Siti_Stock_Settings( self::OPTION_KEY );
|
$settings_repo = new Siti_Stock_Settings( self::OPTION_KEY );
|
||||||
Siti_Stock_Sync_Controller::maybe_schedule_from_settings( self::CRON_HOOK, $settings_repo->get_all() );
|
Siti_Stock_Sync_Controller::maybe_schedule_from_settings( self::CRON_HOOK, $settings_repo->get_all() );
|
||||||
|
|
||||||
|
$notices = new Siti_Stock_Admin_Notices();
|
||||||
|
$supplier_exports = new Siti_Stock_Supplier_Exports( $settings_repo, $notices );
|
||||||
|
$supplier_exports->reschedule_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,18 +97,22 @@ class Siti_Stock_Plugin {
|
|||||||
*/
|
*/
|
||||||
public static function deactivate() {
|
public static function deactivate() {
|
||||||
Siti_Stock_Sync_Controller::clear_schedule( self::CRON_HOOK );
|
Siti_Stock_Sync_Controller::clear_schedule( self::CRON_HOOK );
|
||||||
|
Siti_Stock_Supplier_Exports::clear_all_schedules();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function __construct() {
|
private function __construct() {
|
||||||
$this->settings = new Siti_Stock_Settings( self::OPTION_KEY );
|
$this->settings = new Siti_Stock_Settings( self::OPTION_KEY );
|
||||||
$this->notices = new Siti_Stock_Admin_Notices();
|
$this->notices = new Siti_Stock_Admin_Notices();
|
||||||
$this->sync_controller = new Siti_Stock_Sync_Controller( $this->settings, $this->notices, self::CRON_HOOK );
|
$this->sync_controller = new Siti_Stock_Sync_Controller( $this->settings, $this->notices, self::CRON_HOOK );
|
||||||
|
$this->supplier_exports = new Siti_Stock_Supplier_Exports( $this->settings, $this->notices );
|
||||||
$this->inventory_manager = new Siti_Stock_Inventory_Manager( self::EXTERNAL_STOCK_META_KEY );
|
$this->inventory_manager = new Siti_Stock_Inventory_Manager( self::EXTERNAL_STOCK_META_KEY );
|
||||||
$this->admin = new Siti_Stock_Admin( $this->settings, $this->sync_controller, $this->notices );
|
$this->admin = new Siti_Stock_Admin( $this->settings, $this->sync_controller, $this->notices, $this->supplier_exports );
|
||||||
|
|
||||||
$this->admin->register_hooks();
|
$this->admin->register_hooks();
|
||||||
$this->inventory_manager->register_hooks();
|
$this->inventory_manager->register_hooks();
|
||||||
$this->sync_controller->register_hooks();
|
$this->sync_controller->register_hooks();
|
||||||
|
$this->supplier_exports->register_hooks();
|
||||||
|
$this->supplier_exports->maybe_schedule_all();
|
||||||
add_filter( 'woocommerce_data_stores', array( $this, 'override_product_data_store' ), 20 );
|
add_filter( 'woocommerce_data_stores', array( $this, 'override_product_data_store' ), 20 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ class Siti_Stock_Settings {
|
|||||||
'default_status' => 'instock',
|
'default_status' => 'instock',
|
||||||
'enable_auto_sync' => false,
|
'enable_auto_sync' => false,
|
||||||
'sync_interval' => 'hourly',
|
'sync_interval' => 'hourly',
|
||||||
|
'supplier_exports' => array(),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
643
includes/class-siti-stock-supplier-exports.php
Normal file
643
includes/class-siti-stock-supplier-exports.php
Normal file
@@ -0,0 +1,643 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles scheduled supplier exports and related admin actions.
|
||||||
|
*/
|
||||||
|
class Siti_Stock_Supplier_Exports {
|
||||||
|
|
||||||
|
const CRON_HOOK = 'siti_stock_plugin_supplier_export';
|
||||||
|
const SUPPLIER_META_KEY = '_wpci_supplier';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Siti_Stock_Settings
|
||||||
|
*/
|
||||||
|
private $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Siti_Stock_Admin_Notices
|
||||||
|
*/
|
||||||
|
private $notices;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cached supplier configs.
|
||||||
|
*
|
||||||
|
* @var array<string,array<string,mixed>>|null
|
||||||
|
*/
|
||||||
|
private $supplier_cache = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Siti_Stock_Settings $settings Settings repository.
|
||||||
|
* @param Siti_Stock_Admin_Notices $notices Notice handler.
|
||||||
|
*/
|
||||||
|
public function __construct( Siti_Stock_Settings $settings, Siti_Stock_Admin_Notices $notices ) {
|
||||||
|
$this->settings = $settings;
|
||||||
|
$this->notices = $notices;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register hooks for cron and admin actions.
|
||||||
|
*/
|
||||||
|
public function register_hooks() {
|
||||||
|
add_action( self::CRON_HOOK, array( $this, 'run_scheduled_export' ), 10, 1 );
|
||||||
|
add_action( 'admin_post_siti_stock_send_supplier_export', array( $this, 'handle_manual_export' ) );
|
||||||
|
add_action( 'admin_post_siti_stock_save_supplier_schedule', array( $this, 'handle_schedule_save' ) );
|
||||||
|
add_action( 'update_option_' . $this->settings->get_option_key(), array( $this, 'handle_settings_update' ), 10, 2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger export when cron fires.
|
||||||
|
*
|
||||||
|
* @param string $supplier_key Supplier identifier.
|
||||||
|
*/
|
||||||
|
public function run_scheduled_export( $supplier_key ) {
|
||||||
|
$this->send_export( $supplier_key );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure all configured suppliers are scheduled.
|
||||||
|
*/
|
||||||
|
public function maybe_schedule_all() {
|
||||||
|
$configs = $this->get_supplier_configs();
|
||||||
|
|
||||||
|
foreach ( $configs as $key => $config ) {
|
||||||
|
if ( empty( $config['enabled'] ) || empty( $config['time'] ) ) {
|
||||||
|
$this->clear_schedule_for_supplier( $key );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! wp_next_scheduled( self::CRON_HOOK, array( $key ) ) ) {
|
||||||
|
$this->schedule_supplier( $key, $config );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear and reschedule cron hooks based on current settings.
|
||||||
|
*/
|
||||||
|
public function reschedule_all() {
|
||||||
|
wp_clear_scheduled_hook( self::CRON_HOOK );
|
||||||
|
|
||||||
|
$configs = $this->get_supplier_configs();
|
||||||
|
foreach ( $configs as $key => $config ) {
|
||||||
|
$this->schedule_supplier( $key, $config );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove scheduled events and cache.
|
||||||
|
*/
|
||||||
|
public function reset_cache() {
|
||||||
|
$this->supplier_cache = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all cron events for this feature.
|
||||||
|
*/
|
||||||
|
public static function clear_all_schedules() {
|
||||||
|
wp_clear_scheduled_hook( self::CRON_HOOK );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle manual export submission.
|
||||||
|
*/
|
||||||
|
public function handle_manual_export() {
|
||||||
|
if ( ! current_user_can( 'manage_options' ) ) {
|
||||||
|
wp_die( esc_html__( 'Onvoldoende rechten.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
check_admin_referer( 'siti_stock_send_supplier_export' );
|
||||||
|
|
||||||
|
$supplier_key = isset( $_POST['supplier_key'] ) ? sanitize_text_field( wp_unslash( $_POST['supplier_key'] ) ) : '';
|
||||||
|
$result = $this->send_export( $supplier_key );
|
||||||
|
|
||||||
|
if ( is_wp_error( $result ) ) {
|
||||||
|
$this->notices->add_notice( $result->get_error_message(), 'error' );
|
||||||
|
} else {
|
||||||
|
$this->notices->add_notice(
|
||||||
|
sprintf(
|
||||||
|
/* translators: %s supplier label */
|
||||||
|
__( 'Export voor %s verzonden.', 'siti-stock-plugin' ),
|
||||||
|
isset( $result['label'] ) ? $result['label'] : $supplier_key
|
||||||
|
),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=siti-stock-plugin-stock-updates' ) );
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle saving of schedule settings.
|
||||||
|
*/
|
||||||
|
public function handle_schedule_save() {
|
||||||
|
if ( ! current_user_can( 'manage_options' ) ) {
|
||||||
|
wp_die( esc_html__( 'Onvoldoende rechten.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
check_admin_referer( 'siti_stock_save_supplier_schedule' );
|
||||||
|
|
||||||
|
$raw_input = isset( $_POST['supplier_exports'] ) ? (array) wp_unslash( $_POST['supplier_exports'] ) : array();
|
||||||
|
$this->persist_supplier_settings( $raw_input );
|
||||||
|
$this->notices->add_notice( __( 'Voorkeuren opgeslagen.', 'siti-stock-plugin' ) );
|
||||||
|
|
||||||
|
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=siti-stock-plugin-stock-updates' ) );
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* React to option updates by refreshing cron.
|
||||||
|
*
|
||||||
|
* @param array<string,mixed> $old_value Old settings.
|
||||||
|
* @param array<string,mixed> $value New settings.
|
||||||
|
*/
|
||||||
|
public function handle_settings_update( $old_value, $value ) {
|
||||||
|
unset( $old_value ); // Unused.
|
||||||
|
unset( $value );
|
||||||
|
|
||||||
|
$this->reset_cache();
|
||||||
|
$this->reschedule_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve supplier configs for display/scheduling.
|
||||||
|
*
|
||||||
|
* @return array<string,array<string,mixed>>
|
||||||
|
*/
|
||||||
|
public function get_supplier_configs() {
|
||||||
|
if ( null !== $this->supplier_cache ) {
|
||||||
|
return $this->supplier_cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = $this->settings->get_all();
|
||||||
|
$configured = isset( $settings['supplier_exports'] ) && is_array( $settings['supplier_exports'] )
|
||||||
|
? $settings['supplier_exports']
|
||||||
|
: array();
|
||||||
|
|
||||||
|
$configured = $this->ensure_distinct_meta_suppliers( $configured );
|
||||||
|
|
||||||
|
uasort(
|
||||||
|
$configured,
|
||||||
|
function ( $a, $b ) {
|
||||||
|
return strcmp(
|
||||||
|
strtolower( isset( $a['supplier'] ) ? $a['supplier'] : '' ),
|
||||||
|
strtolower( isset( $b['supplier'] ) ? $b['supplier'] : '' )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->supplier_cache = $configured;
|
||||||
|
|
||||||
|
return $configured;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve config for specific supplier key.
|
||||||
|
*
|
||||||
|
* @param string $supplier_key Supplier key.
|
||||||
|
* @return array<string,mixed>|null
|
||||||
|
*/
|
||||||
|
public function get_supplier_config( $supplier_key ) {
|
||||||
|
$configs = $this->get_supplier_configs();
|
||||||
|
|
||||||
|
return isset( $configs[ $supplier_key ] ) ? $configs[ $supplier_key ] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send the mail export for supplier key.
|
||||||
|
*
|
||||||
|
* @param string $supplier_key Supplier key.
|
||||||
|
* @return array<string,mixed>|WP_Error
|
||||||
|
*/
|
||||||
|
public function send_export( $supplier_key ) {
|
||||||
|
$supplier_key = sanitize_title( $supplier_key );
|
||||||
|
$config = $this->get_supplier_config( $supplier_key );
|
||||||
|
|
||||||
|
if ( empty( $config ) || empty( $config['supplier'] ) ) {
|
||||||
|
return new WP_Error( 'siti_stock_unknown_supplier', __( 'Onbekende leverancier.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! function_exists( 'wc_get_orders' ) ) {
|
||||||
|
return new WP_Error( 'siti_stock_missing_wc', __( 'WooCommerce is vereist voor de exports.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->collect_rows_for_supplier( $config['supplier'] );
|
||||||
|
|
||||||
|
$admin_email = get_option( 'admin_email' );
|
||||||
|
if ( ! $admin_email || ! is_email( $admin_email ) ) {
|
||||||
|
return new WP_Error( 'siti_stock_missing_email', __( 'Admin e-mailadres kon niet worden opgehaald.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$csv = $this->render_csv( $data['rows'] );
|
||||||
|
$subject = sprintf(
|
||||||
|
/* translators: %s supplier name */
|
||||||
|
__( 'Bestellingen %s (24 uur)', 'siti-stock-plugin' ),
|
||||||
|
$config['supplier']
|
||||||
|
);
|
||||||
|
|
||||||
|
$rows_count = count( $data['rows'] );
|
||||||
|
$body = '';
|
||||||
|
$body .= '<p>' . sprintf(
|
||||||
|
/* translators: %s supplier name */
|
||||||
|
esc_html__( 'Overzicht voor leverancier %s.', 'siti-stock-plugin' ),
|
||||||
|
esc_html( $config['supplier'] )
|
||||||
|
) . '</p>';
|
||||||
|
$body .= '<p>' . sprintf(
|
||||||
|
/* translators: %d total items */
|
||||||
|
esc_html__( 'Totaal aantal regels: %d.', 'siti-stock-plugin' ),
|
||||||
|
(int) $rows_count
|
||||||
|
) . '</p>';
|
||||||
|
|
||||||
|
if ( empty( $data['rows'] ) ) {
|
||||||
|
$body .= '<p>' . esc_html__( 'Er zijn geen bestellingen gevonden in de afgelopen 24 uur.', 'siti-stock-plugin' ) . '</p>';
|
||||||
|
} else {
|
||||||
|
$body .= '<p>' . esc_html__( 'CSV-overzicht (kopieer onderstaande tabel):', 'siti-stock-plugin' ) . '</p>';
|
||||||
|
$body .= '<pre style="font-family: monospace; white-space: pre;">' . esc_html( $csv ) . '</pre>';
|
||||||
|
}
|
||||||
|
|
||||||
|
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
|
||||||
|
$sent = wp_mail( $admin_email, $subject, $body, $headers );
|
||||||
|
|
||||||
|
if ( ! $sent ) {
|
||||||
|
return new WP_Error( 'siti_stock_mail_failed', __( 'E-mail verzenden is mislukt.', 'siti-stock-plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'label' => $config['supplier'],
|
||||||
|
'rows' => count( $data['rows'] ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persist sanitized supplier settings.
|
||||||
|
*
|
||||||
|
* @param array<string,mixed> $input Input from form.
|
||||||
|
*/
|
||||||
|
private function persist_supplier_settings( array $input ) {
|
||||||
|
$current = $this->get_supplier_configs();
|
||||||
|
$sanitized = array();
|
||||||
|
|
||||||
|
foreach ( $current as $key => $config ) {
|
||||||
|
$row = isset( $input[ $key ] ) ? (array) $input[ $key ] : array();
|
||||||
|
$time_raw = isset( $row['time'] ) ? sanitize_text_field( $row['time'] ) : ( isset( $config['time'] ) ? $config['time'] : '' );
|
||||||
|
$time = $this->normalize_time( $time_raw );
|
||||||
|
|
||||||
|
$sanitized[ $key ] = array(
|
||||||
|
'supplier' => isset( $config['supplier'] ) ? $config['supplier'] : '',
|
||||||
|
'time' => $time,
|
||||||
|
'enabled' => ! empty( $row['enabled'] ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = $this->settings->get_all();
|
||||||
|
$settings['supplier_exports'] = $sanitized;
|
||||||
|
$this->settings->reset_cache();
|
||||||
|
update_option( $this->settings->get_option_key(), $settings );
|
||||||
|
$this->reset_cache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect CSV rows for supplier meta value.
|
||||||
|
*
|
||||||
|
* @param string $supplier Supplier label.
|
||||||
|
* @return array<string,mixed>
|
||||||
|
*/
|
||||||
|
private function collect_rows_for_supplier( $supplier ) {
|
||||||
|
$key = $this->normalize_supplier_key( $supplier );
|
||||||
|
|
||||||
|
$site_timezone = function_exists( 'wp_timezone' ) ? wp_timezone() : new DateTimeZone( wp_timezone_string() );
|
||||||
|
$after_date = new DateTimeImmutable( 'now', $site_timezone );
|
||||||
|
$after_date = $after_date->sub( new DateInterval( 'P1D' ) );
|
||||||
|
$after_utc = $after_date->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||||
|
|
||||||
|
$date_query = array(
|
||||||
|
array(
|
||||||
|
'column' => 'date_created_gmt',
|
||||||
|
'after' => $after_utc->format( 'Y-m-d H:i:s' ),
|
||||||
|
'inclusive' => true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$query = new WC_Order_Query(
|
||||||
|
array(
|
||||||
|
'status' => array( 'pending', 'processing', 'on-hold', 'completed' ),
|
||||||
|
'limit' => -1,
|
||||||
|
'type' => 'shop_order',
|
||||||
|
'return' => 'objects',
|
||||||
|
'date_query' => $date_query,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$orders = $query->get_orders();
|
||||||
|
$results = array();
|
||||||
|
|
||||||
|
foreach ( $orders as $order ) {
|
||||||
|
foreach ( $order->get_items( 'line_item' ) as $item ) {
|
||||||
|
$product = $item->get_product();
|
||||||
|
|
||||||
|
if ( ! $product ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$item_supplier = $this->get_product_supplier( $product );
|
||||||
|
|
||||||
|
if ( '' === $item_supplier || $this->normalize_supplier_key( $item_supplier ) !== $key ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sku = $this->get_product_sku( $product );
|
||||||
|
|
||||||
|
if ( '' === $sku ) {
|
||||||
|
$sku = sprintf( 'product-%d', $product->get_id() );
|
||||||
|
}
|
||||||
|
|
||||||
|
$index = strtolower( $sku );
|
||||||
|
|
||||||
|
if ( ! isset( $results[ $index ] ) ) {
|
||||||
|
$results[ $index ] = array(
|
||||||
|
'sku' => $sku,
|
||||||
|
'quantity' => 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$results[ $index ]['quantity'] += (int) $item->get_quantity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array(
|
||||||
|
'rows' => array_values( $results ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve supplier meta for product (falls back to parent).
|
||||||
|
*
|
||||||
|
* @param WC_Product $product Product.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function get_product_supplier( $product ) {
|
||||||
|
$product_id = $product->get_id();
|
||||||
|
$value = get_post_meta( $product_id, self::SUPPLIER_META_KEY, true );
|
||||||
|
|
||||||
|
if ( '' !== $value ) {
|
||||||
|
return trim( (string) $value );
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent_id = $product->get_parent_id();
|
||||||
|
|
||||||
|
if ( $parent_id ) {
|
||||||
|
$parent_value = get_post_meta( $parent_id, self::SUPPLIER_META_KEY, true );
|
||||||
|
|
||||||
|
if ( '' !== $parent_value ) {
|
||||||
|
return trim( (string) $parent_value );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve SKU, falling back to parent product SKU.
|
||||||
|
*
|
||||||
|
* @param WC_Product $product Product.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function get_product_sku( $product ) {
|
||||||
|
$sku = $product->get_sku();
|
||||||
|
|
||||||
|
if ( '' !== $sku ) {
|
||||||
|
return $sku;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent_id = $product->get_parent_id();
|
||||||
|
|
||||||
|
if ( $parent_id ) {
|
||||||
|
$parent = wc_get_product( $parent_id );
|
||||||
|
|
||||||
|
if ( $parent && '' !== $parent->get_sku() ) {
|
||||||
|
return $parent->get_sku();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an array of SKU rows to CSV text separated with semicolons.
|
||||||
|
*
|
||||||
|
* @param array<int,array<string,int|string>> $rows Rows.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function render_csv( array $rows ) {
|
||||||
|
$lines = array( 'sku;aantal' );
|
||||||
|
|
||||||
|
foreach ( $rows as $row ) {
|
||||||
|
$sku = isset( $row['sku'] ) ? $row['sku'] : '';
|
||||||
|
$quantity = isset( $row['quantity'] ) ? (int) $row['quantity'] : 0;
|
||||||
|
$lines[] = sprintf( '%s;%d', $sku, $quantity );
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode( "\r\n", $lines );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedule supplier if enabled.
|
||||||
|
*
|
||||||
|
* @param string $supplier_key Key.
|
||||||
|
* @param array<string,mixed>|null $config Config.
|
||||||
|
*/
|
||||||
|
private function schedule_supplier( $supplier_key, $config ) {
|
||||||
|
if ( empty( $config ) || empty( $config['enabled'] ) || empty( $config['time'] ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$timestamp = $this->calculate_next_timestamp( $config['time'] );
|
||||||
|
wp_schedule_event( $timestamp, 'daily', self::CRON_HOOK, array( $supplier_key ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove scheduled event for supplier.
|
||||||
|
*
|
||||||
|
* @param string $supplier_key Key.
|
||||||
|
*/
|
||||||
|
private function clear_schedule_for_supplier( $supplier_key ) {
|
||||||
|
$timestamp = wp_next_scheduled( self::CRON_HOOK, array( $supplier_key ) );
|
||||||
|
|
||||||
|
while ( $timestamp ) {
|
||||||
|
wp_unschedule_event( $timestamp, self::CRON_HOOK, array( $supplier_key ) );
|
||||||
|
$timestamp = wp_next_scheduled( self::CRON_HOOK, array( $supplier_key ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add entries for suppliers found in product meta.
|
||||||
|
*
|
||||||
|
* @param array<string,mixed> $configured Configured values.
|
||||||
|
* @return array<string,mixed>
|
||||||
|
*/
|
||||||
|
private function ensure_distinct_meta_suppliers( array $configured ) {
|
||||||
|
$result = array();
|
||||||
|
|
||||||
|
foreach ( $this->get_distinct_suppliers() as $supplier ) {
|
||||||
|
$key = $this->normalize_supplier_key( $supplier );
|
||||||
|
$existing = isset( $configured[ $key ] ) ? $configured[ $key ] : array();
|
||||||
|
$result[ $key ] = array(
|
||||||
|
'supplier' => $supplier,
|
||||||
|
'time' => $this->normalize_time(
|
||||||
|
isset( $existing['time'] ) ? $existing['time'] : $this->get_default_time_for_key( $key )
|
||||||
|
),
|
||||||
|
'enabled' => isset( $existing['enabled'] ) ? (bool) $existing['enabled'] : $this->is_default_enabled( $key ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve suppliers from product meta.
|
||||||
|
*
|
||||||
|
* @return array<int,string>
|
||||||
|
*/
|
||||||
|
private function get_distinct_suppliers() {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$meta_table = $wpdb->postmeta;
|
||||||
|
$post_table = $wpdb->posts;
|
||||||
|
$sql = "
|
||||||
|
SELECT DISTINCT pm.meta_value
|
||||||
|
FROM {$meta_table} pm
|
||||||
|
INNER JOIN {$post_table} p ON p.ID = pm.post_id
|
||||||
|
WHERE pm.meta_key = %s
|
||||||
|
AND pm.meta_value <> ''
|
||||||
|
AND p.post_type IN ('product','product_variation')
|
||||||
|
AND p.post_status NOT IN ('trash','auto-draft')
|
||||||
|
";
|
||||||
|
|
||||||
|
$results = $wpdb->get_col( $wpdb->prepare( $sql, self::SUPPLIER_META_KEY ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
|
||||||
|
|
||||||
|
if ( empty( $results ) ) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(
|
||||||
|
function ( $value ) {
|
||||||
|
return trim( (string) $value );
|
||||||
|
},
|
||||||
|
$results
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize supplier key to slug.
|
||||||
|
*
|
||||||
|
* @param string $value Value.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function normalize_supplier_key( $value ) {
|
||||||
|
return sanitize_title( (string) $value );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize HH:MM strings, fallback to midnight.
|
||||||
|
*
|
||||||
|
* @param string $time Time string.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function normalize_time( $time ) {
|
||||||
|
$time = trim( (string) $time );
|
||||||
|
if ( ! preg_match( '/^\d{1,2}:\d{2}$/', $time ) ) {
|
||||||
|
return '00:00';
|
||||||
|
}
|
||||||
|
|
||||||
|
list( $hour, $minute ) = array_map( 'intval', explode( ':', $time, 2 ) );
|
||||||
|
|
||||||
|
$hour = max( 0, min( 23, $hour ) );
|
||||||
|
$minute = max( 0, min( 59, $minute ) );
|
||||||
|
|
||||||
|
return sprintf( '%02d:%02d', $hour, $minute );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the next timestamp for a given HH:MM.
|
||||||
|
*
|
||||||
|
* @param string $time Time in HH:MM.
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private function calculate_next_timestamp( $time ) {
|
||||||
|
$time = $this->normalize_time( $time );
|
||||||
|
list( $hour, $minute ) = array_map( 'intval', explode( ':', $time ) );
|
||||||
|
|
||||||
|
$timezone = wp_timezone();
|
||||||
|
$now = new DateTimeImmutable( 'now', $timezone );
|
||||||
|
$target = $now->setTime( $hour, $minute );
|
||||||
|
|
||||||
|
if ( $target <= $now ) {
|
||||||
|
$target = $target->modify( '+1 day' );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $target->getTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default suppliers (label + default time/settings).
|
||||||
|
*
|
||||||
|
* @return array<string,array<string,mixed>>
|
||||||
|
*/
|
||||||
|
private function get_default_suppliers() {
|
||||||
|
return array(
|
||||||
|
'orion' => array(
|
||||||
|
'label' => 'Orion',
|
||||||
|
'time' => '09:00',
|
||||||
|
'enabled' => true,
|
||||||
|
),
|
||||||
|
'shots' => array(
|
||||||
|
'label' => 'Shots',
|
||||||
|
'time' => '10:00',
|
||||||
|
'enabled' => true,
|
||||||
|
),
|
||||||
|
'stots' => array(
|
||||||
|
'label' => 'Stots',
|
||||||
|
'time' => '10:00',
|
||||||
|
'enabled' => true,
|
||||||
|
),
|
||||||
|
'leg-avenue' => array(
|
||||||
|
'label' => 'Leg Avenue',
|
||||||
|
'time' => '14:00',
|
||||||
|
'enabled' => true,
|
||||||
|
),
|
||||||
|
'oproducts' => array(
|
||||||
|
'label' => 'Oproducts',
|
||||||
|
'time' => '13:00',
|
||||||
|
'enabled' => true,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve default time for supplier key.
|
||||||
|
*
|
||||||
|
* @param string $key Supplier key.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function get_default_time_for_key( $key ) {
|
||||||
|
$defaults = $this->get_default_suppliers();
|
||||||
|
|
||||||
|
return isset( $defaults[ $key ] ) ? $defaults[ $key ]['time'] : '09:00';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine default enabled state for supplier key.
|
||||||
|
*
|
||||||
|
* @param string $key Supplier key.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function is_default_enabled( $key ) {
|
||||||
|
$defaults = $this->get_default_suppliers();
|
||||||
|
|
||||||
|
return isset( $defaults[ $key ] ) ? (bool) $defaults[ $key ]['enabled'] : false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,11 @@
|
|||||||
* Plugin Name: Siti Stock Plugin
|
* Plugin Name: Siti Stock Plugin
|
||||||
* Plugin URI: https://github.com/SitiWeb/siti-stock-plugin
|
* Plugin URI: https://github.com/SitiWeb/siti-stock-plugin
|
||||||
* Description: Synchroniseert WooCommerce voorraad met het externe Siti voorraadplatform.
|
* Description: Synchroniseert WooCommerce voorraad met het externe Siti voorraadplatform.
|
||||||
* Version: 1.2.1
|
* Version: 1.2.8
|
||||||
* Author: Siti Web
|
* Author: Roberto Guagliardo | SitiWeb
|
||||||
* Author URI: https://www.siti.nl
|
* Author URI: https://sitiweb.nl/
|
||||||
* Requires PHP: 8.1
|
* Requires PHP: 8.1
|
||||||
* Requires at least: 6.4
|
* Requires at least: 6.5
|
||||||
* Text Domain: siti-stock-plugin
|
* Text Domain: siti-stock-plugin
|
||||||
* Domain Path: /languages
|
* Domain Path: /languages
|
||||||
*/
|
*/
|
||||||
@@ -16,7 +16,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
define( 'SITI_STOCK_PLUGIN_VERSION', '1.2.0' );
|
define( 'SITI_STOCK_PLUGIN_VERSION', '1.2.8' );
|
||||||
define( 'SITI_STOCK_PLUGIN_FILE', __FILE__ );
|
define( 'SITI_STOCK_PLUGIN_FILE', __FILE__ );
|
||||||
define( 'SITI_STOCK_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
|
define( 'SITI_STOCK_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
|
||||||
|
|
||||||
@@ -41,12 +41,12 @@ Siti_Stock_Plugin::instance();
|
|||||||
add_action(
|
add_action(
|
||||||
'admin_init',
|
'admin_init',
|
||||||
function () {
|
function () {
|
||||||
if ( ! class_exists( 'SitiWebUpdater' ) ) {
|
if( ! class_exists( 'SitiWebUpdater2' ) ){
|
||||||
return;
|
include_once( plugin_dir_path( __FILE__ ) . 'includes/SitiWebUpdater2.php' );
|
||||||
}
|
}
|
||||||
|
|
||||||
$updater = new SitiWebUpdater( __FILE__ );
|
$updater = new SitiWebUpdater2( __FILE__ );
|
||||||
$updater->set_username( 'SitiWeb' );
|
$updater->set_owner( 'roberto' );
|
||||||
$updater->set_repository( 'siti-stock-plugin' );
|
$updater->set_repository( 'siti-stock-plugin' );
|
||||||
$updater->initialize();
|
$updater->initialize();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user