PageRenderTime 84ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/kali-forms/Inc/Backend/Views/class-extensions-page.php

https://gitlab.com/chernushov881/charity-fund
PHP | 286 lines | 160 code | 31 blank | 95 comment | 14 complexity | c466111dc3c6f90e54b0ba2df2dd029e MD5 | raw file
  1. <?php
  2. namespace KaliForms\Inc\Backend\Views;
  3. if (!defined('WPINC')) {
  4. die;
  5. }
  6. /**
  7. * Class Extensions_Page
  8. *
  9. * @package Inc\Backend\Views
  10. */
  11. class Extensions_Page
  12. {
  13. /**
  14. * Plugin slug
  15. *
  16. * @var string
  17. */
  18. protected $slug = 'kaliforms';
  19. /**
  20. * Extensions array
  21. *
  22. * @var array
  23. */
  24. protected $extensions = [];
  25. /**
  26. * Known extensions ( that we can install from our server )
  27. *
  28. * @var array
  29. */
  30. public $known_extensions = [];
  31. /**
  32. * Extensions_Page constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->get_extensions();
  37. $this->get_installed_plugins();
  38. }
  39. /**
  40. * Renders app
  41. */
  42. public function render_app()
  43. {
  44. echo '<div class="wrap">';
  45. echo '<div id="kaliforms-extensions-page">';
  46. $this->generate_extensions_html();
  47. echo '</div>';
  48. echo '<div style="display:inline-block; margin-top:15px; width:100%"><button id="kali-extensions-reload" class="button button-primary">' . esc_html__('Reload', 'kaliforms') . '</button></div>';
  49. echo '</div>';
  50. }
  51. /**
  52. * Invoking the class will render the app
  53. */
  54. public function __invoke()
  55. {
  56. /**
  57. * Initiate an action before rendering the app div
  58. */
  59. do_action($this->slug . '_before_extensions_page_rendering');
  60. /**
  61. * Echo the container
  62. */
  63. $this->render_app();
  64. /**
  65. * Initiate an action after rendering the app div
  66. */
  67. do_action($this->slug . '_after_extensions_page_rendering');
  68. }
  69. /**
  70. * Create an array of plugins
  71. *
  72. * @return void
  73. */
  74. public function get_installed_plugins()
  75. {
  76. $this->known_extensions = [
  77. 'kali-forms-pro' => '',
  78. ];
  79. foreach ($this->extensions as $k => $v) {
  80. $this->known_extensions[$v['slug']] = '';
  81. }
  82. $plugins = get_plugins();
  83. foreach ($this->known_extensions as $k => $v) {
  84. $this->known_extensions[$k] = array_key_exists($k . '/' . $k . '.php', $plugins) ? 'installed' : 'not-installed';
  85. if ($this->known_extensions[$k] === 'installed') {
  86. $this->known_extensions[$k] = is_plugin_active($k . '/' . $k . '.php') ? 'active' : 'installed';
  87. }
  88. }
  89. }
  90. /**
  91. * Gets extensions from the rest API
  92. *
  93. * @return void
  94. */
  95. public function get_extensions()
  96. {
  97. $this->extensions = get_transient($this->slug . '_extensions');
  98. if (false === $this->extensions) {
  99. $this->extensions = [];
  100. $this->make_request();
  101. }
  102. }
  103. /**
  104. * Make the request to our API
  105. *
  106. * @return void
  107. */
  108. public function make_request()
  109. {
  110. $url = KALIFORMS_EXTENSIONS_API;
  111. if (defined('KALIFORMS_PRO_EXTENSIONS_API')) {
  112. $url = KALIFORMS_PRO_EXTENSIONS_API;
  113. $arr = [
  114. 'license' => trim(get_option('kaliforms_pro_license_key')),
  115. 'url' => site_url(),
  116. ];
  117. if (empty($arr['license'])) {
  118. $arr = [];
  119. }
  120. $url = add_query_arg($arr, $url);
  121. }
  122. $response = wp_remote_get($url);
  123. if (!is_wp_error($response)) {
  124. $data = json_decode(wp_remote_retrieve_body($response), true);
  125. if (!empty($data) && is_array($data)) {
  126. set_transient($this->slug . '_extensions', $data, 2 * DAY_IN_SECONDS);
  127. $this->extensions = $data;
  128. }
  129. }
  130. }
  131. /**
  132. * Generates the extensions html
  133. *
  134. * @return void
  135. */
  136. public function generate_extensions_html()
  137. {
  138. if (empty($this->extensions)) {
  139. esc_html_e('No extensions available. Come back later!', 'kaliforms');
  140. return;
  141. }
  142. // Move pro version as the first element of array
  143. $pro = $this->extensions['kali-forms-pro'];
  144. unset($this->extensions['kali-forms-pro']);
  145. array_unshift($this->extensions, $pro);
  146. foreach ($this->extensions as $extension) {
  147. echo '<div class="plugin-card">';
  148. echo $this->_get_card_top($extension);
  149. // echo $this->_get_card_bottom($extension);
  150. echo '</div>';
  151. }
  152. }
  153. /**
  154. * Get the card bottom part
  155. *
  156. * @param [type] $extension
  157. * @return string
  158. */
  159. private function _get_card_bottom($extension)
  160. {
  161. $html = '<div class="plugin-card-bottom">';
  162. $html .= '<div class="column-updated">';
  163. $html .= '<strong>' . esc_html__('Last Updated:', 'kaliforms') . '</strong> 4 weeks ago';
  164. $html .= '</div>';
  165. $html .= '</div>';
  166. return $html;
  167. }
  168. /**
  169. * Get the card top part
  170. *
  171. * @param [type] $extension
  172. * @return string
  173. */
  174. private function _get_card_top($extension)
  175. {
  176. if (empty($extension['image'])) {
  177. $extension['image'] = KALIFORMS_URL . 'assets/img/logo.svg';
  178. }
  179. $html = '<div class="plugin-card-top">';
  180. $html .= '<div class="name column-name">';
  181. $html .= '<h3>' . esc_html($extension['name']) . '<img style="height:auto" class="plugin-icon" src="' . $extension['image'] . '" /></h3>';
  182. $html .= '</div>';
  183. $html .= $this->_get_action_links($extension);
  184. $html .= '<div class="desc column-descripton"><p>' . esc_html($extension['description']) . '</p></div>';
  185. $html .= '</div>';
  186. return $html;
  187. }
  188. /**
  189. * Plugin action links
  190. *
  191. * @param [type] $extension
  192. * @return void
  193. */
  194. private function _get_action_links($extension)
  195. {
  196. $html = '<div class="action-links">';
  197. $html .= '<ul class="plugin-action-buttons">';
  198. $html .= '<li>' . $this->_do_now_button($extension) . '</li>';
  199. $html .= '</ul>';
  200. $html .= '</div>';
  201. return $html;
  202. }
  203. /**
  204. * Render the do now button
  205. *
  206. * @param [type] $extension
  207. * @return string
  208. */
  209. private function _do_now_button($extension)
  210. {
  211. return $this->_determine_plugin_state($extension);
  212. }
  213. /**
  214. * Determines plugin state
  215. *
  216. * @param [type] $extension
  217. * @return string
  218. */
  219. private function _determine_plugin_state($extension)
  220. {
  221. $status = $this->known_extensions[$extension['slug']];
  222. $html = '';
  223. switch ($this->known_extensions[$extension['slug']]) {
  224. case 'installed';
  225. $html .= '<a class="button button-primary" data-url="' . $this->generate_activate_link($extension['slug'] . '/' . $extension['slug'] . '.php') . '" data-action="activate" data-slug="' . $extension['slug'] . '">' . esc_html__('Activate', 'kaliforms') . '</a>';
  226. break;
  227. case 'active':
  228. $html .= esc_html__('All done', 'kaliforms');
  229. break;
  230. case 'not-installed':
  231. if ('no-license' === $extension['download_link']) {
  232. $html .= '<a class"button" href="' . admin_url('edit.php?post_type=kaliforms_forms&page=kaliforms-license') . '">' . esc_html__('Please activate your license', 'kaliforms') . '</a>';
  233. } else {
  234. $html .= '<a class="button" data-action="install" data-download-url="' . $extension['download_link'] . '" data-slug="' . $extension['slug'] . '">' . esc_html__('Install Now', 'kaliforms') . '</a>';
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. if ($this->known_extensions['kali-forms-pro'] !== 'active') {
  241. $html = '<a class="button button-primary" target="_blank" href="https://www.kaliforms.com/pricing?utm_source=extensionsPage&utm_campaign=userInterests&utm_medium=upgradeToProButton">' . esc_html__('Upgrade to PRO', 'kaliforms') . '</a>';
  242. }
  243. return $html;
  244. }
  245. /**
  246. * Generates an activate link
  247. *
  248. * @return string
  249. */
  250. public function generate_activate_link($plugin)
  251. {
  252. $link = wp_nonce_url(admin_url('plugins.php?action=activate&plugin=' . $plugin), 'activate-plugin_' . $plugin);
  253. return $link;
  254. }
  255. }