Skip to content

Commit 43569ad

Browse files
authored
Merge pull request #2 from web-lifter/codex/setup-automatic-plugin-updater
Add GitHub updater and order meta viewer
2 parents f038fe1 + a00638b commit 43569ad

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

includes/class-wc-utils-features.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class WC_Utils_Features {
1111
public function __construct() {
1212
add_filter( 'wc_order_statuses', array( $this, 'register_order_status' ) );
1313
add_action( 'init', array( $this, 'add_order_status' ) );
14+
add_action( 'add_meta_boxes', array( $this, 'add_order_meta_box' ) );
1415
}
1516

1617
/**
@@ -42,4 +43,38 @@ public function register_order_status( $order_statuses ) {
4243

4344
return $new_order_statuses;
4445
}
46+
47+
/**
48+
* Add a meta box showing all order meta values.
49+
*/
50+
public function add_order_meta_box() {
51+
add_meta_box(
52+
'wc-utils-order-meta',
53+
__( 'Order Meta Fields', 'woocommerce-utils' ),
54+
array( $this, 'render_order_meta_box' ),
55+
'shop_order',
56+
'normal',
57+
'default'
58+
);
59+
}
60+
61+
/**
62+
* Render the order meta box.
63+
*
64+
* @param WP_Post $post Order post object.
65+
*/
66+
public function render_order_meta_box( $post ) {
67+
$order = wc_get_order( $post->ID );
68+
if ( ! $order ) {
69+
return;
70+
}
71+
72+
echo '<table class="widefat striped"><tbody>';
73+
foreach ( $order->get_meta_data() as $meta ) {
74+
$key = esc_html( $meta->key );
75+
$value = maybe_serialize( $meta->value );
76+
echo '<tr><th style="width:200px">' . $key . '</th><td>' . esc_html( $value ) . '</td></tr>';
77+
}
78+
echo '</tbody></table>';
79+
}
4580
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
if ( ! defined( 'ABSPATH' ) ) {
3+
exit;
4+
}
5+
6+
/**
7+
* Simple GitHub based plugin updater.
8+
*/
9+
class WC_Utils_Updater {
10+
11+
/**
12+
* GitHub repository raw URL.
13+
*
14+
* @var string
15+
*/
16+
private $raw_url;
17+
18+
/**
19+
* GitHub repository zip URL.
20+
*
21+
* @var string
22+
*/
23+
private $zip_url;
24+
25+
/**
26+
* Plugin file basename.
27+
*
28+
* @var string
29+
*/
30+
private $plugin_basename;
31+
32+
/**
33+
* Current version.
34+
*
35+
* @var string
36+
*/
37+
private $version;
38+
39+
/**
40+
* Constructor.
41+
*/
42+
public function __construct( $repo_raw_url, $repo_zip_url, $plugin_basename, $version ) {
43+
$this->raw_url = trailingslashit( $repo_raw_url );
44+
$this->zip_url = $repo_zip_url;
45+
$this->plugin_basename = $plugin_basename;
46+
$this->version = $version;
47+
48+
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
49+
add_filter( 'plugins_api', array( $this, 'plugins_api' ), 10, 3 );
50+
}
51+
52+
/**
53+
* Check GitHub for a newer version.
54+
*
55+
* @param object $transient Update transient.
56+
* @return object
57+
*/
58+
public function check_for_update( $transient ) {
59+
if ( empty( $transient->checked ) ) {
60+
return $transient;
61+
}
62+
63+
$remote = wp_remote_get( $this->raw_url . 'woocommerce-utils.php' );
64+
65+
if ( is_wp_error( $remote ) ) {
66+
return $transient;
67+
}
68+
69+
if ( 200 !== wp_remote_retrieve_response_code( $remote ) ) {
70+
return $transient;
71+
}
72+
73+
$body = wp_remote_retrieve_body( $remote );
74+
75+
if ( ! preg_match( '/^\s*\*\s*Version:\s*(.*)$/mi', $body, $matches ) ) {
76+
return $transient;
77+
}
78+
79+
$remote_version = trim( $matches[1] );
80+
81+
if ( version_compare( $this->version, $remote_version, '>=' ) ) {
82+
return $transient;
83+
}
84+
85+
$plugin = new stdClass();
86+
$plugin->slug = dirname( $this->plugin_basename );
87+
$plugin->new_version = $remote_version;
88+
$plugin->url = $this->raw_url;
89+
$plugin->package = $this->zip_url;
90+
91+
$transient->response[ $this->plugin_basename ] = $plugin;
92+
93+
return $transient;
94+
}
95+
96+
/**
97+
* Provide plugin information for the update screen.
98+
*
99+
* @param false|object|array $result The result object or array. Default false.
100+
* @param string $action The type of information being requested from the Plugin Installation API.
101+
* @param object $args Plugin API arguments.
102+
* @return object|false
103+
*/
104+
public function plugins_api( $result, $action, $args ) {
105+
if ( 'plugin_information' !== $action ) {
106+
return $result;
107+
}
108+
109+
if ( empty( $args->slug ) || dirname( $this->plugin_basename ) !== $args->slug ) {
110+
return $result;
111+
}
112+
113+
$remote = wp_remote_get( $this->raw_url . 'readme.txt' );
114+
115+
if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) ) {
116+
return $result;
117+
}
118+
119+
$readme = wp_remote_retrieve_body( $remote );
120+
121+
$info = new stdClass();
122+
$info->name = 'WooCommerce Utils';
123+
$info->slug = dirname( $this->plugin_basename );
124+
$info->version = $this->version;
125+
$info->sections = array(
126+
'description' => $readme,
127+
);
128+
$info->download_link = $this->zip_url;
129+
130+
return $info;
131+
}
132+
}
133+

woocommerce-utils.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Author: Example Author
77
* Text Domain: woocommerce-utils
88
* Domain Path: /languages
9+
* GitHub Plugin URI: https://github.com/example/woocommerce-utils
910
*/
1011

1112
if ( ! defined( 'ABSPATH' ) ) {
@@ -22,6 +23,12 @@
2223
if ( ! defined( 'WC_UTILS_URL' ) ) {
2324
define( 'WC_UTILS_URL', plugin_dir_url( __FILE__ ) );
2425
}
26+
if ( ! defined( 'WC_UTILS_REPO_RAW' ) ) {
27+
define( 'WC_UTILS_REPO_RAW', 'https://raw.githubusercontent.com/example/woocommerce-utils/main/' );
28+
}
29+
if ( ! defined( 'WC_UTILS_REPO_ZIP' ) ) {
30+
define( 'WC_UTILS_REPO_ZIP', 'https://github.com/example/woocommerce-utils/archive/refs/heads/main.zip' );
31+
}
2532

2633
/**
2734
* Initialize the plugin.
@@ -36,10 +43,12 @@ function wc_utils_init() {
3643
// Include required files.
3744
require_once WC_UTILS_PATH . 'includes/class-wc-utils-admin.php';
3845
require_once WC_UTILS_PATH . 'includes/class-wc-utils-features.php';
46+
require_once WC_UTILS_PATH . 'includes/class-wc-utils-updater.php';
3947

4048
// Initialize classes.
4149
new WC_Utils_Admin();
4250
new WC_Utils_Features();
51+
new WC_Utils_Updater( WC_UTILS_REPO_RAW, WC_UTILS_REPO_ZIP, plugin_basename( __FILE__ ), WC_UTILS_VERSION );
4352
}
4453
add_action( 'plugins_loaded', 'wc_utils_init' );
4554

0 commit comments

Comments
 (0)