| 1 |
<?php |
| 2 |
|
| 3 |
// uncomment this line for testing |
| 4 |
//set_site_transient( 'update_plugins', null ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Allows plugins to use their own update API. |
| 8 |
* |
| 9 |
* This is part of the EDD Software Licensing suite. Just renamed here for Charitable. |
| 10 |
* |
| 11 |
* @author Pippin Williamson |
| 12 |
* @version 1.6 |
| 13 |
*/ |
| 14 |
class Charitable_Plugin_Updater { |
| 15 |
private $api_url = ''; |
| 16 |
private $api_data = array(); |
| 17 |
private $name = ''; |
| 18 |
private $slug = ''; |
| 19 |
private $version = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class constructor. |
| 23 |
* |
| 24 |
* @uses plugin_basename() |
| 25 |
* @uses hook() |
| 26 |
* |
| 27 |
* @param string $_api_url The URL pointing to the custom API endpoint. |
| 28 |
* @param string $_plugin_file Path to the plugin file. |
| 29 |
* @param array $_api_data Optional data to send with API calls. |
| 30 |
*/ |
| 31 |
public function __construct( $_api_url, $_plugin_file, $_api_data = array() ) { |
| 32 |
|
| 33 |
// do not apply plugin updating to core plugin, updated 1.7.0.10 |
| 34 |
if ( 'charitable' === basename( $_plugin_file, '.php' ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$this->api_url = trailingslashit( $_api_url ); |
| 39 |
$this->api_data = $_api_data; |
| 40 |
$this->name = plugin_basename( $_plugin_file ); |
| 41 |
$this->slug = basename( $_plugin_file, '.php' ); |
| 42 |
$this->version = $_api_data['version']; |
| 43 |
|
| 44 |
// Set up hooks. |
| 45 |
$this->init(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Set up WordPress filters to hook into WP's update process. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function init() { |
| 56 |
add_action( 'load-plugins.php', array( $this, 'setup_update_notification' ), 30 ); |
| 57 |
add_action( 'admin_init', array( $this, 'show_changelog' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* This disables the default update row shown for Charitable extensions, |
| 62 |
* and sets up our own callback to show the update notification. |
| 63 |
* |
| 64 |
* @since 1.4.20 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function setup_update_notification() { |
| 69 |
remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 ); |
| 70 |
add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Show update nofication row. |
| 75 |
* |
| 76 |
* This is a drop-in replacement for wp_plugin_update_row(), |
| 77 |
* which is the function that will normally show the update row |
| 78 |
* for a plugin. Replaced here to show our changelog and also |
| 79 |
* prompt to the user to renew/set their license if it has |
| 80 |
* expired or hasn't been added yet. |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* |
| 84 |
* @param string $file |
| 85 |
* @param array $plugin |
| 86 |
*/ |
| 87 |
public function show_update_notification( $file, $plugin ) { |
| 88 |
if ( ! current_user_can( 'update_plugins' ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if ( $this->name != $file ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$version_info = Charitable_Licenses::get_instance()->get_version_info( $this->name ); |
| 97 |
|
| 98 |
if ( ! $version_info ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
if ( version_compare( $this->version, $version_info->new_version, '<' ) ) { |
| 103 |
|
| 104 |
// Build a plugin list row, with update notification. |
| 105 |
echo '<tr class="plugin-update-tr" id="' . $this->slug . '-update" data-slug="' . $this->slug . '" data-plugin="' . $this->slug . '/' . $file . '">'; |
| 106 |
echo '<td colspan="3" class="plugin-update colspanchange">'; |
| 107 |
echo '<div class="update-message notice inline notice-warning notice-alt">'; |
| 108 |
|
| 109 |
$changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' ); |
| 110 |
|
| 111 |
if ( isset( $version_info->download_link ) ) { |
| 112 |
switch ( $version_info->download_link ) { |
| 113 |
case 'missing_license': |
| 114 |
case 'expired_license': |
| 115 |
case 'missing_requirements': |
| 116 |
echo htmlspecialchars_decode( $version_info->package_download_restriction ); |
| 117 |
break; |
| 118 |
|
| 119 |
default: |
| 120 |
if ( isset( $version_info->download_link ) !== false ) { |
| 121 |
$name = esc_html( $version_info->name ); |
| 122 |
} else if ( isset( $version_info->slug ) !== false ) { |
| 123 |
$name = esc_html( $version_info->slug ); |
| 124 |
} else { |
| 125 |
$name = 'Charitable'; |
| 126 |
} |
| 127 |
printf( |
| 128 |
__( '<p>There is a new version of %1$s available. <a target="_blank" class="thickbox" href="%2$s">View version %3$s details</a> or <a href="%4$s">update now</a>.</p>', 'charitable' ), |
| 129 |
$name, |
| 130 |
esc_url( $changelog_link ), |
| 131 |
esc_html( $version_info->new_version ), |
| 132 |
esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) |
| 133 |
); |
| 134 |
break; |
| 135 |
} |
| 136 |
} else { |
| 137 |
if ( isset( $version_info->download_link ) !== false ) { |
| 138 |
$name = esc_html( $version_info->name ); |
| 139 |
} else if ( isset( $version_info->slug ) !== false ) { |
| 140 |
$name = esc_html( $version_info->slug ); |
| 141 |
} else { |
| 142 |
$name = 'Charitable'; |
| 143 |
} |
| 144 |
printf( |
| 145 |
__( '<p>There is a new version of %1$s available. <a target="_blank" class="thickbox" href="%2$s">View version %3$s details</a> or <a href="%4$s">update now</a>.</p>', 'charitable' ), |
| 146 |
$name, |
| 147 |
esc_url( $changelog_link ), |
| 148 |
esc_html( $version_info->new_version ), |
| 149 |
esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
do_action( "in_plugin_update_message-{$file}", $plugin, $version_info ); |
| 154 |
|
| 155 |
echo '</div></td></tr>'; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Display the changelog. |
| 161 |
* |
| 162 |
* @since 1.0.0 |
| 163 |
* |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function show_changelog() { |
| 167 |
if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if ( empty( $_REQUEST['plugin'] ) ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if ( empty( $_REQUEST['slug'] ) ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
if ( ! current_user_can( 'update_plugins' ) ) { |
| 180 |
wp_die( __( 'You do not have permission to install plugin updates', 'charitable' ), __( 'Error', 'charitable' ), array( 'response' => 403 ) ); |
| 181 |
} |
| 182 |
|
| 183 |
$version_info = Charitable_Licenses::get_instance()->get_version_info( $_REQUEST['plugin'] ); |
| 184 |
|
| 185 |
if ( $version_info && isset( $version_info->sections['changelog'] ) ) { |
| 186 |
echo '<div style="padding:10px;">' . $version_info->sections['changelog'] . '</div>'; |
| 187 |
} |
| 188 |
|
| 189 |
exit; |
| 190 |
} |
| 191 |
} |
| 192 |
|