| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Controller; |
| 4 |
|
| 5 |
use Calotes\Component\Response; |
| 6 |
use Calotes\Helper\Route; |
| 7 |
use WP_Defender\Controller; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Tutorial |
| 11 |
* @package WP_Defender\Controller |
| 12 |
*/ |
| 13 |
class Tutorial extends Controller { |
| 14 |
public $slug = 'wdf-tutorial'; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
// Check if tutorials should be hidden. |
| 18 |
$hide = apply_filters( 'wpmudev_branding_hide_doc_link', false ); |
| 19 |
if ( ! $hide ) { |
| 20 |
$this->register_page( |
| 21 |
esc_html__( 'Tutorials', 'wpdef' ), |
| 22 |
$this->slug, |
| 23 |
array( |
| 24 |
&$this, |
| 25 |
'main_view', |
| 26 |
), |
| 27 |
$this->parent_slug |
| 28 |
); |
| 29 |
add_action( 'defender_enqueue_assets', array( &$this, 'enqueue_assets' ) ); |
| 30 |
$this->register_routes(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Enqueue assets & output data. |
| 36 |
*/ |
| 37 |
public function enqueue_assets() { |
| 38 |
if ( ! $this->is_page_active() ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
wp_localize_script( 'def-tutorial', 'tutorial', $this->data_frontend() ); |
| 42 |
wp_enqueue_script( 'def-tutorial' ); |
| 43 |
$this->enqueue_main_assets(); |
| 44 |
} |
| 45 |
|
| 46 |
public function main_view() { |
| 47 |
$this->render( 'main' ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
public function is_show() { |
| 54 |
return ! get_site_option( 'wp_defender_hide_tutorials' ) && ! apply_filters( 'wpmudev_branding_hide_doc_link', false ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
public function to_array() { |
| 61 |
[$routes, $nonces] = Route::export_routes( 'tutorial' ); |
| 62 |
|
| 63 |
return array( |
| 64 |
'show' => $this->is_show(), |
| 65 |
'endpoints' => $routes, |
| 66 |
'nonces' => $nonces, |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Hide tutorials. |
| 72 |
* |
| 73 |
* @return Response |
| 74 |
* @defender_route |
| 75 |
*/ |
| 76 |
public function hide() { |
| 77 |
update_site_option( 'wp_defender_hide_tutorials', true ); |
| 78 |
|
| 79 |
return new Response( |
| 80 |
true, |
| 81 |
array( |
| 82 |
'message' => sprintf( |
| 83 |
/* translators: %s - tutorial link */ |
| 84 |
__( |
| 85 |
"The widget has been removed. You can check all defender tutorials at the <a href=\"%s\">tutorials' tab</a> at any time.", |
| 86 |
'wpdef' |
| 87 |
), |
| 88 |
network_admin_url( 'admin.php?page=wdf-tutorial' ) |
| 89 |
), |
| 90 |
) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public function remove_settings() { |
| 95 |
delete_site_option( 'wp_defender_hide_tutorials' ); |
| 96 |
} |
| 97 |
|
| 98 |
public function remove_data() { |
| 99 |
delete_site_option( 'wp_defender_hide_tutorials' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* All the variables that we will show on frontend, both in the main page, or dashboard widget. |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
public function data_frontend() { |
| 108 |
return array_merge( |
| 109 |
array( |
| 110 |
'show' => $this->is_show(), |
| 111 |
), |
| 112 |
$this->dump_routes_and_nonces() |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset. |
| 118 |
* |
| 119 |
* @param array $data |
| 120 |
*/ |
| 121 |
public function import_data( $data ) {} |
| 122 |
|
| 123 |
/** |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function export_strings() { |
| 127 |
return array(); |
| 128 |
} |
| 129 |
} |
| 130 |
|