PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/shapely/inc/libraries/epsilon-framework/customizer/sections/class-epsilon-section-recommended-actions.php

https://gitlab.com/chernushov881/charity-fund
PHP | 542 lines | 387 code | 50 blank | 105 comment | 29 complexity | 3b70a025b8086fb482bab575ae0500be MD5 | raw file
  1. <?php
  2. if ( ! defined( 'WPINC' ) ) {
  3. die;
  4. }
  5. /**
  6. * Class Epsilon_Section_Recommended_Actions
  7. */
  8. class Epsilon_Section_Recommended_Actions extends WP_Customize_Section {
  9. /**
  10. * The type of customize section being rendered.
  11. *
  12. * @since 1.0.0
  13. * @access public
  14. * @var string
  15. */
  16. public $type = 'epsilon-section-recommended-actions';
  17. /**
  18. * @var array
  19. */
  20. public $actions = array();
  21. /**
  22. * @var array
  23. */
  24. public $plugins = array();
  25. /**
  26. * @var string
  27. */
  28. public $theme_specific_option = '';
  29. /**
  30. * @var string
  31. */
  32. public $theme_specific_plugin_option = '';
  33. /**
  34. * @var string
  35. */
  36. public $total_actions = '';
  37. /**
  38. * @var string
  39. */
  40. public $social_text = '';
  41. /**
  42. * @var string
  43. */
  44. public $plugin_text = '';
  45. /**
  46. * @var string
  47. */
  48. public $facebook = '';
  49. /**
  50. * @var string
  51. */
  52. public $twitter = '';
  53. /**
  54. * @var bool
  55. */
  56. public $wp_review = false;
  57. /**
  58. * @var string
  59. */
  60. public $theme_slug = '';
  61. /**
  62. * Epsilon_Section_Recommended_Actions constructor.
  63. *
  64. * @param WP_Customize_Manager $manager
  65. * @param string $id
  66. * @param array $args
  67. */
  68. public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
  69. $manager->register_section_type( 'Epsilon_Section_Recommended_Actions' );
  70. $this->enqueue();
  71. parent::__construct( $manager, $id, $args );
  72. }
  73. /**
  74. * Enqueue necessary styles and scripts
  75. */
  76. public function enqueue() {
  77. wp_enqueue_style( 'plugin-install' );
  78. wp_enqueue_script( 'plugin-install' );
  79. wp_enqueue_script( 'updates' );
  80. wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
  81. 'totals' => wp_get_update_data(),
  82. ) );
  83. wp_add_inline_script( 'plugin-install', 'var pagenow = "plugin-install";' );
  84. }
  85. /**
  86. * Add custom parameters to pass to the JS via JSON.
  87. *
  88. * @since 1.0.0
  89. * @access public
  90. */
  91. public function json() {
  92. $json = parent::json();
  93. $json['action_option'] = $this->theme_specific_option;
  94. $json['plugin_option'] = $this->theme_specific_plugin_option;
  95. $json['actions'] = $this->get_actions();
  96. $json['plugins'] = $this->get_plugins();
  97. $count = 0;
  98. foreach ( $this->actions as $action ) {
  99. if ( $action['check'] ) {
  100. continue;
  101. }
  102. $count += 1;
  103. }
  104. $json['total_actions'] = $count;
  105. $json['social_text'] = $this->social_text;
  106. $json['plugin_text'] = $this->plugin_text;
  107. $json['facebook'] = $this->facebook;
  108. $json['facebook_text'] = esc_html__( 'Facebook', 'epsilon-framework' );
  109. $json['twitter'] = $this->twitter;
  110. $json['twitter_text'] = esc_html__( 'Twitter', 'epsilon-framework' );
  111. $json['wp_review'] = $this->wp_review;
  112. $json['wp_review_text'] = esc_html__( 'Review this theme on w.org', 'epsilon-framework' );
  113. if ( $this->wp_review ) {
  114. $json['theme_slug'] = $this->theme_slug;
  115. if ( empty( $this->theme_slug ) ) {
  116. $json['theme_slug'] = get_template();
  117. }
  118. }
  119. return $json;
  120. }
  121. /**
  122. * Outputs the Underscore.js template.
  123. *
  124. * @since 1.0.0
  125. * @access public
  126. * @return void
  127. */
  128. protected function render_template() {
  129. //@formatter:off
  130. ?>
  131. <li id="accordion-section-{{ data.id }}"
  132. class="accordion-section control-section control-section-{{ data.type }} cannot-expand">
  133. <h3 class="accordion-section-title">
  134. <span class="section-title" data-social="{{{ data.social_text }}}" data-plugin_text="{{{ data.plugin_text }}}">
  135. <# if( data.actions.length > 0 ){ #>
  136. {{{ data.title }}}
  137. <# }else{ #>
  138. <# if( data.plugins.length > 0 ){ #>
  139. {{{ data.plugin_text }}}
  140. <# }else{ #>
  141. {{{ data.social_text }}}
  142. <# } #>
  143. <# } #>
  144. </span>
  145. <# if( data.actions.length > 0 ){ #>
  146. <span class="epsilon-actions-count">
  147. <span class="current-index" data-total="{{{ data.total_actions }}}">1</span> / {{{ data.total_actions }}}
  148. </span>
  149. <# } #>
  150. <a href="#" class="epsilon-close-recommended-section"><span class="dashicons dashicons-arrow-up-alt2"></span></a>
  151. </h3>
  152. <div class="recommended-actions_container" id="plugin-filter">
  153. <# if( data.actions.length > 0 ){ #>
  154. <# i = 1 #>
  155. <# for (action in data.actions) { #>
  156. <div class="epsilon-recommended-actions-container"
  157. data-index="{{ i }}">
  158. <# if( !data.actions[action].check ){ #>
  159. <div class="epsilon-recommended-actions">
  160. <p class="title">{{{ data.actions[action].title }}}</p>
  161. <span data-option="{{ data.action_option }}" data-action="dismiss"
  162. class="dashicons dashicons-visibility epsilon-dismiss-required-action"
  163. id="{{ data.actions[action].id }}"></span>
  164. <div class="description">{{{ data.actions[action].description }}}</div>
  165. <# if( data.actions[action].plugin_slug ){ #>
  166. <div class="custom-action">
  167. <p class="plugin-card-{{ data.actions[action].plugin_slug }} action_button {{ data.actions[action].class }}">
  168. <a data-slug="{{ data.actions[action].plugin_slug }}"
  169. data-plugin="{{ data.actions[action].path }}"
  170. class="{{ data.actions[action].button_class }}"
  171. href="{{ data.actions[action].url }}">{{{
  172. data.actions[action].button_label }}}</a>
  173. </p>
  174. </div>
  175. <# } #>
  176. <# if( data.actions[action].help ){ #>
  177. <div class="custom-action">{{{ data.actions[action].help }}}</div>
  178. <# } #>
  179. </div>
  180. <# } #>
  181. </div>
  182. <# i++ #>
  183. <# } #>
  184. <# } #>
  185. <# if( data.plugins.length > 0 ){ #>
  186. <# for (plugin in data.plugins) { #>
  187. <div class="epsilon-recommended-actions-container epsilon-recommended-plugins" data-index="{{ data.plugins[plugin].index }}">
  188. <# if( !data.plugins[plugin].check ){ #>
  189. <div class="epsilon-recommended-plugins">
  190. <p class="title">{{{ data.plugins[plugin].title }}}</p>
  191. <span data-option="{{ data.plugin_option }}" data-action="dismiss" class="dashicons dashicons-visibility epsilon-recommended-plugin-button" id="{{ data.plugins[plugin].id }}"></span>
  192. <div class="description">{{{ data.plugins[plugin].description }}}</div>
  193. <# if( data.plugins[plugin].plugin_slug ){ #>
  194. <div class="custom-plugin">
  195. <p class="plugin-card-{{ data.plugins[plugin].plugin_slug }} action_button {{ data.plugins[plugin].class }}">
  196. <a data-slug="{{ data.plugins[plugin].plugin_slug }}" class="{{ data.plugins[plugin].button_class }} button-primary" href="{{ data.plugins[plugin].url }}">{{{ data.plugins[plugin].button_label }}}</a>
  197. </p>
  198. </div>
  199. <# } #>
  200. <# if( data.plugins[plugin].help ){ #>
  201. <div class="custom-plugin">{{{ data.plugins[plugin].help }}}</div>
  202. <# } #>
  203. </div>
  204. <# } #>
  205. </div>
  206. <# } #>
  207. <# } #>
  208. <p <# if( data.actions.length == 0 && data.plugins.length == 0 ){ #> class="succes" <# } else { #> class="succes hide" <# } #> >
  209. <# if( data.facebook ){ #>
  210. <a target="_blank" href="{{ data.facebook }}" class="button social"><span class="dashicons dashicons-facebook-alt"></span>{{{ data.facebook_text }}}</a>
  211. <# } #>
  212. <# if( data.twitter ){ #>
  213. <a target="_blank" href="{{ data.twitter }}" class="button social"><span class="dashicons dashicons-twitter"></span>{{{ data.twitter_text }}}</a>
  214. <# } #>
  215. <# if( data.wp_review ){ #>
  216. <a target="_blank" href="https://wordpress.org/support/theme/{{ data.theme_slug }}/reviews/#new-post" class="button button-primary epsilon-wordpress"><span class="dashicons dashicons-wordpress"></span>{{{ data.wp_review_text }}}</a>
  217. <# } #>
  218. </p>
  219. </div>
  220. </li>
  221. <?php
  222. //@formatter:on
  223. }
  224. /**
  225. * @return array
  226. */
  227. private function get_actions() {
  228. $arr = array();
  229. $req_actions = get_option( $this->theme_specific_option );
  230. if ( ! $req_actions ) {
  231. $req_actions = array();
  232. foreach ( $this->actions as $k => $v ) {
  233. $req_actions[ $v['id'] ] = true;
  234. }
  235. }
  236. foreach ( $this->actions as $k => $v ) {
  237. if ( $v['check'] ) {
  238. continue;
  239. }
  240. if ( isset( $req_actions[ $v['id'] ] ) && ! $req_actions[ $v['id'] ] ) {
  241. continue;
  242. }
  243. $v['index'] = $k + 1;
  244. if ( ! empty( $v['plugin_slug'] ) ) {
  245. $active = $this->_check_active( $v['plugin_slug'] );
  246. $v['url'] = $this->_create_action_link( $active['needs'], $v['plugin_slug'] );
  247. $v['class'] = '';
  248. $plugin_update = $this->_check_plugin_update( $v['plugin_slug'] );
  249. if ( 'deactivate' == $active['needs'] && ! $plugin_update ) {
  250. $active['needs'] = 'update';
  251. }
  252. if ( 'install' !== $active['needs'] && $active['status'] ) {
  253. $v['class'] = 'active';
  254. }
  255. switch ( $active['needs'] ) {
  256. case 'install':
  257. $v['button_class'] = 'install-now button';
  258. $v['button_label'] = esc_html__( 'Install', 'epsilon-framework' );
  259. break;
  260. case 'activate':
  261. $v['button_class'] = 'activate-now button button-primary';
  262. $v['button_label'] = esc_html__( 'Activate', 'epsilon-framework' );
  263. break;
  264. case 'update':
  265. $v['button_class'] = 'update-now button button-primary';
  266. $v['button_label'] = esc_html__( 'Update', 'epsilon-framework' );
  267. break;
  268. case 'deactivate':
  269. $v['button_class'] = 'deactivate-now button';
  270. $v['button_label'] = esc_html__( 'Deactivate', 'epsilon-framework' );
  271. break;
  272. }
  273. $v['path'] = $active['plugin_path'];
  274. }
  275. $arr[] = $v;
  276. };// End foreach().
  277. return $arr;
  278. }
  279. /**
  280. * @return array
  281. */
  282. private function get_plugins() {
  283. $arr = array();
  284. $req_plugins = get_option( $this->theme_specific_plugin_option );
  285. if ( ! $req_plugins ) {
  286. $req_plugins = array();
  287. foreach ( $this->plugins as $k => $v ) {
  288. $req_plugins[ $k ] = true;
  289. }
  290. }
  291. foreach ( $this->plugins as $k => $v ) {
  292. $active = $this->_check_active( $k );
  293. if ( 'deactivate' === $active['needs'] ) {
  294. continue;
  295. }
  296. if ( isset( $req_plugins[ $k ] ) && ! $req_plugins[ $k ] ) {
  297. continue;
  298. }
  299. $t = array(
  300. 'class' => '',
  301. 'id' => $k,
  302. 'path' => $active['plugin_path'],
  303. 'plugin_slug' => $k,
  304. );
  305. $t['url'] = $this->_create_action_link( $active['needs'], $k );
  306. if ( 'install' !== $active['needs'] && $active['status'] ) {
  307. $t['class'] = 'active';
  308. }
  309. switch ( $active['needs'] ) {
  310. case 'install':
  311. $t['button_class'] = 'install-now button';
  312. $t['button_label'] = esc_html__( 'Install', 'epsilon-framework' );
  313. break;
  314. case 'activate':
  315. $t['button_class'] = 'activate-now button button-primary';
  316. $t['button_label'] = esc_html__( 'Activate', 'epsilon-framework' );
  317. break;
  318. case 'update':
  319. $t['button_class'] = 'update-now button button-primary';
  320. $t['button_label'] = esc_html__( 'Update', 'epsilon-framework' );
  321. break;
  322. case 'deactivate':
  323. $t['button_class'] = 'deactivate-now button';
  324. $t['button_label'] = esc_html__( 'Deactivate', 'epsilon-framework' );
  325. break;
  326. }
  327. $info = $this->_call_plugin_api( $k );
  328. $t['description'] = isset( $info->short_description ) ? $info->short_description : '';
  329. $t['title'] = $t['button_label'] . ': ' . isset( $info->name ) ? $info->name : '';
  330. $arr[] = $t;
  331. }// End foreach().
  332. return $arr;
  333. }
  334. /**
  335. * @param string $plugin_slug
  336. *
  337. * @return array
  338. */
  339. private function _check_active( $plugin_slug = '' ) {
  340. $plugin_path = $this->_get_plugin_basename_from_slug( $plugin_slug );
  341. if ( file_exists( ABSPATH . 'wp-content/plugins/' . $plugin_path ) ) {
  342. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  343. $needs = is_plugin_active( $plugin_path ) ? 'deactivate' : 'activate';
  344. return array(
  345. 'status' => is_plugin_active( $plugin_path ),
  346. 'needs' => $needs,
  347. 'plugin_path' => $plugin_path,
  348. );
  349. }
  350. return array(
  351. 'status' => false,
  352. 'needs' => 'install',
  353. 'plugin_path' => false,
  354. );
  355. }
  356. /**
  357. * @param string $need
  358. * @param string $plugin_slug
  359. *
  360. * @return bool|string
  361. */
  362. private function _create_action_link( $need = '', $plugin_slug = '' ) {
  363. switch ( $need ) {
  364. case 'install':
  365. return wp_nonce_url(
  366. add_query_arg(
  367. array(
  368. 'action' => 'install-plugin',
  369. 'plugin' => $this->_get_plugin_basename_from_slug( $plugin_slug ),
  370. ),
  371. network_admin_url( 'update.php' )
  372. ),
  373. 'install-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug )
  374. );
  375. break;
  376. case 'deactivate':
  377. return add_query_arg(
  378. array(
  379. 'action' => 'deactivate',
  380. 'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
  381. 'plugin_status' => 'all',
  382. 'paged' => '1',
  383. '_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
  384. ),
  385. network_admin_url( 'plugins.php' )
  386. );
  387. break;
  388. case 'activate':
  389. return add_query_arg(
  390. array(
  391. 'action' => 'activate',
  392. 'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
  393. 'plugin_status' => 'all',
  394. 'paged' => '1',
  395. '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
  396. ),
  397. network_admin_url( 'plugins.php' )
  398. );
  399. break;
  400. case 'update':
  401. return wp_nonce_url(
  402. add_query_arg(
  403. array(
  404. 'action' => 'upgrade-plugin',
  405. 'plugin' => rawurlencode( $this->_get_plugin_basename_from_slug( $plugin_slug ) ),
  406. ),
  407. network_admin_url( 'update.php' )
  408. ),
  409. 'upgrade-plugin_' . $plugin_slug
  410. );
  411. break;
  412. default:
  413. return false;
  414. break;
  415. }// End switch().
  416. }
  417. /**
  418. * @param string $plugin_folder
  419. *
  420. * @return array
  421. */
  422. private function _get_plugins( $plugin_folder = '' ) {
  423. if ( ! function_exists( 'get_plugins' ) ) {
  424. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  425. }
  426. return get_plugins( $plugin_folder );
  427. }
  428. /**
  429. * @param $slug
  430. *
  431. * @return mixed
  432. */
  433. private function _get_plugin_basename_from_slug( $slug ) {
  434. $keys = array_keys( $this->_get_plugins() );
  435. foreach ( $keys as $key ) {
  436. if ( preg_match( '|^' . $slug . '/|', $key ) ) {
  437. return $key;
  438. }
  439. }
  440. return $slug;
  441. }
  442. /**
  443. * @param $slug
  444. *
  445. * @return bool
  446. */
  447. private function _check_plugin_update( $slug ) {
  448. $update_plugin_transient = get_site_transient( 'update_plugins' );
  449. if ( isset( $update_plugin_transient->response ) ) {
  450. $plugins = $update_plugin_transient->response;
  451. foreach ( $plugins as $key => $plugin ) {
  452. if ( preg_match( '|^' . $slug . '/|', $key ) ) {
  453. return false;
  454. }
  455. }
  456. }
  457. return true;
  458. }
  459. /**
  460. * @param $slug
  461. *
  462. * @return array|mixed|object|WP_Error
  463. */
  464. private function _call_plugin_api( $slug ) {
  465. include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
  466. $call_api = get_transient( 'epsilon_plugin_information_transient_' . $slug );
  467. if ( false === $call_api ) {
  468. $call_api = plugins_api( 'plugin_information', array(
  469. 'slug' => $slug,
  470. 'fields' => array(
  471. 'downloaded' => false,
  472. 'rating' => false,
  473. 'description' => false,
  474. 'short_description' => true,
  475. 'donate_link' => false,
  476. 'tags' => false,
  477. 'sections' => true,
  478. 'homepage' => true,
  479. 'added' => false,
  480. 'last_updated' => false,
  481. 'compatibility' => false,
  482. 'tested' => false,
  483. 'requires' => false,
  484. 'downloadlink' => false,
  485. 'icons' => true,
  486. ),
  487. ) );
  488. set_transient( 'epsilon_plugin_information_transient_' . $slug, $call_api, 30 * MINUTE_IN_SECONDS );
  489. }
  490. return $call_api;
  491. }
  492. }