Skip to content

Commit

Permalink
feat: added new updater from github
Browse files Browse the repository at this point in the history
  • Loading branch information
lightningspirit committed Mar 27, 2024
1 parent 347bf78 commit 2816935
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 3 deletions.
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ node_modules
*.sql
*.tar.gz
*.zip
wp-info.json
19 changes: 18 additions & 1 deletion email-logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Author URI: https://moveyourdigital.com
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: email-logs
* Update URI: https://github.com/moveyourdigital/wp-email-logs/raw/main/wp-info.json
* Text Domain: email-logs
* Domain Path: /languages
*
Expand All @@ -32,6 +32,10 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Load plugin translations and post type
*
Expand All @@ -48,6 +52,19 @@ function () {

include __DIR__ . '/inc/post-types/email-log.php';
include __DIR__ . '/inc/filters/pre-wp-mail.php';
include __DIR__ . '/updater.php';
}
);

/**
* Filters this plugin data
*
* @since 0.3.2
*/
add_filter(
'plugin_basename_file_' . plugin_basename( __DIR__ ),
function () {
return plugin_basename( __FILE__ );
}
);

Expand Down
4 changes: 4 additions & 0 deletions inc/filters/pre-wp-mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* @package email-logs
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Filters whether to preempt sending an email.
*
Expand Down
11 changes: 10 additions & 1 deletion inc/post-types/email-log.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
/**
* Registers the `email_log` post type.
* Registers core plugin functionality
*
* @package email-logs
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Registers the `email_log` post type.
*
* @package email-logs
*/
add_action(
'init',
function () {
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Log all emails sent from WordPress.

This section describes how to install the plugin and get it working.

1. Upload `wp-email-logs` directory to the `/wp-content/plugins/` directory
1. Upload `wp-email-logs-main` directory to the `/wp-content/plugins/` directory
1. Activate the plugin through the 'Plugins' menu in WordPress
178 changes: 178 additions & 0 deletions updater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* Hook plugin updater from git repository
*
* @package email-logs
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Fetch and return plugin data from declared remote
*
* @since 0.3.2
*
* @param false|object|array $default_result default data to return if fetch fails
*/
add_filter(
'plugin_update_remote_data_' . plugin_basename( __DIR__ ),
function ( $default_result = '' ) {
$remote_data = wp_remote_get(
get_option( 'plugin_update_uri_' . plugin_basename( __DIR__ ) ),
array(
'timeout' => 10,
'headers' => array(
'Accept' => 'application/json',
),
)
);

if ( is_wp_error( $remote_data )
|| 200 !== wp_remote_retrieve_response_code( $remote_data )
|| empty( wp_remote_retrieve_body( $remote_data ) ) ) {
return $default_result;
}

return json_decode( wp_remote_retrieve_body( $remote_data ) );
}
);

/**
* Filters the response for the current WordPress.org Plugin Installation API request.
*
* Returning a non-false value will effectively short-circuit the WordPress.org API request.
*
* If `$action` is 'query_plugins' or 'plugin_information', an object MUST be passed.
* If `$action` is 'hot_tags' or 'hot_categories', an array should be passed.
*
* @since 2.7.0
*
* @param false|object|array $result The result object or array. Default false.
* @param string $action The type of information being requested from the Plugin Installation API.
* @param object $args Plugin API arguments.
*/
add_filter(
'plugins_api',
function ( $result, $action, $args ) {
if ( 'plugin_information' !== $action ) {
return $result;
}

if ( plugin_basename( __DIR__ ) !== $args->slug ) {
return $result;
}

$result = apply_filter( 'plugin_update_remote_data_' . plugin_basename( __DIR__ ), $result );
$result->slug = plugin_basename( __DIR__ );
$result->trunk = $remote->download_url;

return $result;
},
20,
3
);

/**
* Fires once activated plugins have loaded.
*
* Pluggable functions are also available at this point in the loading order.
*
* @since 1.5.0
*/
add_action(
'init',
function () {
$basename_file = apply_filters( 'plugin_basename_file_' . plugin_basename( __DIR__ ) );
$update_uri = get_option( 'plugin_update_uri_' . plugin_basename( __DIR__ ) );

if ( ! $update_uri ) {
if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$plugin_data = get_plugin_data( WP_PLUGIN_DIR . $basename_file );
$update_uri = $plugin_data['UpdateURI'];

add_option( 'plugin_update_uri_' . plugin_basename( __DIR__ ), $update_uri );
}

$hostname = wp_parse_url( sanitize_url( $update_uri ), PHP_URL_HOST );

add_filter(
'update_plugins_' . $hostname,
function ( $update, $plugin_data, $plugin_file ) {
if ( $plugin_file !== $basename_file ) {
return $update;
}

if ( ! empty( $update ) ) {
return $update;
}

$remote_data = apply_filter( 'plugin_update_remote_data_' . plugin_basename( __DIR__ ), $result );

if ( ! version_compare( $plugin_data['Version'], $remote_data->version, '<' ) ) {
return $update;
}

return array(
'slug' => plugin_basename( __DIR__ ),
'version' => $remote_data->version,
'url' => $plugin_data['PluginURI'],
'package' => $remote_data->download_url,
);
},
10,
4
);
}
);

/**
* Fires when the upgrader process is complete.
*
* See also {@see 'upgrader_package_options'}.
*
* @since 3.6.0
* @since 3.7.0 Added to WP_Upgrader::run().
* @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
*
* @param WP_Upgrader $upgrader WP_Upgrader instance. In other contexts this might be a
* Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
* @param array $hook_extra {
* Array of bulk item update data.
*
* @type string $action Type of action. Default 'update'.
* @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
* @type bool $bulk Whether the update process is a bulk update. Default true.
* @type array $plugins Array of the basename paths of the plugins' main files.
* @type array $themes The theme slugs.
* @type array $translations {
* Array of translations update data.
*
* @type string $language The locale the translation is for.
* @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
* @type string $slug Text domain the translation is for. The slug of a theme/plugin or
* 'default' for core translations.
* @type string $version The version of a theme, plugin, or core.
* }
* }
*/
add_action(
'upgrader_process_complete',
function ( $upgrader_object, $options ) {
$basename_file = apply_filters( 'plugin_basename_file_' . plugin_basename( __DIR__ ) );

if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) {
foreach ( $options['plugins'] as $each_plugin ) {
if ( $each_plugin === $basename_file ) {
delete_option( 'plugin_update_uri_' . plugin_basename( __DIR__ ) );
}
}
}
},
10,
2
);
16 changes: 16 additions & 0 deletions wp-info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Email Logs",
"slug": "wp-email-logs",
"author": "<a href='https://github.com/moveyourdigital'>Move Your Digital</a>",
"author_profile": "https://github.com/moveyourdigital",
"version": "0.3.1",
"download_url": "https://github.com/moveyourdigital/wp-email-logs/archive/refs/heads/main.zip",
"requires": "5.8",
"tested": "6.4",
"requires_php": "7.4",
"last_updated": "2024-03-27 18:35:00",
"sections": {
"description": "This simple plugin does nothing, only gets updates from a custom server",
"installation": "Click the activate button and that's it."
}
}

0 comments on commit 2816935

Please sign in to comment.