| 1 |
<?php |
| 2 |
|
| 3 |
namespace ConvertPro; |
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; // Called directly, nothing to do here. |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
use ConvertPro\Controller\Controller; |
| 10 |
|
| 11 |
/** |
| 12 |
* Admin Pages Handler |
| 13 |
*/ |
| 14 |
class Admin |
| 15 |
{ |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('admin_menu', [$this, 'admin_menu']); |
| 20 |
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* The admin screen our own assets belong on. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $hook = ''; |
| 29 |
|
| 30 |
/** |
| 31 |
* Register our menu page |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function admin_menu() |
| 36 |
{ |
| 37 |
global $submenu; |
| 38 |
|
| 39 |
$capability = 'manage_options'; |
| 40 |
$slug = 'convertpro-settings'; |
| 41 |
|
| 42 |
$hook = add_menu_page(__('EasyTest', 'convertpro'), __('EasyTest', 'convertpro'), $capability, $slug, [$this, 'ab_tester_settings'], 'dashicons-text'); |
| 43 |
|
| 44 |
$this->hook = $hook; |
| 45 |
// add_submenu_page($slug, __('Settings', 'convertpro'), __('Settings', 'convertpro'), $capability, 'convertpro-settings', [$this, 'ab_tester_settings']); |
| 46 |
// if (current_user_can($capability)) { |
| 47 |
// $submenu[$slug][] = array(__('App', 'convertpro'), $capability, 'admin.php?page=' . $slug . '#/'); |
| 48 |
// $submenu[$slug][] = array(__('Settings', 'convertpro'), $capability, 'admin.php?page=' . $slug . '#/settings'); |
| 49 |
// } |
| 50 |
|
| 51 |
// add_action('load-' . $hook, [$this, 'init_hooks']); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Load scripts and styles for the app |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function enqueue_scripts($hook_suffix = '') |
| 61 |
{ |
| 62 |
// Only on our own screen. These were loading on every page of wp-admin, |
| 63 |
// which meant a stylesheet with a plain `p` rule restyling other people's |
| 64 |
// screens, select2 downloaded for nothing, and a query for every |
| 65 |
// published slug on the site running on every admin request. |
| 66 |
if ($hook_suffix !== $this->hook) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
wp_enqueue_style('convertpro-admin'); |
| 71 |
wp_enqueue_style('select2-style'); |
| 72 |
wp_enqueue_script('test-variations-admin'); |
| 73 |
wp_enqueue_script('ab-tester-select2'); |
| 74 |
|
| 75 |
wp_localize_script('test-variations-admin', 'convertproForm', $this->form_validation_data()); |
| 76 |
// wp_enqueue_script('test-variations-admin', CONVERTPRO_ASSETS . '/js/test-variation.js', ['jquery'], CONVERTPRO_VERSION, true); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Everything the test form needs to check itself as the user types. |
| 81 |
* |
| 82 |
* Passing the taken slugs and URLs up front means the form can point out a |
| 83 |
* clash straight away instead of waiting for a save to bounce back. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
private function form_validation_data() |
| 88 |
{ |
| 89 |
global $wpdb; |
| 90 |
|
| 91 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 92 |
$editing = isset($_GET['id']) ? (int) $_GET['id'] : 0; |
| 93 |
|
| 94 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 95 |
$taken_slugs = $wpdb->get_col("SELECT post_name FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ('page','post')"); |
| 96 |
|
| 97 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 98 |
$taken_uris = $wpdb->get_col( |
| 99 |
$wpdb->prepare("SELECT test_uri FROM {$wpdb->prefix}convertpro WHERE id != %d AND test_uri != ''", $editing) |
| 100 |
); |
| 101 |
|
| 102 |
return array( |
| 103 |
'takenSlugs' => array_values(array_filter((array) $taken_slugs)), |
| 104 |
'takenUris' => array_values(array_filter((array) $taken_uris)), |
| 105 |
'i18n' => array( |
| 106 |
'nameMissing' => __('Give the test a name so you can recognise it later.', 'convertpro'), |
| 107 |
'typeMissing' => __('Pick whether you are testing whole pages or an element.', 'convertpro'), |
| 108 |
'uriMissing' => __('Add a test URL. This is the link you share.', 'convertpro'), |
| 109 |
'uriSlash' => __('No slashes here. Use letters, numbers, dashes and underscores.', 'convertpro'), |
| 110 |
'uriTakenContent' => __('You already have a page at this address. Pick something else, or that page would start redirecting people.', 'convertpro'), |
| 111 |
'uriTakenTest' => __('Another test already uses this URL.', 'convertpro'), |
| 112 |
'pageMissing' => __('Choose the page this version sends visitors to.', 'convertpro'), |
| 113 |
'pageDuplicate' => __('Another version already uses this page, so there would be nothing to compare.', 'convertpro'), |
| 114 |
'pageIsConversion' => __('This is your conversion page, so everyone would convert the moment they arrive.', 'convertpro'), |
| 115 |
'classMissing' => __('Add a CSS class, then paste it onto the matching element in your page builder.', 'convertpro'), |
| 116 |
'classDuplicate' => __('Another version already uses this class. Each one needs its own.', 'convertpro'), |
| 117 |
'percentRange' => __('Use a share between 1 and 100.', 'convertpro'), |
| 118 |
/* translators: %s: current total of the shares. */ |
| 119 |
'percentTotal' => __('The shares add up to %s. They need to total 100.', 'convertpro'), |
| 120 |
'conversionMissing' => __('Choose the page that counts as a conversion.', 'convertpro'), |
| 121 |
'selectorMissing' => __('Add what to watch for a click, such as .buy-now or part of the link address.', 'convertpro'), |
| 122 |
'needTwo' => __('A test needs at least two versions to compare.', 'convertpro'), |
| 123 |
'fixBeforeSaving' => __('Fix the highlighted fields before saving.', 'convertpro'), |
| 124 |
), |
| 125 |
); |
| 126 |
} |
| 127 |
/** |
| 128 |
* ab_tester_settings |
| 129 |
* settings page include |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function ab_tester_settings() |
| 133 |
{ |
| 134 |
$testcon = new Controller(); |
| 135 |
$testcon->Run(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Render our admin page |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function plugin_page() |
| 144 |
{ |
| 145 |
echo '<div class="wrap"><div id="vue-admin-app">Heelo</div></div>'; |
| 146 |
} |
| 147 |
|
| 148 |
// admin fronted js |
| 149 |
} |
| 150 |
|