PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/mu-plugins/gd-system-plugin/plugins/wp-easy-mode/includes/class-admin.php

https://gitlab.com/sksabir/support4planet
PHP | 453 lines | 243 code | 139 blank | 71 comment | 22 complexity | 9adf2e2a2eada987df3f2970f305373a MD5 | raw file
  1. <?php
  2. namespace WPEM;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. final class Admin {
  7. use Data;
  8. /**
  9. * Holds the image api instance
  10. *
  11. * @var object
  12. */
  13. private $image_api;
  14. /**
  15. * Holds the Log instance
  16. *
  17. * @var object
  18. */
  19. private $log;
  20. /**
  21. * Class constructor
  22. */
  23. public function __construct() {
  24. $this->cap = 'manage_options';
  25. $this->steps = [];
  26. add_action( 'init', [ $this, 'load' ] );
  27. }
  28. /**
  29. * Return an array of steps
  30. *
  31. * @return array
  32. */
  33. public function get_steps() {
  34. $steps = (array) $this->steps;
  35. if ( ! $steps ) {
  36. return [];
  37. }
  38. return $steps;
  39. }
  40. /**
  41. * Load admin area
  42. *
  43. * @action init
  44. */
  45. public function load() {
  46. if ( ! current_user_can( $this->cap ) ) {
  47. return;
  48. }
  49. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  50. return;
  51. }
  52. $this->log = new Log;
  53. $this->image_api = new Image_API;
  54. $this->register_steps();
  55. $this->maybe_force_redirect();
  56. add_action( 'admin_menu', [ $this, 'menu' ] );
  57. add_action( 'admin_init', [ $this, 'submit' ] );
  58. add_action( 'admin_init', [ $this, 'screen' ] );
  59. add_filter( 'option_blogname', function( $value ) {
  60. return wpem_get_step_field( 'blogname', 'settings', $value );
  61. } );
  62. add_filter( 'option_blogdescription', function( $value ) {
  63. return wpem_get_step_field( 'blogdescription', 'settings', $value );
  64. } );
  65. }
  66. /**
  67. * Determine if we are viewing the wizard
  68. *
  69. * @return bool
  70. */
  71. public function is_wizard() {
  72. return ( current_user_can( $this->cap ) && wpem()->page_slug === filter_input( INPUT_GET, 'page' ) );
  73. }
  74. /**
  75. * Register the steps used by the wizard
  76. */
  77. private function register_steps() {
  78. // Some steps depend on the image api
  79. $this->steps = [
  80. new Step_Start( $this->log ),
  81. new Step_Settings( $this->log, $this->image_api ),
  82. new Step_Contact( $this->log ),
  83. new Step_Theme( $this->log, $this->image_api ),
  84. ];
  85. foreach ( $this->steps as $i => $step ) {
  86. $step->position = $i + 1;
  87. $step->url = add_query_arg(
  88. [
  89. 'step' => $step->name,
  90. ],
  91. wpem_get_wizard_url()
  92. );
  93. }
  94. $this->last_viewed = $this->get_step_by( 'name', get_option( 'wpem_last_viewed', 'start' ) );
  95. }
  96. /**
  97. * Force the wizard to be completed
  98. */
  99. private function maybe_force_redirect() {
  100. if ( ! $this->is_wizard() ) {
  101. wp_safe_redirect( $this->last_viewed->url );
  102. exit;
  103. }
  104. $current_step = wpem_get_current_step();
  105. if ( $current_step->position <= $this->last_viewed->position ) {
  106. return;
  107. }
  108. $steps = array_slice( $this->get_steps(), $this->last_viewed->position - 1 );
  109. foreach ( $steps as $step ) {
  110. if ( $step->position === $current_step->position ) {
  111. break;
  112. }
  113. if ( ! $step->can_skip ) {
  114. wp_safe_redirect( $step->url );
  115. exit;
  116. }
  117. }
  118. }
  119. /**
  120. * Register admin menu and assets
  121. *
  122. * @action admin_menu
  123. */
  124. public function menu() {
  125. add_dashboard_page(
  126. _x( 'WP Easy Mode', 'Main plugin title', 'wp-easy-mode' ),
  127. _x( 'Easy Mode', 'Menu title', 'wp-easy-mode' ),
  128. $this->cap,
  129. wpem()->page_slug,
  130. [ $this, 'screen' ]
  131. );
  132. $suffix = SCRIPT_DEBUG ? '' : '.min';
  133. wp_register_style(
  134. 'font-awesome',
  135. wpem()->assets_url . 'css/font-awesome.min.css',
  136. [],
  137. '4.5.0'
  138. );
  139. wp_register_style(
  140. 'wpem-fullscreen',
  141. wpem()->assets_url . "css/fullscreen{$suffix}.css",
  142. [ 'dashicons', 'buttons', 'install' ],
  143. wpem()->version
  144. );
  145. wp_register_script(
  146. 'jquery-blockui',
  147. wpem()->assets_url . 'js/jquery.blockui.min.js',
  148. [ 'jquery' ],
  149. '2.70.0'
  150. );
  151. wp_register_script(
  152. 'wpem',
  153. wpem()->assets_url . "js/common{$suffix}.js",
  154. [ 'jquery' ],
  155. wpem()->version
  156. );
  157. wp_register_script(
  158. 'wpem-contact',
  159. wpem()->assets_url . "js/contact{$suffix}.js",
  160. [ 'wpem' ],
  161. wpem()->version
  162. );
  163. wp_register_script(
  164. 'wpem-theme',
  165. wpem()->assets_url . "js/theme{$suffix}.js",
  166. [ 'wpem', 'wp-pointer', 'wpem-pointers' ],
  167. wpem()->version
  168. );
  169. wp_localize_script(
  170. 'wpem',
  171. 'wpem_vars',
  172. [
  173. 'step' => wpem_get_current_step()->name,
  174. 'i18n' => [
  175. 'exit_confirm' => esc_attr__( 'Are you sure you want to exit and configure WordPress on your own?', 'wp-easy-mode' ),
  176. ],
  177. ]
  178. );
  179. /**
  180. * Filter the list of themes to display
  181. *
  182. * @var array
  183. */
  184. $themes = (array) apply_filters(
  185. 'wpem_themes',
  186. [
  187. 'twentysixteen',
  188. 'twentyfifteen',
  189. 'twentyfourteen',
  190. ]
  191. );
  192. wp_localize_script(
  193. 'wpem-theme',
  194. 'wpem_theme_vars',
  195. [
  196. 'themes' => array_map( 'esc_js', array_values( array_unique( $themes ) ) ),
  197. 'i18n' => [
  198. 'expand' => esc_attr__( 'Expand Sidebar', 'wp-easy-mode' ),
  199. 'collapse' => esc_attr__( 'Collapse Sidebar', 'wp-easy-mode' ),
  200. ],
  201. 'preview_url' => static::demo_site_url(
  202. [
  203. 'blogname' => get_option( 'blogname' ),
  204. 'blogdescription' => get_option( 'blogdescription' ),
  205. 'email' => wpem_get_contact_info( 'email' ),
  206. 'phone' => wpem_get_contact_info( 'phone' ),
  207. 'fax' => wpem_get_contact_info( 'fax' ),
  208. 'address' => wpem_get_contact_info( 'address' ),
  209. 'social' => implode( ',', wpem_get_social_profiles() ),
  210. ],
  211. false
  212. ),
  213. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  214. 'customizer_url' => wpem_get_customizer_url(
  215. [
  216. 'return' => self_admin_url(),
  217. 'wpem' => 1,
  218. ]
  219. ),
  220. ]
  221. );
  222. }
  223. /**
  224. * Return a URL for the demo API
  225. *
  226. * @param array $args (optional)
  227. * @param bool $hide_empty_args (optional)
  228. *
  229. * @return string
  230. */
  231. public static function demo_site_url( $args = [], $hide_empty_args = true ) {
  232. $defaults = [
  233. 'site_type' => wpem_get_site_type(),
  234. 'site_industry' => wpem_get_site_industry(),
  235. 'lang' => get_locale(),
  236. ];
  237. $args = array_merge( $defaults, $args );
  238. $args = ( $hide_empty_args ) ? array_filter( $args ) : $args;
  239. return add_query_arg(
  240. array_map( 'urlencode', $args ),
  241. esc_url_raw( wpem()->api_url )
  242. );
  243. }
  244. /**
  245. * Listen for POST requests and process them
  246. *
  247. * @action admin_init
  248. */
  249. public function submit() {
  250. $nonce = filter_input( INPUT_POST, 'wpem_step_nonce' );
  251. $name = filter_input( INPUT_POST, 'wpem_step_name' );
  252. if ( false === wp_verify_nonce( $nonce, sprintf( 'wpem_step_nonce-%s-%d', $name, get_current_user_id() ) ) ) {
  253. return;
  254. }
  255. $step = $this->get_step_by( 'name', $name );
  256. if ( ! $step ) {
  257. return;
  258. }
  259. $took = filter_input( INPUT_POST, 'wpem_step_took' );
  260. if ( $took ) {
  261. $this->log->add_step_time( $took );
  262. }
  263. $step->callback();
  264. $next_step = wpem_get_next_step();
  265. if ( $next_step ) {
  266. update_option( 'wpem_last_viewed', $next_step->name );
  267. wp_safe_redirect( $next_step->url );
  268. exit;
  269. }
  270. new Done( $this->log );
  271. }
  272. /**
  273. * Register admin menu screen
  274. *
  275. * @action admin_init
  276. */
  277. public function screen() {
  278. $template = wpem()->base_dir . 'templates/fullscreen.php';
  279. if ( is_readable( $template ) ) {
  280. require_once $template;
  281. exit;
  282. }
  283. }
  284. /**
  285. * Get a step by name or actual position
  286. *
  287. * @param string $field
  288. * @param mixed $value
  289. *
  290. * @return object
  291. */
  292. public function get_step_by( $field, $value ) {
  293. $steps = (array) $this->steps;
  294. if ( empty( $steps ) || empty( $value ) ) {
  295. return;
  296. }
  297. if ( 'name' === $field ) {
  298. foreach ( $steps as $step ) {
  299. if ( $step->name !== $value ) {
  300. continue;
  301. }
  302. return $step;
  303. }
  304. }
  305. if ( 'position' === $field && is_numeric( $value ) ) {
  306. foreach ( $steps as $step ) {
  307. if ( $step->position !== $value ) {
  308. continue;
  309. }
  310. return $step;
  311. }
  312. }
  313. }
  314. }