upgrade.php 0000666 00000045251 15214264240 0006717 0 ustar 00 options = &$options;
}
/**
* Check if upgrade is possible otherwise die to avoid activation
*
* @since 1.2
*/
public function can_activate() {
if ( ! $this->can_upgrade() ) {
ob_start();
$this->admin_notices(); // FIXME the error message is displayed two times
die( ob_get_contents() );
}
}
/**
* Upgrades if possible otherwise returns false to stop Polylang loading
*
* @since 1.2
*
* @return bool true if upgrade is possible, false otherwise
*/
public function upgrade() {
if ( ! $this->can_upgrade() ) {
add_action( 'all_admin_notices', array( $this, 'admin_notices' ) );
return false;
}
add_action( 'admin_init', array( $this, '_upgrade' ) );
return true;
}
/**
* Check if we the previous version is not too old
* Upgrades if OK
* /!\ never start any upgrade before admin_init as it is likely to conflict with some other plugins
*
* @since 1.2
*
* @return bool true if upgrade is possible, false otherwise
*/
public function can_upgrade() {
// Don't manage upgrade from version < 0.8
return version_compare( $this->options['version'], '0.8', '>=' );
}
/**
* Displays a notice when ugrading from a too old version
*
* @since 1.0
*/
public function admin_notices() {
load_plugin_textdomain( 'polylang', false, basename( POLYLANG_DIR ) . '/languages' );
printf(
'
',
esc_html__( 'Polylang has been deactivated because you upgraded from a too old version.', 'polylang' ),
sprintf(
/* translators: %s are Polylang version numbers */
esc_html__( 'Please upgrade first to %s before ugrading to %s.', 'polylang' ),
'0.9.8',
POLYLANG_VERSION
)
);
}
/**
* Upgrades the plugin depending on the previous version
*
* @since 1.2
*/
public function _upgrade() {
foreach ( array( '0.9', '1.0', '1.1', '1.2', '1.2.1', '1.2.3', '1.3', '1.4', '1.4.1', '1.4.4', '1.5', '1.6', '1.7.4', '1.8', '2.0.8', '2.1', '2.2' ) as $version ) {
if ( version_compare( $this->options['version'], $version, '<' ) ) {
call_user_func( array( $this, 'upgrade_' . str_replace( '.', '_', $version ) ) );
}
}
$delete_pre_1_2_data = get_transient( 'pll_upgrade_1_4' );
if ( false !== $delete_pre_1_2_data && absint( $delete_pre_1_2_data ) < time() ) {
$this->delete_pre_1_2_data();
}
$this->options['previous_version'] = $this->options['version']; // Remember the previous version of Polylang since v1.7.7
$this->options['version'] = POLYLANG_VERSION;
update_option( 'polylang', $this->options );
}
/**
* Upgrades if the previous version is < 0.9
*
* @since 1.2
*/
protected function upgrade_0_9() {
$this->options['sync'] = defined( 'PLL_SYNC' ) && ! PLL_SYNC ? 0 : 1; // The option replaces PLL_SYNC in 0.9
}
/**
* Upgrades if the previous version is < 1.0
*
* @since 1.2
*/
protected function upgrade_1_0() {
// The option replaces PLL_MEDIA_SUPPORT in 1.0
$this->options['media_support'] = defined( 'PLL_MEDIA_SUPPORT' ) && ! PLL_MEDIA_SUPPORT ? 0 : 1;
// Split the synchronization options in 1.0
$this->options['sync'] = empty( $this->options['sync'] ) ? array() : array_keys( PLL_Settings_Sync::list_metas_to_sync() );
// Set default values for post types and taxonomies to translate
$this->options['post_types'] = array_values( get_post_types( array( '_builtin' => false, 'show_ui' => true ) ) );
$this->options['taxonomies'] = array_values( get_taxonomies( array( '_builtin' => false, 'show_ui' => true ) ) );
update_option( 'polylang', $this->options );
flush_rewrite_rules(); // Rewrite rules have been modified in 1.0
}
/**
* Upgrades if the previous version is < 1.1
*
* @since 1.2
*/
protected function upgrade_1_1() {
// Update strings register with icl_register_string
$strings = get_option( 'polylang_wpml_strings' );
if ( $strings ) {
foreach ( $strings as $key => $string ) {
$strings[ $key ]['icl'] = 1;
}
update_option( 'polylang_wpml_strings', $strings );
}
// Move polylang_widgets options
if ( $widgets = get_option( 'polylang_widgets' ) ) {
$this->options['widgets'] = $widgets;
delete_option( 'polylang_widgets' );
}
}
/**
* Upgrades if the previous version is < 1.2
*
* @since 1.2
*/
protected function upgrade_1_2() {
$this->options['domains'] = array(); // Option added in 1.2
// Need to register the taxonomies
foreach ( array( 'language', 'term_language', 'post_translations', 'term_translations' ) as $taxonomy ) {
register_taxonomy( $taxonomy, null , array( 'label' => false, 'public' => false, 'query_var' => false, 'rewrite' => false ) );
}
// Abort if the db upgrade has already been done previously
if ( get_terms( 'term_language', array( 'hide_empty' => 0 ) ) ) {
return;
}
set_time_limit( 0 ); // In case we upgrade a huge site
// Upgrade old model based on metas to new model based on taxonomies
global $wpdb;
$wpdb->termmeta = $wpdb->prefix . 'termmeta'; // Registers the termmeta table in wpdb
$languages = get_terms( 'language', array( 'hide_empty' => 0 ) ); // Don't use get_languages_list which can't work with the old model
foreach ( $languages as $lang ) {
// First update language with new storage for locale and text direction
$text_direction = get_metadata( 'term', $lang->term_id, '_rtl', true );
$desc = serialize( array( 'locale' => $lang->description, 'rtl' => $text_direction ) );
wp_update_term( (int) $lang->term_id, 'language', array( 'description' => $desc ) );
// Add language to new 'term_language' taxonomy
$term_lang = wp_insert_term( $lang->name, 'term_language', array( 'slug' => 'pll_' . $lang->slug ) );
$lang_tt_ids[ $lang->term_id ] = $term_lang['term_taxonomy_id']; // Keep the term taxonomy id for future
}
// Get all terms with a language defined
$terms = $wpdb->get_results( "SELECT term_id, meta_value FROM $wpdb->termmeta WHERE meta_key = '_language'" );
foreach ( $terms as $key => $term ) {
$terms[ $key ] = $wpdb->prepare( '( %d, %d )', $term->term_id, $lang_tt_ids[ $term->meta_value ] );
}
$terms = array_unique( $terms );
// Assign language to each term
if ( ! empty( $terms ) ) {
$wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $terms ) );
}
// Translations
foreach ( array( 'post', 'term' ) as $type ) {
$table = $type . 'meta';
$terms = $slugs = $tts = $trs = array();
// Get all translated objects
$objects = $wpdb->get_col( "SELECT DISTINCT meta_value FROM {$wpdb->$table} WHERE meta_key = '_translations'" );
if ( empty( $objects ) ) {
continue;
}
foreach ( $objects as $obj ) {
$term = uniqid( 'pll_' ); // The term name
$terms[] = $wpdb->prepare( '( "%s", "%s" )', $term, $term );
$slugs[] = $wpdb->prepare( '"%s"', $term );
$translations = maybe_unserialize( maybe_unserialize( $obj ) ); // 2 unserialize due to an old storage bug
$description[ $term ] = serialize( $translations );
}
$terms = array_unique( $terms );
// Insert terms
if ( ! empty( $terms ) ) {
$wpdb->query( "INSERT INTO $wpdb->terms ( slug, name ) VALUES " . implode( ',', $terms ) );
}
// Get all terms with their term_id
$terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug IN ( " . implode( ',', $slugs ) . ' )' );
// Prepare terms taxonomy relationship
foreach ( $terms as $term ) {
$tts[] = $wpdb->prepare( '( %d, "%s", "%s" )', $term->term_id, $type . '_translations', $description[ $term->slug ] );
}
$tts = array_unique( $tts );
// Insert term_taxonomy
if ( ! empty( $tts ) ) {
$wpdb->query( "INSERT INTO $wpdb->term_taxonomy ( term_id, taxonomy, description ) VALUES " . implode( ',', $tts ) );
}
// Get all terms with term_taxonomy_id
$terms = get_terms( $type . '_translations', array( 'hide_empty' => false ) );
// Prepare objects relationships
foreach ( $terms as $term ) {
$translations = unserialize( $term->description );
foreach ( $translations as $object_id ) {
if ( ! empty( $object_id ) ) {
$trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
}
}
}
$trs = array_unique( $trs );
// Insert term_relationships
if ( ! empty( $trs ) ) {
$wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) );
}
}
// Upgrade of string translations is now in upgrade_1_2_1
// Upgrade of nav menus is now in upgrade_1_2_3
}
/**
* Upgrades if the previous version is < 1.2.1
*
* @since 1.2.1
*/
protected function upgrade_1_2_1() {
// Strings translations
foreach ( get_terms( 'language', array( 'hide_empty' => 0 ) ) as $lang ) {
if ( $strings = get_option( 'polylang_mo' . $lang->term_id ) ) {
$mo = new PLL_MO();
foreach ( $strings as $msg ) {
$mo->add_entry( $mo->make_entry( $msg[0], $msg[1] ) );
}
$mo->export_to_db( $lang );
}
}
}
/**
* Upgrades if the previous version is < 1.2.3
* Uprades multilingual menus depending on the old version due to multiple changes in menus management
*
* @since 1.2.3
*/
public function upgrade_1_2_3() {
// Old version < 1.1
// Multilingal locations and switcher item were stored in a dedicated option
if ( version_compare( $this->options['version'], '1.1', '<' ) ) {
if ( $menu_lang = get_option( 'polylang_nav_menus' ) ) {
foreach ( $menu_lang as $location => $arr ) {
if ( ! in_array( $location, array_keys( get_registered_nav_menus() ) ) ) {
continue;
}
$switch_options = array_slice( $arr, -5, 5 );
$translations = array_diff_key( $arr, $switch_options );
$has_switcher = array_shift( $switch_options );
foreach ( get_terms( 'language', array( 'hide_empty' => 0 ) ) as $lang ) {
// Move nav menus locations
if ( ! empty( $translations[ $lang->slug ] ) ) {
$locations[ $location ][ $lang->slug ] = $translations[ $lang->slug ];
}
// Create the menu items for the language switcher
if ( ! empty( $has_switcher ) ) {
$menu_item_db_id = wp_update_nav_menu_item( $translations[ $lang->slug ], 0, array(
'menu-item-title' => __( 'Language switcher', 'polylang' ),
'menu-item-url' => '#pll_switcher',
'menu-item-status' => 'publish',
) );
update_post_meta( $menu_item_db_id, '_pll_menu_item', $switch_options );
}
}
}
if ( ! empty( $locations ) ) {
$this->options['nav_menus'][ get_option( 'stylesheet' ) ] = $locations;
}
delete_option( 'polylang_nav_menus' );
}
}
elseif ( empty( $this->options['nav_menus'] ) ) {
$menus = get_theme_mod( 'nav_menu_locations' );
if ( is_array( $menus ) ) {
// If old version < 1.2
// Clean the WP option as it was a bad idea to pollute it
if ( version_compare( $this->options['version'], '1.2', '<' ) ) {
foreach ( $menus as $loc => $menu ) {
if ( $pos = strpos( $loc, '#' ) ) {
unset( $menus[ $loc ] );
}
}
set_theme_mod( 'nav_menu_locations', $menus );
}
// Get the multilingual locations
foreach ( $menus as $loc => $menu ) {
foreach ( get_terms( 'language', array( 'hide_empty' => 0 ) ) as $lang ) {
$arr[ $loc ][ $lang->slug ] = pll_get_term( $menu, $lang );
}
}
if ( ! empty( $arr ) ) {
$this->options['nav_menus'][ get_option( 'stylesheet' ) ] = $arr;
}
}
}
}
/**
* Upgrades if the previous version is < 1.3
* Moves the user biographies in default language to the 'description' user meta
*
* @since 1.3
*/
protected function upgrade_1_3() {
$usermeta = 'description_' . $this->options['default_lang'];
$query = new WP_User_Query( array( 'blog_id' => $GLOBALS['blog_id'], 'meta_key' => $usermeta ) );
foreach ( $query->get_results() as $user ) {
$desc = get_user_meta( $user->ID, $usermeta, true );
if ( ! empty( $desc ) ) {
update_user_meta( $user->ID, 'description', $desc );
delete_user_meta( $user->ID, $usermeta );
}
}
}
/**
* Upgrades if the previous version is < 1.4
* Sets a transient to delete old model data
* Deletes language cache (due to bug correction in home urls in 1.3.1 and added mo_id in 1.4)
*
* @since 1.4
*/
protected function upgrade_1_4() {
set_transient( 'pll_upgrade_1_4', time() + 60 * 24 * 60 * 60 ); // 60 days
delete_transient( 'pll_languages_list' );
}
/**
* Old data were not deleted in 1.2, just in case...
* Delete them at first upgrade at least 60 days after upgrade to 1.4
*
* @since 1.4
*/
protected function delete_pre_1_2_data() {
// Suppress data of the old model < 1.2
global $wpdb;
$wpdb->termmeta = $wpdb->prefix . 'termmeta'; // registers the termmeta table in wpdb in case WP < 4.4
// Do nothing if the termmeta table does not exists
if ( count( $wpdb->get_results( "SHOW TABLES LIKE '$wpdb->termmeta'" ) ) ) {
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_translations'" );
$wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_language'" );
$wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_rtl'" );
$wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_translations'" );
}
// Delete the strings translations
$languages = get_terms( 'language', array( 'hide_empty' => false ) );
foreach ( $languages as $lang ) {
delete_option( 'polylang_mo' . $lang->term_id );
}
delete_transient( 'pll_upgrade_1_4' );
}
/**
* Upgrades if the previous version is < 1.4.1
* Disables the browser detection when using multiple domains
*
* @since 1.4.1
*/
protected function upgrade_1_4_1() {
if ( 3 == $this->options['force_lang'] ) {
$this->options['browser'] = $this->options['hide_default'] = 0;
}
}
/**
* Upgrades if the previous version is < 1.4.4
* Uprades widgets options for language filter
*
* @since 1.4.4
*/
protected function upgrade_1_4_4() {
foreach ( $GLOBALS['wp_registered_widgets'] as $widget ) {
if ( ! empty( $this->options['widgets'][ $widget['id'] ] ) && ! empty( $widget['callback'][0] ) && ! empty( $widget['params'][0]['number'] ) ) {
$obj = $widget['callback'][0];
if ( is_object( $obj ) && method_exists( $obj, 'get_settings' ) && method_exists( $obj, 'save_settings' ) ) {
$settings = $obj->get_settings();
$settings[ $widget['params'][0]['number'] ]['pll_lang'] = $this->options['widgets'][ $widget['id'] ];
$obj->save_settings( $settings );
}
}
}
unset( $this->options['widgets'] );
}
/**
* Upgrades if the previous version is < 1.5
* Deletes language cache (due to host property added and bug on search url)
*
* @since 1.5
*/
protected function upgrade_1_5() {
delete_transient( 'pll_languages_list' );
}
/**
* Upgrades if the previous version is < 1.6
* Upgrades core language files to get the .po file (only for WP 4.0+)
*
* @since 1.6
*/
protected function upgrade_1_6() {
if ( version_compare( $GLOBALS['wp_version'], '4.0', '>=' ) ) {
self::download_language_packs();
}
}
/**
* Downloads language packs
* Intended to be used only one time (at upgrade to Polylang 1.6 or first upgrade of WP 4.0 or later)
* Adapted from wp_download_language_pack
* Rewritten because wp_download_language_pack checks the existence of .mo and I need to download .po
*
* @since 1.6
*/
static function download_language_packs() {
$languages = pll_languages_list( array( 'fields' => 'locale' ) );
// Prevents upgrade if the .po file is already here. Let WP manage the upgrades :)
foreach ( $languages as $key => $locale ) {
if ( file_exists( WP_LANG_DIR . "/$locale.po" ) ) {
unset( $languages[ $key ] );
}
}
if ( empty( $languages ) ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
$translations = wp_get_available_translations();
if ( ! $translations ) {
return;
}
foreach ( $translations as $translation ) {
if ( in_array( $translation['language'], $languages ) ) {
$translation['type'] = 'core';
$translations_to_load[] = (object) $translation;
}
}
if ( ! empty( $translations_to_load ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new Language_Pack_Upgrader( new Automatic_Upgrader_Skin );
$upgrader->bulk_upgrade( $translations_to_load, array( 'clear_update_cache' => false ) );
}
}
/**
* Upgrades if the previous version is < 1.7.4
*
* @since 1.7.4
*/
protected function upgrade_1_7_4() {
delete_transient( 'pll_languages_list' ); // Deletes language cache (due to flag properties added in 1.7, page on front removed in 1.7.2, home url fixes in 1.7.4)
flush_rewrite_rules(); // Flush rewrite rules due to custom taxonomy rewrite rule bug fix
}
/**
* Upgrades if the previous version is < 1.8
*
* @since 1.8
*/
protected function upgrade_1_8() {
// Adds the flag code in languages stored in DB
include PLL_SETTINGS_INC . '/languages.php';
$terms = get_terms( 'language', array( 'hide_empty' => 0 ) );
foreach ( $terms as $lang ) {
$description = maybe_unserialize( $lang->description );
if ( isset( $languages[ $description['locale'] ] ) ) {
$description['flag_code'] = $languages[ $description['locale'] ][4];
$description = serialize( $description );
wp_update_term( (int) $lang->term_id, 'language', array( 'description' => $description ) );
}
}
delete_transient( 'pll_languages_list' );
}
/**
* Upgrades if the previous version is < 2.0.8
* Changes the user meta 'user_lang' to 'locale' to match WP 4.7 choice
*
* @since 2.0.8
*/
protected function upgrade_2_0_8() {
global $wpdb;
$wpdb->update( $wpdb->usermeta, array( 'meta_key' => 'locale' ), array( 'meta_key' => 'user_lang' ) );
}
/**
* Upgrades if the previous version is < 2.1
* Moves strings translations from polylang_mo post_content to post meta _pll_strings_translations
*
* @since 2.1
*/
protected function upgrade_2_1() {
foreach ( get_terms( 'language', array( 'hide_empty' => 0 ) ) as $lang ) {
$mo_id = PLL_MO::get_id( $lang );
$meta = get_post_meta( $mo_id, '_pll_strings_translations', true );
if ( empty( $meta ) ) {
$post = get_post( $mo_id, OBJECT );
$strings = unserialize( $post->post_content );
if ( is_array( $strings ) ) {
update_post_meta( $mo_id, '_pll_strings_translations', $strings );
}
}
}
}
/**
* Upgrades if the previous version is < 2.2
*
* @since 2.2
*/
protected function upgrade_2_2() {
delete_transient( 'pll_languages_list' ); // Deletes language cache (due to 'redirect_lang' option removed for subdomains and multiple domains)
}
}
install-base.php 0000666 00000005041 15214264240 0007637 0 ustar 00 plugin_basename = $plugin_basename;
// Manages plugin activation and deactivation
register_activation_hook( $plugin_basename, array( $this, 'activate' ) );
register_deactivation_hook( $plugin_basename, array( $this, 'deactivate' ) );
// Blog creation on multisite
add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog' ), 5 ); // Before WP attempts to send mails which can break on some PHP versions
}
/**
* Allows to detect plugin deactivation
*
* @since 1.7
*
* @return bool true if the plugin is currently beeing deactivated
*/
public function is_deactivation() {
return isset( $_GET['action'], $_GET['plugin'] ) && 'deactivate' == $_GET['action'] && $this->plugin_basename == $_GET['plugin'];
}
/**
* Activation or deactivation for all blogs
*
* @since 1.2
*
* @param string $what Either 'activate' or 'deactivate'
* @param bool $networkwide
*/
protected function do_for_all_blogs( $what, $networkwide ) {
// Network
if ( is_multisite() && $networkwide ) {
global $wpdb;
foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) as $blog_id ) {
switch_to_blog( $blog_id );
'activate' == $what ? $this->_activate() : $this->_deactivate();
}
restore_current_blog();
}
// Single blog
else {
'activate' == $what ? $this->_activate() : $this->_deactivate();
}
}
/**
* Plugin activation for multisite
*
* @since 1.7
*
* @param bool $networkwide
*/
public function activate( $networkwide ) {
$this->do_for_all_blogs( 'activate', $networkwide );
}
/**
* Plugin activation
*
* @since 0.5
*/
protected function _activate() {
// Can be overriden in child class
}
/**
* Plugin deactivation for multisite
*
* @since 0.1
*
* @param bool $networkwide
*/
public function deactivate( $networkwide ) {
$this->do_for_all_blogs( 'deactivate', $networkwide );
}
/**
* Plugin deactivation
*
* @since 0.5
*/
protected function _deactivate() {
// Can be overriden in child class
}
/**
* Blog creation on multisite ( to set default options )
*
* @since 0.9.4
*
* @param int $blog_id
*/
public function wpmu_new_blog( $blog_id ) {
switch_to_blog( $blog_id );
$this->_activate();
restore_current_blog();
}
}
install.php 0000666 00000005741 15214264240 0006736 0 ustar 00 %s',
/* translators: %s are WordPress version numbers */
sprintf( esc_html__( 'You are using WordPress %s. Polylang requires at least WordPress %s.', 'polylang' ),
esc_html( $wp_version ),
PLL_MIN_WP_VERSION
)
) );
}
$this->do_for_all_blogs( 'activate', $networkwide );
}
/**
* Get default Polylang options
*
* @since 1.8
*
* return array
*/
static public function get_default_options() {
return array(
'browser' => 1, // Default language for the front page is set by browser preference
'rewrite' => 1, // Remove /language/ in permalinks ( was the opposite before 0.7.2 )
'hide_default' => 1, // Remove URL language information for default language ( was the opposite before 2.1.5 )
'force_lang' => 1, // Add URL language information ( was 0 before 1.7 )
'redirect_lang' => 0, // Do not redirect the language page to the homepage
'media_support' => 1, // Support languages and translation for media by default
'uninstall' => 0, // Do not remove data when uninstalling Polylang
'sync' => array(), // Synchronisation is disabled by default ( was the opposite before 1.2 )
'post_types' => array(),
'taxonomies' => array(),
'domains' => array(),
'version' => POLYLANG_VERSION,
);
}
/**
* Plugin activation
*
* @since 0.5
*/
protected function _activate() {
if ( $options = get_option( 'polylang' ) ) {
// Check if we will be able to upgrade
if ( version_compare( $options['version'], POLYLANG_VERSION, '<' ) ) {
$upgrade = new PLL_Upgrade( $options );
$upgrade->can_activate();
}
}
// Defines default values for options in case this is the first installation
else {
update_option( 'polylang', self::get_default_options() );
}
// Avoid 1 query on every pages if no wpml strings is registered
if ( ! get_option( 'polylang_wpml_strings' ) ) {
update_option( 'polylang_wpml_strings', array() );
}
// Don't use flush_rewrite_rules at network activation. See #32471
// Thanks to RavanH for the trick. See https://polylang.wordpress.com/2015/06/10/polylang-1-7-6-and-multisite/
// Rewrite rules are created at next page load :)
delete_option( 'rewrite_rules' );
}
/**
* Plugin deactivation
*
* @since 0.5
*/
protected function _deactivate() {
delete_option( 'rewrite_rules' ); // Don't use flush_rewrite_rules at network activation. See #32471
}
}
plugin-updater.php 0000666 00000034304 15214264240 0010225 0 ustar 00 api_url = trailingslashit( $_api_url );
$this->api_data = $_api_data;
$this->name = plugin_basename( $_plugin_file );
$this->slug = basename( $_plugin_file, '.php' );
$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 ) );
$edd_plugin_data[ $this->slug ] = $this->api_data;
// Set up hooks.
$this->init();
}
/**
* Set up WordPress filters to hook into WP's update process.
*
* @uses add_filter()
*
* @return void
*/
public function init() {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 );
add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 );
add_action( 'admin_init', array( $this, 'show_changelog' ) );
}
/**
* Check for Updates at the defined API endpoint and modify the update array.
*
* This function dives into the update API just when WordPress creates its update array,
* then adds a custom API call and injects the custom plugin data retrieved from the API.
* It is reassembled from parts of the native WordPress plugin update code.
* See wp-includes/update.php line 121 for the original wp_update_plugins() function.
*
* @uses api_request()
*
* @param array $_transient_data Update array build by WordPress.
* @return array Modified update array with custom plugin data.
*/
public function check_update( $_transient_data ) {
global $pagenow;
if ( ! is_object( $_transient_data ) ) {
$_transient_data = new stdClass;
}
if ( 'plugins.php' == $pagenow && is_multisite() ) {
return $_transient_data;
}
if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
return $_transient_data;
}
$version_info = $this->get_cached_version_info();
if ( false === $version_info ) {
$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
$this->set_version_info_cache( $version_info );
}
if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
$_transient_data->response[ $this->name ] = $version_info;
}
$_transient_data->last_checked = current_time( 'timestamp' );
$_transient_data->checked[ $this->name ] = $this->version;
}
return $_transient_data;
}
/**
* show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
*
* @param string $file
* @param array $plugin
*/
public function show_update_notification( $file, $plugin ) {
if ( is_network_admin() ) {
return;
}
if( ! current_user_can( 'update_plugins' ) ) {
return;
}
if( ! is_multisite() ) {
return;
}
if ( $this->name != $file ) {
return;
}
// Remove our filter on the site transient
remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
$update_cache = get_site_transient( 'update_plugins' );
$update_cache = is_object( $update_cache ) ? $update_cache : new stdClass();
if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
$version_info = $this->get_cached_version_info();
if ( false === $version_info ) {
$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
$this->set_version_info_cache( $version_info );
}
if ( ! is_object( $version_info ) ) {
return;
}
if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
$update_cache->response[ $this->name ] = $version_info;
}
$update_cache->last_checked = current_time( 'timestamp' );
$update_cache->checked[ $this->name ] = $this->version;
set_site_transient( 'update_plugins', $update_cache );
} else {
$version_info = $update_cache->response[ $this->name ];
}
// Restore our filter
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
// build a plugin list row, with update notification
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
# |
echo ' |
';
echo '';
echo '';
$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' );
if ( empty( $version_info->download_link ) ) {
printf(
/* translators: %1$s plugin name, %3$s plugin version, %2$s and %4$s are html tags */
esc_html__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'polylang' ),
esc_html( $version_info->name ),
' ',
esc_html( $version_info->new_version ),
''
);
} else {
printf(
/* translators: %1$s plugin name, %3$s plugin version, %2$s, %4$s, %5$s and %6$s are html tags */
esc_html__( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'polylang' ),
esc_html( $version_info->name ),
' ',
esc_html( $version_info->new_version ),
'',
' ',
''
);
}
do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );
echo ' |
';
}
}
/**
* Updates information on the "View version x.x details" page with custom data.
*
* @uses api_request()
*
* @param mixed $_data
* @param string $_action
* @param object $_args
* @return object $_data
*/
public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
if ( $_action != 'plugin_information' ) {
return $_data;
}
if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
return $_data;
}
$to_send = array(
'slug' => $this->slug,
'is_ssl' => is_ssl(),
'fields' => array(
'banners' => array(),
'reviews' => false
)
);
$cache_key = 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) );
// Get the transient where we store the api request for this plugin for 24 hours
$edd_api_request_transient = $this->get_cached_version_info( $cache_key );
//If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
if ( empty( $edd_api_request_transient ) ) {
$api_response = $this->api_request( 'plugin_information', $to_send );
// Expires in 3 hours
$this->set_version_info_cache( $api_response, $cache_key );
if ( false !== $api_response ) {
$_data = $api_response;
}
} else {
$_data = $edd_api_request_transient;
}
// 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;
}
// 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 = $new_banners;
}
return $_data;
}
/**
* Disable SSL verification in order to prevent download update failures
*
* @param array $args
* @param string $url
* @return object $array
*/
public function http_request_args( $args, $url ) {
// If it is an https request and we are performing a package download, disable ssl verification
if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
$args['sslverify'] = false;
}
return $args;
}
/**
* Calls the API and, if successfull, returns the object delivered by the API.
*
* @uses get_bloginfo()
* @uses wp_remote_post()
* @uses is_wp_error()
*
* @param string $_action The requested action.
* @param array $_data Parameters for the API action.
* @return false|object
*/
private function api_request( $_action, $_data ) {
global $wp_version;
$data = array_merge( $this->api_data, $_data );
if ( $data['slug'] != $this->slug ) {
return;
}
if( $this->api_url == trailingslashit (home_url() ) ) {
return false; // Don't allow a plugin to ping itself
}
$api_params = array(
'edd_action' => 'get_version',
'license' => ! empty( $data['license'] ) ? $data['license'] : '',
'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
'version' => isset( $data['version'] ) ? $data['version'] : false,
'slug' => $data['slug'],
'author' => $data['author'],
'url' => home_url(),
'beta' => ! empty( $data['beta'] ),
);
$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
if ( ! is_wp_error( $request ) ) {
$request = json_decode( wp_remote_retrieve_body( $request ) );
}
if ( $request && isset( $request->sections ) ) {
$request->sections = maybe_unserialize( $request->sections );
} else {
$request = false;
}
if ( $request && isset( $request->banners ) ) {
$request->banners = maybe_unserialize( $request->banners );
}
if( ! empty( $request->sections ) ) {
foreach( $request->sections as $key => $section ) {
$request->$key = (array) $section;
}
}
return $request;
}
public function show_changelog() {
global $edd_plugin_data;
if( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
return;
}
if( empty( $_REQUEST['plugin'] ) ) {
return;
}
if( empty( $_REQUEST['slug'] ) ) {
return;
}
if( ! current_user_can( 'update_plugins' ) ) {
wp_die( __( 'You do not have permission to install plugin updates', 'polylang' ), __( 'Error', 'polylang' ), array( 'response' => 403 ) );
}
$data = $edd_plugin_data[ $_REQUEST['slug'] ];
$beta = ! empty( $data['beta'] ) ? true : false;
$cache_key = md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $beta . '_version_info' );
$version_info = $this->get_cached_version_info( $cache_key );
if( false === $version_info ) {
$api_params = array(
'edd_action' => 'get_version',
'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
'slug' => $_REQUEST['slug'],
'author' => $data['author'],
'url' => home_url(),
'beta' => ! empty( $data['beta'] )
);
$request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
if ( ! is_wp_error( $request ) ) {
$version_info = json_decode( wp_remote_retrieve_body( $request ) );
}
if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
$version_info->sections = maybe_unserialize( $version_info->sections );
} else {
$version_info = false;
}
if( ! empty( $version_info ) ) {
foreach( $version_info->sections as $key => $section ) {
$version_info->$key = (array) $section;
}
}
$this->set_version_info_cache( $version_info, $cache_key );
}
if( ! empty( $version_info ) && isset( $version_info->sections['changelog'] ) ) {
echo '' . $version_info->sections['changelog'] . '
';
}
exit;
}
public function get_cached_version_info( $cache_key = '' ) {
if( empty( $cache_key ) ) {
$cache_key = $this->cache_key;
}
$cache = get_option( $cache_key );
if( empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
return false; // Cache is expired
}
return json_decode( $cache['value'] );
}
public function set_version_info_cache( $value = '', $cache_key = '' ) {
if( empty( $cache_key ) ) {
$cache_key = $this->cache_key;
}
$data = array(
'timeout' => strtotime( '+3 hours', current_time( 'timestamp' ) ),
'value' => json_encode( $value )
);
update_option( $cache_key, $data );
}
}