woocommerce/plugins/woocommerce-beta-tester/includes/class-wc-beta-tester-admin-...

320 lines
11 KiB
PHP
Raw Normal View History

2018-06-05 08:37:08 +00:00
<?php
/**
* Beta Tester Admin Menus class
*
* @package WC_Beta_Tester
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Beta_Tester_Admin_Menus Main Class.
*/
class WC_Beta_Tester_Admin_Menus {
/**
* Constructor
*/
public function __construct() {
if ( class_exists( 'WC_Admin_Status' ) ) {
2018-06-07 11:55:40 +00:00
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 50 );
}
add_action( 'admin_footer', array( $this, 'version_information_template' ) );
2018-06-05 08:37:08 +00:00
}
/**
* Get issue template from GitHub.
*
* @return string
*/
protected function get_ticket_template() {
$bug_tpl = wp_remote_retrieve_body( wp_remote_get( 'https://raw.githubusercontent.com/woocommerce/woocommerce/master/.github/ISSUE_TEMPLATE/Bug_report.md' ) );
$begin = strpos( $bug_tpl, '**Describe the bug**' );
if ( $begin ) {
$bug_tpl = substr( $bug_tpl, $begin );
}
return $bug_tpl;
}
/**
* Get info about the current theme for SSR.
*
2018-06-25 11:35:57 +00:00
* @param string $theme Current theme.
* @return string
*/
private function get_theme_ssr_info( $theme ) {
$theme_info = __( "\n\n### Theme ###\n\n", 'woocommerce-beta-tester' );
$version = $theme['version'];
if ( version_compare( $theme['version'], $theme['version_latest'], '<' ) ) {
/* translators: 1: latest version */
$version .= sprintf( __( ' - %s is available', 'woocommerce-beta-tester' ), esc_html( $theme['version_latest'] ) );
}
2018-06-07 13:40:48 +00:00
$child_theme = $theme['is_child_theme'] ? __( 'Yes', 'woocommerce-beta-tester' ) : __( 'No - If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme.', 'woocommerce-beta-tester' );
if ( $theme['is_child_theme'] ) {
$parent_version = $theme['parent_version'];
if ( version_compare( $theme['parent_version'], $theme['parent_version_latest'], '<' ) ) {
/* translators: 1: latest version */
$parent_version .= sprintf( __( ' - %s is available', 'woocommerce-beta-tester' ), esc_html( $theme['parent_version_latest'] ) );
}
$child_theme .= sprintf(
__( "Parent theme name: %1\$s\nParent theme version: %2\$s\nParent theme author URL: %3\$s\n", 'woocommerce-beta-tester' ),
$theme['parent_name'],
$parent_version,
$theme['parent_author_url']
);
}
$theme_info .= sprintf(
__( "Name: %1\$s\nVersion: %2\$s\nAuthor URL: %3\$s\nChild Theme: %4\$s\nWooCommerce Support: %5\$s", 'woocommerce-beta-tester' ),
$theme['name'],
$version,
esc_html( $theme['author_url'] ),
$child_theme,
! $theme['has_woocommerce_support'] ? __( 'Not declared', 'woocommerce-beta-tester' ) : __( 'Yes', 'woocommerce-beta-tester' )
);
return $theme_info;
}
/**
* Get info about the current plugins for SSR.
*
2018-06-25 11:35:57 +00:00
* @param array $active_plugins List of active plugins.
* @param array $untested_plugins List of untested plugins.
* @return string
*/
private function get_plugins_ssr_info( $active_plugins, $untested_plugins ) {
$plugins_info = __( "\n\n### Plugins ###\n\n", 'woocommerce-beta-tester' );
foreach ( $active_plugins as $plugin ) {
if ( ! empty( $plugin['name'] ) ) {
// Link the plugin name to the plugin url if available.
$plugin_name = esc_html( $plugin['name'] );
$version_string = '';
$network_string = '';
if ( strstr( $plugin['url'], 'woothemes.com' ) || strstr( $plugin['url'], 'woocommerce.com' ) ) {
if ( ! empty( $plugin['version_latest'] ) && version_compare( $plugin['version_latest'], $plugin['version'], '>' ) ) {
/* translators: %s: plugin latest version */
$version_string = sprintf( esc_html__( '%s is available', 'woocommerce-beta-tester' ), $plugin['version_latest'] );
}
2018-06-25 11:35:57 +00:00
if ( false !== (bool) $plugin['network_activated'] ) {
$network_string = __( 'Network enabled', 'woocommerce-beta-tester' );
}
}
$untested_string = '';
if ( array_key_exists( $plugin['plugin'], $untested_plugins ) ) {
$untested_string = __( 'Not tested with the active version of WooCommerce', 'woocommerce-beta-tester' );
}
$plugins_info .= sprintf( __( "%1\$s: by %2\$s - %3\$s - %4\$s\n", 'woocommerce-beta-tester' ), $plugin_name, $plugin['author_name'], $plugin['version'], $version_string . $untested_string . $network_string );
}
}
return $plugins_info;
}
/**
* Constructs a WC System Status Report.
*
* @return string
*/
protected function construct_ssr() {
$transient_name = 'wc-beta-tester-ssr';
2018-06-05 17:36:09 +00:00
$ssr = get_transient( $transient_name );
if ( false === $ssr ) {
// Get SSR.
$api = new WC_REST_System_Status_Controller();
$schema = $api->get_item_schema();
$mappings = $api->get_item_mappings();
$response = array();
foreach ( $mappings as $section => $values ) {
foreach ( $values as $key => $value ) {
if ( isset( $schema['properties'][ $section ]['properties'][ $key ]['type'] ) ) {
settype( $values[ $key ], $schema['properties'][ $section ]['properties'][ $key ]['type'] );
}
}
settype( $values, $schema['properties'][ $section ]['type'] );
$response[ $section ] = $values;
}
$ssr = '';
foreach ( $response['environment'] as $key => $value ) {
$index = $key;
2018-06-05 19:04:11 +00:00
// @todo remove this hack after fix schema in WooCommerce, and Claudio never let you folks forget that need to write schema first, and review after every change, schema is the most important part of the REST API.
if ( 'version' === $index ) {
$index = 'wc_version';
} elseif ( 'external_object_cache' === $index ) {
$ssr .= sprintf( "%s: %s\n", 'External object cache', wc_bool_to_string( $value ) );
continue;
}
if ( is_bool( $value ) ) {
$value = wc_bool_to_string( $value );
}
$ssr .= sprintf( "%s: %s\n", $schema['properties']['environment']['properties'][ $index ]['description'], $value );
}
$active_plugins = $api->get_active_plugins();
2018-06-25 11:35:57 +00:00
$plugin_updates = new WC_Plugin_Updates();
$untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, 'minor' );
$ssr .= $this->get_plugins_ssr_info( $active_plugins, $untested_plugins );
$theme = $api->get_theme_info();
$ssr .= $this->get_theme_ssr_info( $theme );
set_transient( $transient_name, $ssr, DAY_IN_SECONDS );
}
return $ssr;
}
/**
* Get URL for creating a GitHub ticket.
*
* @return string
*/
protected function get_github_ticket_url() {
$bug_tpl = $this->get_ticket_template();
$ssr = $this->construct_ssr();
$body = str_replace( 'Copy and paste the system status report from **WooCommerce > System Status** in WordPress admin.', $ssr, $bug_tpl );
$body = str_replace( '```', '', $body ); // Remove since this break how is displayed.
$wc_plugin_data = get_plugin_data( WC_PLUGIN_FILE );
if ( isset( $wc_plugin_data['Version'] ) ) {
$version = $wc_plugin_data['Version'];
} else {
$version = '-';
}
return add_query_arg(
array(
2018-06-05 17:36:09 +00:00
'body' => rawurlencode( $body ),
/* translators: %s: woocommerce version */
'title' => rawurlencode( sprintf( __( '[WC Beta Tester] Bug report for version "%s"', 'woocommerce-beta-tester' ), $version ) ),
),
'https://github.com/woocommerce/woocommerce/issues/new'
);
}
2018-06-05 08:37:08 +00:00
/**
* Add the "Visit Store" link in admin bar main menu.
*
* @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
*/
public function admin_bar_menus( $wp_admin_bar ) {
if ( ! is_admin() || ! is_user_logged_in() ) {
return;
}
// Show only when the user is a member of this site, or they're a super admin.
if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
return;
}
// Add the beta tester root node.
$wp_admin_bar->add_node( array(
'parent' => 0,
'id' => 'wc-beta-tester',
'title' => __( 'WC Beta Tester', 'woocommerce-beta-tester' ),
) );
2018-06-06 15:14:32 +00:00
$settings = WC_Beta_Tester::get_settings();
switch ( $settings->channel ) {
case 'beta':
$current_channel = __( 'Beta', 'woocommerce-beta-tester' );
break;
case 'rc':
$current_channel = __( 'Release Candidate', 'woocommerce-beta-tester' );
break;
default:
$current_channel = __( 'Stable', 'woocommerce-beta-tester' );
break;
2018-06-05 11:45:09 +00:00
}
2018-06-05 08:37:08 +00:00
$nodes = array(
array(
'parent' => 'wc-beta-tester',
2018-06-07 11:55:40 +00:00
'id' => 'update-channel',
2018-06-05 17:36:09 +00:00
/* translators: %s: current channel */
2018-06-07 11:55:40 +00:00
'title' => sprintf( __( 'Current channel: %s', 'woocommerce-beta-tester' ), $current_channel ),
'href' => admin_url( 'plugins.php?page=wc-beta-tester' ),
2018-06-05 08:37:08 +00:00
),
array(
'parent' => 'wc-beta-tester',
'id' => 'show-version-info',
2018-06-07 11:55:40 +00:00
/* translators: %s: current version */
2018-06-25 11:35:57 +00:00
'title' => sprintf( __( 'Release %s information', 'woocommerce-beta-tester' ), WC_VERSION ),
'href' => '#',
2018-06-05 08:37:08 +00:00
),
array(
'parent' => 'wc-beta-tester',
'id' => 'switch-version',
2018-06-07 11:55:40 +00:00
'title' => __( 'Switch versions', 'woocommerce-beta-tester' ),
'href' => admin_url( 'tools.php?page=wc-beta-tester-version-picker' ),
2018-06-05 08:37:08 +00:00
),
array(
'parent' => 'wc-beta-tester',
2018-06-07 11:55:40 +00:00
'id' => 'submit-gh-ticket',
'title' => __( 'Submit bug report', 'woocommerce-beta-tester' ),
'href' => '#',
'meta' => array(
// We can't simply use the href here since WP core calls esc_url on it which strips some parts.
'onclick' => 'javascript:window.open( "' . esc_js( $this->get_github_ticket_url() ) . '" );',
),
2018-06-05 08:37:08 +00:00
),
);
foreach ( $nodes as $node ) {
$wp_admin_bar->add_node( $node );
}
}
/**
2018-06-06 12:32:45 +00:00
* Template for version information.
*/
public function version_information_template() {
?>
<script type="text/template" id="tmpl-wc-beta-tester-version-info">
<div class="wc-backbone-modal wc-backbone-modal-beta-tester-version-info">
<div class="wc-backbone-modal-content">
<section class="wc-backbone-modal-main" role="main">
<header class="wc-backbone-modal-header">
2018-06-06 08:07:45 +00:00
<h1>
<?php
/* translators: %s: version number */
2018-06-25 11:35:57 +00:00
echo esc_html( sprintf( __( 'Release %s information', 'woocommerce-beta-tester' ), '{{ data.version }}' ) );
2018-06-06 08:07:45 +00:00
?>
</h1>
<button class="modal-close modal-close-link dashicons dashicons-no-alt">
<span class="screen-reader-text"><?php esc_html_e( 'Close modal panel', 'woocommerce-beta-tester' ); ?></span>
</button>
</header>
<article>
<?php do_action( 'woocommerce_admin_version_information_start' ); ?>
{{ data.description }}
<?php do_action( 'woocommerce_admin_version_information_end' ); ?>
</article>
<footer>
2018-06-25 11:35:57 +00:00
<a target="_blank" href="https://github.com/woocommerce/woocommerce/releases/tag/{{ data.version }}"><?php esc_html_e( 'Read more on GitHub', 'woocommerce-beta-tester' ); ?></a>
</footer>
</section>
</div>
</div>
<div class="wc-backbone-modal-backdrop modal-close"></div>
</script>
<?php
}
2018-06-05 08:37:08 +00:00
}
return new WC_Beta_Tester_Admin_Menus();