Skip to content

Commit

Permalink
Merge pull request #25 from Yoohoo-Plugins/dev
Browse files Browse the repository at this point in the history
Version 1.3
  • Loading branch information
andrewlimaza authored Apr 2, 2019
2 parents f10e58f + 80ff63d commit 52be76a
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 89 deletions.
27 changes: 18 additions & 9 deletions includes/settings-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
* Setting header for admin area.
*/
?>
<h1><?php _e( 'Zapier Settings', 'wp-zapier' ); ?></h1>
<hr/>
<style>
.wp-zapier-action-links { margin-top: 20px; }
.wp-zapier-action-links a { text-decoration:none; }
.wp-zapier-action-links a.active { color: black; font-weight:bold; }
</style>
<div class='wp-zapier-action-links'>
<a <?php if ( ! isset( $_REQUEST['receive_data'] ) && ! isset( $_REQUEST['license_settings'] ) ) { echo "class='active'"; } ?> href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings' ); ?>"><?php _e( 'Send Data To Zapier', 'wp-zapier' ); ?></a> | <a <?php if ( isset( $_REQUEST['receive_data'] ) ) { echo "class='active'"; } ?> href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings&receive_data=true' ); ?>"><?php _e( 'Receive Data From Zapier', 'wp-zapier' ); ?></a> | <a <?php if ( isset( $_REQUEST['license_settings'] ) ) { echo "class='active'"; } ?> href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings&license_settings=1' ); ?>"><?php _e( 'License Settings', 'wp-zapier' ); ?></a>
<div class="wp-zapier-settings-header" style="font-size:25px;margin:2% 0 0 0;display:inline-block;">
<?php _e( 'WP Zapier - Settings', 'wp-zapier' );?>
<div class="wp-zapier-settings-header-meta" style="font-size:14px;margin-left:5px;display:inline-block;">
<?php echo "v" . WPZP_VERSION; ?>
<span style="margin-left:5px;"><a href="https://yoohooplugins.com/?s=zapier" class="button-primary" style="margin-top:-4px;"><?php _e( 'Documentation', 'wp-zapier' ); ?></a></span>
</div>
</div>

<div class="wrap"><h2 style="margin:0;font-size:1px;"></h2></div>

<nav class='wp-zapier-action-links nav-tab-wrapper'>
<a href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings' ); ?>" class="nav-tab <?php if( ! isset( $_REQUEST['receive_data'] ) && ! isset( $_REQUEST['license_settings'] ) ) { echo "nav-tab-active"; } ?>"><?php _e( 'Send Data To Zapier', 'wp-zapier' ); ?></a>

<a href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings&receive_data=true' ); ?>" class="nav-tab <?php if( isset( $_REQUEST['receive_data'] ) && $_REQUEST['receive_data'] == 'true' ) { echo "nav-tab-active"; } ?>"><?php _e( 'Receive Data From Zapier', 'wp-zapier' ); ?></a>

<a href="<?php echo admin_url( 'options-general.php?page=wp-zapier-settings&license_settings=true' ); ?>" class="nav-tab <?php if( isset( $_REQUEST['license_settings'] ) && $_REQUEST['license_settings'] == 'true' ) { echo "nav-tab-active"; } ?>"><?php _e( 'License Settings', 'wp-zapier' ); ?></a>

<?php do_action( 'wp_zapier_settings_header_tabs' ); ?>
</nav>
<br/>
20 changes: 15 additions & 5 deletions includes/settings-receive-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

$api_key = $settings['api_key'];

$webhook_url = add_query_arg( array( 'wpz_webhook' => '1', 'api_key' => $api_key ), home_url( '/' ) );

// Force HTTPS:// when generating URL.
if ( is_ssl() ) {
$webhook_url = apply_filters( 'wp_zapier_webhook_url', str_replace( 'http://', 'https://', $webhook_url ) );
}

?>
<?php require_once( 'settings-header.php' ); ?>

Expand All @@ -18,7 +25,9 @@
<label for="wp_zapier_webhook_url"><?php _e( 'Webhook URL', 'wp-zapier'); ?></label>
</th>
<td>
<input type='text' size='77' name='wp_zapier_webhook_url' value="<?php echo add_query_arg( array( 'wpz_webhook' => '1', 'api_key' => $api_key ), home_url( '/' ) ); ?>" readonly />
<input type='text' size='77' name='wp_zapier_webhook_url' value="<?php echo $webhook_url; ?>" readonly /><br/>
<small><?php _e( 'Please copy entire URL when adding this to Zapier.', 'wp-zapier' ); ?></small>

</td>
</tr>

Expand All @@ -40,10 +49,11 @@

<tr>
<th>

<p><?php _e( 'create_user', 'wp-zapier'); ?></p>
</th>
<td>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*, username, first_name, last_name, role, usermeta**</strong></p>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*, username*, first_name, last_name, role, usermeta**</strong></p>
<p><?php _e( 'Create a new user and send an email notification to both the user and site administrator.', 'wp-zapier' ); ?></p>
<p><?php _e( "This will not create the user if the user's email exists inside WordPress and will be skipped.", "wp-zapier" ); ?></p>
</td>
Expand All @@ -54,7 +64,7 @@
<p><?php _e( 'update_user', 'wp-zapier'); ?></p>
</th>
<td>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*, newemail, first_name, last_name, role, usermeta**</strong></p>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*, new_email, username, first_name, last_name, role, usermeta**</strong></p>
<p><?php _e( 'Update existing user data via email.' ); ?></p>
<p><?php _e( "This will create a new user if the user's email does not exist.", "wp-zapier" ); ?></p>
</td>
Expand All @@ -65,7 +75,7 @@
<p><?php _e( 'delete_user', 'wp-zapier'); ?></p>
</th>
<td>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*</strong></p>
<p><strong><?php _e( 'Accepts', 'wp-zapier'); ?>: email*, username</strong></p>
<p><?php _e( 'Delete an existing user from WordPress which will delete all their usermeta.', 'wp-zapier' ); ?></p>
</td>
</tr>
Expand All @@ -75,4 +85,4 @@
</div>

<p><small>* <?php _e( 'Required fields.', 'wp-zapier' ); ?></small></p>
<p><small>** <?php _e( sprintf( 'Please see %s for the layout needed for usermeta fields.', '<a href="https://yoohooplugins.com/documentation/" target="_blank" rel="noopener nofollow">documentation</a>' ), 'wp-zapier' );?></small></p>
<p><small>** <?php _e( sprintf( 'Please see %s for the layout needed for usermeta fields.', '<a href="https://yoohooplugins.com/documentation/add-user-meta-wp-zapier/?utm-source=plugin" target="_blank" rel="noopener nofollow">documentation</a>' ), 'wp-zapier' );?></small></p>
127 changes: 104 additions & 23 deletions includes/updates/zapier-update-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Allows plugins to use their own update API.
*
* @author Easy Digital Downloads
* @version 1.6.14
* @version 1.6.18
*/
class Yoohoo_Zapier_Update_Checker {
class Yoohoo_Zapier_Update_Checker{

private $api_url = '';
private $api_data = array();
Expand All @@ -19,6 +19,8 @@ class Yoohoo_Zapier_Update_Checker {
private $wp_override = false;
private $cache_key = '';

private $health_check_timeout = 5;

/**
* Class constructor.
*
Expand All @@ -40,10 +42,19 @@ public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
$this->version = $_api_data['version'];
$this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false;
$this->beta = ! empty( $this->api_data['beta'] ) ? true : false;
$this->cache_key = md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) );
$this->cache_key = 'edd_sl_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) );

$edd_plugin_data[ $this->slug ] = $this->api_data;

/**
* Fires after the $edd_plugin_data is setup.
*
* @since x.x.x
*
* @param array $edd_plugin_data Array of EDD SL plugin data.
*/
do_action( 'post_edd_sl_plugin_updater_setup', $edd_plugin_data );

// Set up hooks.
$this->init();

Expand Down Expand Up @@ -110,9 +121,12 @@ public function check_update( $_transient_data ) {

$_transient_data->response[ $this->name ] = $version_info;

// Make sure the plugin property is set to the plugin's name/location. See issue 1463 on Software Licensing's GitHub repo.
$_transient_data->response[ $this->name ]->plugin = $this->name;

}

$_transient_data->last_checked = current_time( 'timestamp' );
$_transient_data->last_checked = time();
$_transient_data->checked[ $this->name ] = $this->version;

}
Expand Down Expand Up @@ -158,6 +172,19 @@ public function show_update_notification( $file, $plugin ) {
if ( false === $version_info ) {
$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );

// Since we disabled our filter for the transient, we aren't running our object conversion on banners, sections, or icons. Do this now:
if ( isset( $version_info->banners ) && ! is_array( $version_info->banners ) ) {
$version_info->banners = $this->convert_object_to_array( $version_info->banners );
}

if ( isset( $version_info->sections ) && ! is_array( $version_info->sections ) ) {
$version_info->sections = $this->convert_object_to_array( $version_info->sections );
}

if ( isset( $version_info->icons ) && ! is_array( $version_info->icons ) ) {
$version_info->icons = $this->convert_object_to_array( $version_info->icons );
}

$this->set_version_info_cache( $version_info );
}

Expand All @@ -171,7 +198,7 @@ public function show_update_notification( $file, $plugin ) {

}

$update_cache->last_checked = current_time( 'timestamp' );
$update_cache->last_checked = time();
$update_cache->checked[ $this->name ] = $this->version;

set_site_transient( 'update_plugins', $update_cache );
Expand Down Expand Up @@ -251,7 +278,8 @@ public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
'is_ssl' => is_ssl(),
'fields' => array(
'banners' => array(),
'reviews' => false
'reviews' => false,
'icons' => array(),
)
);

Expand All @@ -278,27 +306,47 @@ public function plugins_api_filter( $_data, $_action = '', $_args = null ) {

// Convert sections into an associative array, since we're getting an object, but Core expects an array.
if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
$new_sections = array();
foreach ( $_data->sections as $key => $value ) {
$new_sections[ $key ] = $value;
}

$_data->sections = $new_sections;
$_data->sections = $this->convert_object_to_array( $_data->sections );
}

// Convert banners into an associative array, since we're getting an object, but Core expects an array.
if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
$new_banners = array();
foreach ( $_data->banners as $key => $value ) {
$new_banners[ $key ] = $value;
}
$_data->banners = $this->convert_object_to_array( $_data->banners );
}

$_data->banners = $new_banners;
// Convert icons into an associative array, since we're getting an object, but Core expects an array.
if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
$_data->icons = $this->convert_object_to_array( $_data->icons );
}

if( ! isset( $_data->plugin ) ) {
$_data->plugin = $this->name;
}

return $_data;
}

/**
* Convert some objects to arrays when injecting data into the update API
*
* Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON
* decoding, they are objects. This method allows us to pass in the object and return an associative array.
*
* @since 3.6.5
*
* @param stdClass $data
*
* @return array
*/
private function convert_object_to_array( $data ) {
$new_data = array();
foreach ( $data as $key => $value ) {
$new_data[ $key ] = $value;
}

return $new_data;
}

/**
* Disable SSL verification in order to prevent download update failures
*
Expand Down Expand Up @@ -329,15 +377,39 @@ public function http_request_args( $args, $url ) {
*/
private function api_request( $_action, $_data ) {

global $wp_version;
global $wp_version, $edd_plugin_url_available;

$verify_ssl = $this->verify_ssl();

// Do a quick status check on this domain if we haven't already checked it.
$store_hash = md5( $this->api_url );
if ( ! is_array( $edd_plugin_url_available ) || ! isset( $edd_plugin_url_available[ $store_hash ] ) ) {
$test_url_parts = parse_url( $this->api_url );

$scheme = ! empty( $test_url_parts['scheme'] ) ? $test_url_parts['scheme'] : 'http';
$host = ! empty( $test_url_parts['host'] ) ? $test_url_parts['host'] : '';
$port = ! empty( $test_url_parts['port'] ) ? ':' . $test_url_parts['port'] : '';

if ( empty( $host ) ) {
$edd_plugin_url_available[ $store_hash ] = false;
} else {
$test_url = $scheme . '://' . $host . $port;
$response = wp_remote_get( $test_url, array( 'timeout' => $this->health_check_timeout, 'sslverify' => $verify_ssl ) );
$edd_plugin_url_available[ $store_hash ] = is_wp_error( $response ) ? false : true;
}
}

if ( false === $edd_plugin_url_available[ $store_hash ] ) {
return;
}

$data = array_merge( $this->api_data, $_data );

if ( $data['slug'] != $this->slug ) {
return;
}

if( $this->api_url == trailingslashit (home_url() ) ) {
if( $this->api_url == trailingslashit ( home_url() ) ) {
return false; // Don't allow a plugin to ping itself
}

Expand All @@ -353,7 +425,6 @@ private function api_request( $_action, $_data ) {
'beta' => ! empty( $data['beta'] ),
);

$verify_ssl = $this->verify_ssl();
$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params ) );

if ( ! is_wp_error( $request ) ) {
Expand All @@ -370,6 +441,10 @@ private function api_request( $_action, $_data ) {
$request->banners = maybe_unserialize( $request->banners );
}

if ( $request && isset( $request->icons ) ) {
$request->icons = maybe_unserialize( $request->icons );
}

if( ! empty( $request->sections ) ) {
foreach( $request->sections as $key => $section ) {
$request->$key = (array) $section;
Expand Down Expand Up @@ -455,11 +530,17 @@ public function get_cached_version_info( $cache_key = '' ) {

$cache = get_option( $cache_key );

if( empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
if( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
return false; // Cache is expired
}

return json_decode( $cache['value'] );
// We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point.
$cache['value'] = json_decode( $cache['value'] );
if ( ! empty( $cache['value']->icons ) ) {
$cache['value']->icons = (array) $cache['value']->icons;
}

return $cache['value'];

}

Expand All @@ -470,7 +551,7 @@ public function set_version_info_cache( $value = '', $cache_key = '' ) {
}

$data = array(
'timeout' => strtotime( '+3 hours', current_time( 'timestamp' ) ),
'timeout' => strtotime( '+3 hours', time() ),
'value' => json_encode( $value )
);

Expand Down
Loading

0 comments on commit 52be76a

Please sign in to comment.