PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/essential-addons-for-elementor-lite/includes/Traits/Library.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 367 lines | 221 code | 49 blank | 97 comment | 36 complexity | c9a82fab65104a3b24f5f380dd8c1042 MD5 | raw file
  1. <?php
  2. namespace Essential_Addons_Elementor\Traits;
  3. use Elementor\Plugin;
  4. if (!defined('ABSPATH')) {
  5. exit;
  6. } // Exit if accessed directly
  7. trait Library
  8. {
  9. public $a;
  10. /**
  11. * Return array of registered elements.
  12. *
  13. * @todo filter output
  14. */
  15. public $OPTION_CLEAR_HOOK = "eael_remove_unused_options_data";
  16. public function get_registered_elements()
  17. {
  18. return array_keys($this->registered_elements);
  19. }
  20. /**
  21. * Return saved settings
  22. *
  23. * @since 3.0.0
  24. */
  25. public function get_settings($element = null)
  26. {
  27. $defaults = array_fill_keys(array_keys(array_merge($this->registered_elements, $this->registered_extensions)), true);
  28. $elements = get_option('eael_save_settings', $defaults);
  29. $elements = array_merge($defaults, $elements);
  30. return (isset($element) ? (isset($elements[$element]) ? $elements[$element] : 0) : array_keys(array_filter($elements)));
  31. }
  32. /**
  33. * @param $page_obj
  34. * @param $key
  35. * @return string
  36. */
  37. public function get_extension_settings($page_settings = [], $global_settings = [], $extension = '', $key = '')
  38. {
  39. if (isset($page_settings) && $page_settings->get_settings($extension) == 'yes') {
  40. return $page_settings->get_settings($key);
  41. } else if (isset($global_settings[$extension]['enabled'])) {
  42. return isset($global_settings[$extension][$key]) ? $global_settings[$extension][$key] : '';
  43. }
  44. return '';
  45. }
  46. /**
  47. * @param $id
  48. * @param $global_data
  49. * @return string
  50. */
  51. public function get_typography_data($id, $global_data)
  52. {
  53. $typo_data = '';
  54. $fields_keys = [
  55. 'font_family',
  56. 'font_weight',
  57. 'text_transform',
  58. 'font_style',
  59. 'text_decoration',
  60. 'font_size',
  61. 'letter_spacing',
  62. 'line_height',
  63. ];
  64. foreach ($fields_keys as $key => $field) {
  65. $typo_attr = $global_data[$id . '_' . $field];
  66. $attr = str_replace('_', '-', $field);
  67. if (in_array($field, ['font_size', 'letter_spacing', 'line_height'])) {
  68. if (!empty($typo_attr['size'])) {
  69. $typo_data .= "{$attr}:{$typo_attr['size']}{$typo_attr['unit']} !important;";
  70. }
  71. } elseif (!empty($typo_attr)) {
  72. $typo_data .= ($attr == 'font-family') ? "{$attr}:{$typo_attr}, sans-serif;" : "{$attr}:{$typo_attr};";
  73. }
  74. }
  75. return $typo_data;
  76. }
  77. /**
  78. * Check if assets files exists
  79. *
  80. * @since 3.0.0
  81. */
  82. public function has_assets_files($uid = null, $ext = ['css', 'js'])
  83. {
  84. if (!is_array($ext)) {
  85. $ext = (array) $ext;
  86. }
  87. foreach ($ext as $e) {
  88. $path = EAEL_ASSET_PATH . DIRECTORY_SEPARATOR . ($uid ? $uid : 'eael') . '.min.' . $e;
  89. if (!is_readable($this->safe_path($path))) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * Remove files
  97. *
  98. * @since 3.0.0
  99. */
  100. public function remove_files($uid = null, $ext = ['css', 'js'])
  101. {
  102. foreach ($ext as $e) {
  103. $path = EAEL_ASSET_PATH . DIRECTORY_SEPARATOR . ($uid ? $uid : 'eael') . '.min.' . $e;
  104. if (file_exists($path)) {
  105. unlink($path);
  106. }
  107. }
  108. do_action( 'eael_remove_assets', $uid, $ext );
  109. }
  110. /**
  111. * Remove files in dir
  112. *
  113. * @since 3.0.0
  114. */
  115. public function empty_dir($path)
  116. {
  117. if (!is_dir($path) || !file_exists($path)) {
  118. return;
  119. }
  120. foreach (scandir($path) as $item) {
  121. if ($item == '.' || $item == '..') {
  122. continue;
  123. }
  124. unlink($this->safe_path($path . DIRECTORY_SEPARATOR . $item));
  125. }
  126. }
  127. /**
  128. * Clear cache files
  129. *
  130. * @since 3.0.0
  131. */
  132. /**
  133. * Check if wp running in background
  134. *
  135. * @since 3.0.0
  136. */
  137. public function is_running_background()
  138. {
  139. if (wp_doing_cron()) {
  140. return true;
  141. }
  142. if (wp_doing_ajax()) {
  143. return true;
  144. }
  145. if (!empty($_REQUEST['action']) && !$this->check_background_action( sanitize_text_field( $_REQUEST['action'] ) )) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Check if elementor edit mode or not
  152. *
  153. * @since 3.0.0
  154. */
  155. public function is_edit_mode()
  156. {
  157. if (isset($_REQUEST['elementor-preview'])) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Check if elementor edit mode or not
  164. *
  165. * @since 3.0.0
  166. */
  167. public function is_preview_mode()
  168. {
  169. if (isset($_REQUEST['elementor-preview'])) {
  170. return false;
  171. }
  172. if (!empty($_REQUEST['action']) && !$this->check_background_action( sanitize_text_field( $_REQUEST['action'] ) )) {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * Check if a plugin is installed
  179. *
  180. * @since v3.0.0
  181. */
  182. public function is_plugin_installed($basename)
  183. {
  184. if (!function_exists('get_plugins')) {
  185. include_once ABSPATH . '/wp-admin/includes/plugin.php';
  186. }
  187. $installed_plugins = get_plugins();
  188. return isset($installed_plugins[$basename]);
  189. }
  190. /**
  191. * Generate safe path
  192. *
  193. * @since v3.0.0
  194. */
  195. public function safe_path($path)
  196. {
  197. $path = str_replace(['//', '\\\\'], ['/', '\\'], $path);
  198. return str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
  199. }
  200. /**
  201. * Generate safe url
  202. *
  203. * @since v3.0.0
  204. */
  205. public function safe_url($url)
  206. {
  207. if (is_ssl()) {
  208. $url = wp_parse_url($url);
  209. if (!empty($url['host'])) {
  210. $url['scheme'] = 'https';
  211. }
  212. return $this->unparse_url($url);
  213. }
  214. return $url;
  215. }
  216. public function unparse_url($parsed_url)
  217. {
  218. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  219. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  220. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  221. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  222. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  223. $pass = ($user || $pass) ? "$pass@" : '';
  224. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  225. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  226. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  227. return "$scheme$user$pass$host$port$path$query$fragment";
  228. }
  229. /**
  230. * Allow to load asset for some pre defined action query param in elementor preview
  231. * @return bool
  232. */
  233. public function check_background_action($action_name){
  234. $allow_action = [
  235. 'subscriptions',
  236. 'mepr_unauthorized',
  237. 'home',
  238. 'subscriptions',
  239. 'payments',
  240. 'newpassword',
  241. 'manage_sub_accounts',
  242. 'ppw_postpass',
  243. ];
  244. if (in_array($action_name, $allow_action)){
  245. return true;
  246. }
  247. return false;
  248. }
  249. /*
  250. * Check some other cookie for solve asset loading issue
  251. */
  252. public function check_third_party_cookie_status($id='') {
  253. global $Password_Protected;
  254. if ( is_object( $Password_Protected ) && method_exists( $Password_Protected, 'cookie_name' ) && isset( $_COOKIE[ $Password_Protected->cookie_name() ] ) ) {
  255. return true;
  256. }
  257. return false;
  258. }
  259. /**
  260. * check_protected_content_status
  261. *
  262. * check EaeL Protected content cookie set or not
  263. *
  264. * @return bool
  265. */
  266. public function check_protected_content_status(){
  267. if(!empty($_POST['eael_protected_content_id'])){
  268. if(!empty($_POST['protection_password_'.$_POST['eael_protected_content_id']])){
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. /**
  275. * eael_job_init
  276. *
  277. * Register Cron Event
  278. *
  279. * @access public
  280. * @return void
  281. * @since 5.0.6
  282. */
  283. public function eael_job_init() {
  284. add_action( $this->OPTION_CLEAR_HOOK, [ $this, 'remove_unused_options_data' ] );
  285. if ( ! wp_next_scheduled( $this->OPTION_CLEAR_HOOK ) ) {
  286. wp_schedule_event( time(), 'daily', $this->OPTION_CLEAR_HOOK );
  287. }
  288. }
  289. /**
  290. * remove_unused_options_data
  291. * Remove unused eael related fields from wp_option for increase site speed.
  292. * This method attached with wp schedule cron job and run one time daily
  293. *
  294. * @access public
  295. * @return void
  296. * @since 5.0.6
  297. */
  298. public function remove_unused_options_data() {
  299. global $wpdb;
  300. $sql = "from {$wpdb->options} as options_tb
  301. inner join (SELECT option_id FROM {$wpdb->options}
  302. WHERE ((option_name like '%\_eael_elements' and LENGTH(option_name) = 23 and option_value = 'a:0:{}' )
  303. or (option_name like '%\_eael_custom_js' and LENGTH(option_name) = 24 and (option_value IS NULL or option_value = '')))
  304. ) AS options_tb2
  305. ON options_tb2.option_id = options_tb.option_id";
  306. $selection_sql = "select count(options_tb.option_id) as total " . $sql;
  307. $results = $wpdb->get_var( $selection_sql );
  308. if ( $results > 0 ) {
  309. $deletiation_sql = "delete options_tb " . $sql;
  310. $wpdb->query( $deletiation_sql );
  311. }
  312. }
  313. }