rebase from live enviornment

This commit is contained in:
Rachit Bhargava
2024-01-09 22:14:20 -05:00
parent ff0b49a046
commit 3a22fcaa4a
15968 changed files with 2344674 additions and 45234 deletions

View File

@@ -0,0 +1,413 @@
<?php
/**
* Admin functions.
*
* @package BSF core
*/
if ( ! function_exists( 'bsf_generate_rand_token' ) ) {
/**
* Generate 32 characters random token.
*
* @return string
*/
function bsf_generate_rand_token() {
$valid_characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
$token = '';
$length = 32;
for ( $n = 1; $n < $length; $n++ ) {
$which_character = wp_rand( 0, strlen( $valid_characters ) - 1 );
$token .= $valid_characters[ $which_character ];
}
return $token;
}
}
/**
* Update version numbers of all the brainstorm products in options `brainstorm_products` and `brainstrom_bundled_products`
*
* @todo Current version numbers can be fetched from WordPress at runtime whenever ruquired,
* Remote version can only be required when transient for update data is deleted (i hope)
*/
if ( ! function_exists( 'bsf_update_all_product_version' ) ) {
/**
* Updates all product versions.
*
* @return void
*/
function bsf_update_all_product_version() {
$brainstrom_products = get_option( 'brainstrom_products', array() );
$brainstrom_bundled_products = get_option( 'brainstrom_bundled_products', array() );
$bsf_product_themes = array();
if ( ! empty( $brainstrom_products ) ) :
$bsf_product_plugins = ( isset( $brainstrom_products['plugins'] ) ) ? $brainstrom_products['plugins'] : array();
$bsf_product_themes = ( isset( $brainstrom_products['themes'] ) ) ? $brainstrom_products['themes'] : array();
endif;
$bundled_product_updated = false;
if ( ! empty( $bsf_product_plugins ) ) {
foreach ( $bsf_product_plugins as $key => $plugin ) {
if ( ! isset( $plugin['id'] ) || empty( $plugin['id'] ) ) {
continue;
}
if ( ! isset( $plugin['template'] ) || empty( $plugin['template'] ) ) {
continue;
}
if ( ! isset( $plugin['type'] ) || empty( $plugin['type'] ) ) {
continue;
}
$version = ( isset( $plugin['version'] ) ) ? $plugin['version'] : '';
$current_version = bsf_get_current_version( $plugin['template'], $plugin['type'] );
$name = bsf_get_current_name( $plugin['template'], $plugin['type'] );
if ( '' !== $name ) {
$brainstrom_products['plugins'][ $key ]['product_name'] = $name;
}
if ( '' !== $current_version ) {
if ( version_compare( $version, $current_version ) === - 1 || 1 === version_compare( $version, $current_version ) ) {
$brainstrom_products['plugins'][ $key ]['version'] = $current_version;
}
}
}
}
if ( ! empty( $bsf_product_themes ) ) {
foreach ( $bsf_product_themes as $key => $theme ) {
if ( ! isset( $theme['id'] ) || empty( $theme['id'] ) ) {
continue;
}
if ( ! isset( $theme['template'] ) || empty( $theme['template'] ) ) {
continue;
}
if ( ! isset( $theme['type'] ) || empty( $theme['type'] ) ) {
continue;
}
$version = ( isset( $theme['version'] ) ) ? $theme['version'] : '';
$current_version = bsf_get_current_version( $theme['template'], $theme['type'] );
$name = bsf_get_current_name( $theme['template'], $theme['type'] );
if ( '' !== $name ) {
$brainstrom_products['themes'][ $key ]['product_name'] = $name;
}
if ( '' !== $current_version || false !== $current_version ) {
if ( version_compare( $version, $current_version ) === - 1 || 1 === version_compare( $version, $current_version ) ) {
$brainstrom_products['themes'][ $key ]['version'] = $current_version;
}
}
}
}
if ( ! empty( $brainstrom_bundled_products ) ) {
foreach ( $brainstrom_bundled_products as $keys => $bps ) {
$version = '';
if ( strlen( $keys ) > 1 ) {
foreach ( $bps as $key => $bp ) {
if ( ! isset( $bp->id ) || '' === $bp->id ) {
continue;
}
$version = $bp->version;
$current_version = bsf_get_current_version( $bp->init, $bp->type );
if ( '' !== $current_version && false !== $current_version ) {
if ( 1 === - version_compare( $version, $current_version ) || 1 === version_compare( $version, $current_version ) ) {
if ( is_object( $brainstrom_bundled_products ) ) {
$brainstrom_bundled_products = array( $brainstrom_bundled_products );
}
$single_bp = $brainstrom_bundled_products[ $keys ];
$single_bp[ $key ]->version = $current_version;
$bundled_product_updated = true;
$brainstrom_bundled_products[ $keys ] = $single_bp;
}
}
}
} else {
if ( ! isset( $bps->id ) || '' === $bps->id ) {
continue;
}
$version = $bps->version;
$current_version = bsf_get_current_version( $bps->init, $bps->type );
if ( '' !== $current_version || false !== $current_version ) {
if ( - 1 === version_compare( $version, $current_version ) || 1 === version_compare( $version, $current_version ) ) {
$brainstrom_bundled_products[ $keys ]->version = $current_version;
$bundled_product_updated = true;
}
}
}
}
}
update_option( 'brainstrom_products', $brainstrom_products );
if ( $bundled_product_updated ) {
update_option( 'brainstrom_bundled_products', $brainstrom_bundled_products );
}
}
}
add_action( 'admin_init', 'bsf_update_all_product_version', 1000 );
if ( ! function_exists( 'bsf_get_current_version' ) ) {
/**
* Get current version of plugin / theme.
*
* @param string $template plugin template/slug.
* @param string $type type of product.
*
* @return float
*/
function bsf_get_current_version( $template, $type ) {
if ( '' === $template ) {
return false;
}
if ( 'theme' === $type || 'themes' === $type ) {
$theme = wp_get_theme( $template );
$version = $theme->get( 'Version' );
} elseif ( 'plugin' === $type || 'plugins' === $type ) {
$plugin_file = rtrim( WP_PLUGIN_DIR, '/' ) . '/' . $template;
if ( ! is_file( $plugin_file ) ) {
return false;
}
$plugin = get_plugin_data( $plugin_file );
$version = $plugin['Version'];
}
return $version;
}
}
if ( ! function_exists( 'bsf_get_current_name' ) ) {
/**
* Get name of plugin / theme.
*
* @param string $template plugin template/slug.
* @param string $type type of product.
* @return string
*/
function bsf_get_current_name( $template, $type ) {
if ( '' === $template ) {
return false;
}
if ( 'theme' === $type || 'themes' === $type ) {
$theme = wp_get_theme( $template );
$name = $theme->get( 'Name' );
} elseif ( 'plugin' === $type || 'plugins' === $type ) {
$plugin_file = rtrim( WP_PLUGIN_DIR, '/' ) . '/' . $template;
if ( ! is_file( $plugin_file ) ) {
return false;
}
$plugin = get_plugin_data( $plugin_file );
$name = $plugin['Name'];
}
return $name;
}
}
add_action( 'admin_notices', 'bsf_notices', 1000 );
add_action( 'network_admin_notices', 'bsf_notices', 1000 );
if ( ! function_exists( 'bsf_notices' ) ) {
/**
* Display admin notices.
*
* @return bool
*/
function bsf_notices() {
global $pagenow;
if ( 'update-core.php' === $pagenow || 'plugins.php' === $pagenow || 'post-new.php' === $pagenow || 'edit.php' === $pagenow || 'post.php' === $pagenow ) {
$brainstrom_products = get_option( 'brainstrom_products' );
$brainstrom_bundled_products = get_option( 'brainstrom_bundled_products', array() );
if ( empty( $brainstrom_products ) ) {
return false;
}
$brainstrom_bundled_products_keys = array();
if ( ! empty( $brainstrom_bundled_products ) ) :
foreach ( $brainstrom_bundled_products as $bps ) {
foreach ( $bps as $key => $bp ) {
array_push( $brainstrom_bundled_products_keys, $bp->id );
}
}
endif;
$mix = array();
$plugins = ( isset( $brainstrom_products['plugins'] ) ) ? $brainstrom_products['plugins'] : array();
$themes = ( isset( $brainstrom_products['themes'] ) ) ? $brainstrom_products['themes'] : array();
$mix = array_merge( $plugins, $themes );
if ( empty( $mix ) ) {
return false;
}
if ( ( defined( 'BSF_PRODUCTS_NOTICES' ) && ( 'false' === BSF_PRODUCTS_NOTICES || false === BSF_PRODUCTS_NOTICES ) ) ) {
return false;
}
$is_multisite = is_multisite();
$is_network_admin = is_network_admin();
foreach ( $mix as $product ) :
if ( ! isset( $product['id'] ) ) {
continue;
}
if ( false === apply_filters( "bsf_display_product_activation_notice_{$product['id']}", true ) ) {
continue;
}
if ( isset( $product['is_product_free'] ) && ( 'true' === $product['is_product_free'] || true === $product['is_product_free'] ) ) {
continue;
}
$constant = strtoupper( str_replace( '-', '_', $product['id'] ) );
$constant_nag = 'BSF_' . $constant . '_NAG';
$constant_notice = 'BSF_' . $constant . '_NOTICES';
if ( defined( $constant_nag ) && ( 'false' === constant( $constant_nag ) || false === constant( $constant_nag ) ) ) {
continue;
}
if ( defined( $constant_notice ) && ( 'false' === constant( $constant_notice ) || false === constant( $constant_notice ) ) ) {
continue;
}
$status = ( isset( $product['status'] ) ) ? $product['status'] : false;
$type = ( isset( $product['type'] ) ) ? $product['type'] : false;
if ( ! $type ) {
continue;
}
if ( 'plugin' === $type ) {
if ( ! is_plugin_active( $product['template'] ) ) {
continue;
}
} elseif ( 'theme' === $type ) {
$theme = wp_get_theme();
if ( $product['template'] !== $theme->template ) {
continue;
}
} else {
continue;
}
if ( BSF_Update_Manager::bsf_is_product_bundled( $product['id'] ) ) {
continue;
}
if ( 'registered' !== $status ) :
$url = bsf_registration_page_url( '', $product['id'] );
$message = __( 'Please', 'bsf' ) . ' <a href="' . esc_url( $url ) . '" class="bsf-core-license-form-btn" plugin-slug="' . esc_html( $product['id'] ) . '">' . __( 'activate', 'bsf' ) . '</a> ' . __( 'your copy of the', 'bsf' ) . ' <i>' . esc_html( $product['product_name'] ) . '</i> ' . __( 'to get update notifications, access to support features & other resources!', 'bsf' );
$message = apply_filters( "bsf_product_activation_notice_{$product['id']}", $message, $url, $product['product_name'] );
$allowed_html = array(
'a' => array(
'href' => array(),
'class' => array(),
'title' => array(),
'plugin-slug' => array(),
),
'br' => array(),
'em' => array(),
'strong' => array(),
'i' => array(),
);
if ( ( $is_multisite && $is_network_admin ) || ! $is_multisite ) {
echo '<div class="notice notice-warning"><p>' . wp_kses( $message, $allowed_html ) . '</p></div>';
}
endif;
endforeach;
}
}
}
// delete bundled products after switch theme.
if ( ! function_exists( 'bsf_theme_deactivation' ) ) {
/**
* Delete transients while switching theme.
*
* @return void
*/
function bsf_theme_deactivation() {
update_option( 'bsf_force_check_extensions', false );
}
}
add_action( 'switch_theme', 'bsf_theme_deactivation' );
add_action( 'deactivated_plugin', 'bsf_theme_deactivation' );
if ( ! function_exists( 'bsf_get_free_menu_position' ) ) {
/**
* Get free theme position.
*
* @param int $start menu position priority index.
* @param float $increment increment number for menu position.
* @return int
*/
function bsf_get_free_menu_position( $start, $increment = 0.3 ) {
foreach ( $GLOBALS['menu'] as $key => $menu ) {
$menus_positions[] = $key;
}
if ( ! in_array( $start, $menus_positions, true ) ) {
return $start;
}
/* the position is already reserved find the closet one */
while ( in_array( $start, $menus_positions, true ) ) {
$start += $increment;
}
return $start;
}
}
if ( ! function_exists( 'bsf_get_option' ) ) {
/**
* Get free theme position.
*
* @param bool $request return complete option data OR a single variable.
* @return array
*/
function bsf_get_option( $request = false ) {
$bsf_options = get_option( 'bsf_options' );
if ( ! $request ) {
return $bsf_options;
} else {
return ( isset( $bsf_options[ $request ] ) ) ? $bsf_options[ $request ] : false;
}
}
}
if ( ! function_exists( 'bsf_update_option' ) ) {
/**
* Update bsf option with key and value.
*
* @param string $request variable key.
* @param string $value variable value.
* @return bool
*/
function bsf_update_option( $request, $value ) {
$bsf_options = get_option( 'bsf_options' );
$bsf_options[ $request ] = $value;
return update_option( 'bsf_options', $bsf_options );
}
}
if ( ! function_exists( 'bsf_sort' ) ) {
/**
* Sort array of objects.
*
* @param string $a The first string.
* @param string $b The second string.
* @return int
*/
function bsf_sort( $a, $b ) {
return strcmp( strtolower( $a->short_name ), strtolower( $b->short_name ) );
}
}

View File

@@ -0,0 +1,239 @@
<?php
/**
* Product update functions.
*
* @package BSF core
*/
// Alternative function for wp_remote_get.
if ( ! function_exists( 'bsf_get_remote_version' ) ) {
/**
* Get remote version for product
*
* @param array $product_list products data.
* @param array $products products data (deprecated).
* @return array
*/
function bsf_get_remote_version( $product_list, $products ) {
global $ultimate_referer;
$path = bsf_get_api_url() . '?referer=' . $ultimate_referer;
$data = array(
'action' => 'bsf_get_product_versions',
'ids' => $products,
'products' => $product_list,
'site_url' => get_site_url(),
'php_version' => bsf_get_php_version(),
'wp_version' => get_bloginfo( 'version' ),
'locale' => get_locale(),
'bsf_core_version' => BSF_UPDATER_VERSION,
'active_theme' => get_template(),
'active_stylesheet' => get_stylesheet(),
'php_max_input_vars' => ini_get( 'max_input_vars' ), // phpcs:ignore:PHPCompatibility.IniDirectives.NewIniDirectives.max_input_varsFound
'php_post_max_size' => ini_get( 'post_max_size' ),
'php_max_execution_time' => ini_get( 'max_execution_time' ),
'php_memory_limit' => ini_get( 'memory_limit' ),
);
$request = wp_remote_post(
$path,
array(
'body' => $data,
'timeout' => '10',
)
);
// Request http URL if the https version fails.
if ( is_wp_error( $request ) && 200 !== wp_remote_retrieve_response_code( $request ) ) {
$path = bsf_get_api_url( true ) . '?referer=' . $ultimate_referer;
$request = wp_remote_post(
$path,
array(
'body' => $data,
'timeout' => '8',
)
);
}
if ( ! is_wp_error( $request ) || 200 === wp_remote_retrieve_response_code( $request ) ) {
$result = json_decode( wp_remote_retrieve_body( $request ) );
if ( ! empty( $result ) ) {
if ( empty( $result->error ) ) {
return $result->updated_versions;
} else {
return $result->error;
}
}
}
}
}
if ( ! function_exists( 'bsf_check_product_update' ) ) {
/**
* Check product updates.
*
* @return void
*/
function bsf_check_product_update() {
$is_update = true;
$registered = array(); // This list will be deprecated in favor of $product_list.
$product_list = array();
$all_products = brainstorm_get_all_products( false, false, true );
$brainstrom_products = get_option( 'brainstrom_products', array() );
$brainstrom_bundled_products = get_option( 'brainstrom_bundled_products', array() );
foreach ( $all_products as $key => $product ) {
if ( ! isset( $product['id'] ) ) {
continue;
}
$constant = strtoupper( str_replace( '-', '_', $product['id'] ) );
$constant = 'BSF_' . $constant . '_CHECK_UPDATES';
if ( defined( $constant ) && ( 'false' === constant( $constant ) || false === constant( $constant ) ) ) {
continue;
}
$registered[] = $product['id'];
$product_list[ $product['id'] ] = array(
'id' => $product['id'],
'installed_version' => $product['version'],
);
// Add bundled products to the list.
if ( isset( $brainstrom_bundled_products[ $product['id'] ] ) ) {
$bundled_products = $brainstrom_bundled_products[ $product['id'] ];
$bundled_product_data = array();
foreach ( $bundled_products as $bundled_product ) {
$bundled_product_data[ $bundled_product->id ] = array(
'id' => $bundled_product->id,
'installed_version' => $bundled_product->version,
);
}
$product_list[ $product['id'] ]['bundled_products'] = $bundled_product_data;
}
}
$remote_versions = bsf_get_remote_version( $product_list, $registered );
$bsf_product_plugins = isset( $brainstrom_products['plugins'] ) ? $brainstrom_products['plugins'] : array();
$bsf_product_themes = isset( $brainstrom_products['themes'] ) ? $brainstrom_products['themes'] : array();
if ( false !== $remote_versions ) {
if ( ! empty( $remote_versions ) ) {
$is_bundled_update = false;
foreach ( $remote_versions as $rkey => $remote_data ) {
$rid = ( isset( $remote_data->id ) ) ? (string) $remote_data->id : '';
$remote_version = ( isset( $remote_data->remote_version ) ) ? $remote_data->remote_version : '';
$in_house = ( isset( $remote_data->in_house ) ) ? $remote_data->in_house : '';
$on_market = ( isset( $remote_data->on_market ) ) ? $remote_data->on_market : '';
$is_product_free = ( isset( $remote_data->is_product_free ) ) ? $remote_data->is_product_free : '';
$short_name = ( isset( $remote_data->short_name ) ) ? $remote_data->short_name : '';
$changelog_url = ( isset( $remote_data->changelog_url ) ) ? $remote_data->changelog_url : '';
$purchase_url = ( isset( $remote_data->purchase_url ) ) ? $remote_data->purchase_url : '';
$version_beta = ( isset( $remote_data->version_beta ) ) ? $remote_data->version_beta : '';
$download_url = ( isset( $remote_data->download_url ) ) ? $remote_data->download_url : '';
$download_url_beta = ( isset( $remote_data->download_url_beta ) ) ? $remote_data->download_url_beta : '';
$tested_upto = ( isset( $remote_data->tested ) ) ? $remote_data->tested : '';
foreach ( $bsf_product_plugins as $key => $plugin ) {
if ( ! isset( $plugin['id'] ) ) {
continue;
}
$pid = (string) $plugin['id'];
if ( $pid === $rid ) {
$brainstrom_products['plugins'][ $key ]['remote'] = $remote_version;
$brainstrom_products['plugins'][ $key ]['in_house'] = $in_house;
$brainstrom_products['plugins'][ $key ]['on_market'] = $on_market;
$brainstrom_products['plugins'][ $key ]['is_product_free'] = $is_product_free;
$brainstrom_products['plugins'][ $key ]['short_name'] = $short_name;
$brainstrom_products['plugins'][ $key ]['changelog_url'] = $changelog_url;
$brainstrom_products['plugins'][ $key ]['purchase_url'] = $purchase_url;
$brainstrom_products['plugins'][ $key ]['version_beta'] = $version_beta;
$brainstrom_products['plugins'][ $key ]['download_url_beta'] = $download_url_beta;
$brainstrom_products['plugins'][ $key ]['download_url'] = $download_url;
$brainstrom_products['plugins'][ $key ]['tested'] = $tested_upto;
// Deregister status for plugin.
if ( isset( $remote_data->status ) && 0 === $remote_data->status ) {
$brainstrom_products['plugins'][ $key ]['status'] = 'not-registered';
} else {
$brainstrom_products['plugins'][ $key ]['status'] = 'registered';
}
$is_update = true;
}
}
foreach ( $bsf_product_themes as $key => $theme ) {
if ( ! isset( $theme['id'] ) ) {
continue;
}
$pid = $theme['id'];
if ( $pid === $rid ) {
$brainstrom_products['themes'][ $key ]['remote'] = $remote_version;
$brainstrom_products['themes'][ $key ]['in_house'] = $in_house;
$brainstrom_products['themes'][ $key ]['on_market'] = $on_market;
$brainstrom_products['themes'][ $key ]['is_product_free'] = $is_product_free;
$brainstrom_products['themes'][ $key ]['short_name'] = $short_name;
$brainstrom_products['themes'][ $key ]['changelog_url'] = $changelog_url;
$brainstrom_products['themes'][ $key ]['purchase_url'] = $purchase_url;
$brainstrom_products['themes'][ $key ]['version_beta'] = $version_beta;
$brainstrom_products['themes'][ $key ]['download_url'] = $download_url;
$brainstrom_products['themes'][ $key ]['download_url_beta'] = $download_url_beta;
$is_update = true;
// Deregister status for theme.
if ( isset( $remote_data->status ) && 0 === $remote_data->status ) {
$brainstrom_products['themes'][ $key ]['status'] = 'not-registered';
} else {
$brainstrom_products['themes'][ $key ]['status'] = 'registered';
}
}
}
if ( isset( $remote_data->bundled_products ) && ! empty( $remote_data->bundled_products ) ) {
if ( ! empty( $brainstrom_bundled_products ) && is_array( $brainstrom_bundled_products ) ) {
foreach ( $brainstrom_bundled_products as $bkeys => $bps ) {
foreach ( $bps as $bkey => $bp ) {
if ( ! isset( $bp->id ) ) {
continue;
}
foreach ( $remote_data->bundled_products as $rbp ) {
if ( ! isset( $rbp->id ) ) {
continue;
}
if ( $rbp->id === $bp->id ) {
$bprd = $brainstrom_bundled_products[ $bkeys ];
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->remote = $rbp->remote_version;
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->parent = $rbp->parent;
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->short_name = $rbp->short_name;
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->changelog_url = $rbp->changelog_url;
if ( isset( $rbp->download_url ) ) {
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->download_url = $rbp->download_url;
}
if ( isset( $rbp->download_url_beta ) ) {
$brainstrom_bundled_products[ $bkeys ][ $bkey ]->download_url_beta = $rbp->download_url_beta;
}
$is_bundled_update = true;
}
}
}
}
}
}
}
if ( $is_bundled_update ) {
update_option( 'brainstrom_bundled_products', $brainstrom_bundled_products );
}
}
}
if ( $is_update ) {
update_option( 'brainstrom_products', $brainstrom_products );
}
}
}