PageRenderTime 102ms CodeModel.GetById 19ms RepoModel.GetById 8ms app.codeStats 0ms

/wp-content/plugins/live-composer-page-builder/includes/other-functions.php

https://gitlab.com/mostafame/team_website
PHP | 471 lines | 239 code | 91 blank | 141 comment | 75 complexity | 0ad6a4b727089d0ddd0ed545bfb24c19 MD5 | raw file
  1. <?php
  2. /**
  3. * Table of Contents
  4. *
  5. * class DSLC_Aq_Resize ( Image resizing class )
  6. * dslc_aq_resize ( Resize an image using DSLC_Aq_Resize Class )
  7. * dslc_get_social_count ( Returns amount of social shares a page has )
  8. * dslc_icons_current_set ( Returns the ID of the currently used set based on icon )
  9. * dslc_get_attachment_alt ( Returnt he ALT attribute for an attachment )
  10. */
  11. if ( ! class_exists( 'DSLC_Aq_Resize' ) ) {
  12. /**
  13. * Image resizing class
  14. *
  15. * @since 1.0
  16. */
  17. class DSLC_Aq_Resize {
  18. /**
  19. * The singleton instance
  20. */
  21. static private $instance = null;
  22. /**
  23. * No initialization allowed
  24. */
  25. private function __construct() {}
  26. /**
  27. * No cloning allowed
  28. */
  29. private function __clone() {}
  30. /**
  31. * For your custom default usage you may want to initialize an Aq_Resize object by yourself and then have own defaults
  32. */
  33. static public function getInstance() {
  34. if ( self::$instance == null ) {
  35. self::$instance = new self;
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * Run, forest.
  41. */
  42. public function process( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = true ) {
  43. // Validate inputs.
  44. if ( ! $url || ( ! $width && ! $height ) ) return false;
  45. $upscale = true;
  46. // Caipt'n, ready to hook.
  47. if ( true === $upscale ) add_filter( 'image_resize_dimensions', array($this, 'aq_upscale'), 10, 6 );
  48. // Define upload path & dir.
  49. $upload_info = wp_upload_dir();
  50. $upload_dir = $upload_info['basedir'];
  51. $upload_url = $upload_info['baseurl'];
  52. $http_prefix = "http://";
  53. $https_prefix = "https://";
  54. /* if the $url scheme differs from $upload_url scheme, make them match
  55. if the schemes differe, images don't show up. */
  56. if ( ! strncmp( $url, $https_prefix, strlen( $https_prefix ) ) ) { //if url begins with https:// make $upload_url begin with https:// as well
  57. $upload_url = str_replace( $http_prefix, $https_prefix, $upload_url );
  58. }
  59. elseif ( ! strncmp( $url, $http_prefix, strlen( $http_prefix ) ) ) { //if url begins with http:// make $upload_url begin with http:// as well
  60. $upload_url = str_replace( $https_prefix, $http_prefix, $upload_url );
  61. }
  62. // Check if $img_url is local.
  63. if ( false === strpos( $url, $upload_url ) ) return false;
  64. // Define path of image.
  65. $rel_path = str_replace( $upload_url, '', $url );
  66. $img_path = $upload_dir . $rel_path;
  67. // Check if img path exists, and is an image indeed.
  68. if ( ! file_exists( $img_path ) or ! getimagesize( $img_path ) ) return false;
  69. // Get image info.
  70. $info = pathinfo( $img_path );
  71. $ext = $info['extension'];
  72. list( $orig_w, $orig_h ) = getimagesize( $img_path );
  73. // Get image size after cropping.
  74. $dims = image_resize_dimensions( $orig_w, $orig_h, $width, $height, $crop );
  75. $dst_w = $dims[4];
  76. $dst_h = $dims[5];
  77. // Return the original image only if it exactly fits the needed measures.
  78. if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {
  79. $img_url = $url;
  80. $dst_w = $orig_w;
  81. $dst_h = $orig_h;
  82. } else {
  83. // Use this to check if cropped image already exists, so we can return that instead.
  84. $suffix = "{$dst_w}x{$dst_h}";
  85. $dst_rel_path = str_replace( '.' . $ext, '', $rel_path );
  86. $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
  87. if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) {
  88. // Can't resize, so return false saying that the action to do could not be processed as planned.
  89. return $url;
  90. }
  91. // Else check if cache exists.
  92. elseif ( file_exists( $destfilename ) && getimagesize( $destfilename ) ) {
  93. $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
  94. }
  95. // Else, we resize the image and return the new resized image url.
  96. else {
  97. $editor = wp_get_image_editor( $img_path );
  98. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
  99. return $url;
  100. $resized_file = $editor->save();
  101. if ( ! is_wp_error( $resized_file ) ) {
  102. $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path'] );
  103. $img_url = $upload_url . $resized_rel_path;
  104. } else {
  105. return $url;
  106. }
  107. }
  108. }
  109. // Okay, leave the ship.
  110. if ( true === $upscale ) remove_filter( 'image_resize_dimensions', array($this, 'aq_upscale') );
  111. // Return the output.
  112. if ( $single ) {
  113. // str return.
  114. $image = $img_url;
  115. } else {
  116. // array return.
  117. $image = array(
  118. 0 => $img_url,
  119. 1 => $dst_w,
  120. 2 => $dst_h
  121. );
  122. }
  123. return $image;
  124. }
  125. /**
  126. * Callback to overwrite WP computing of thumbnail measures
  127. */
  128. function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
  129. if ( ! $crop ) return null; // Let the wordpress default function handle this.
  130. // Here is the point we allow to use larger image size than the original one.
  131. $aspect_ratio = $orig_w / $orig_h;
  132. $new_w = $dest_w;
  133. $new_h = $dest_h;
  134. if ( ! $new_w ) {
  135. $new_w = intval( $new_h * $aspect_ratio );
  136. }
  137. if ( ! $new_h ) {
  138. $new_h = intval( $new_w / $aspect_ratio );
  139. }
  140. $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h );
  141. $crop_w = round( $new_w / $size_ratio );
  142. $crop_h = round( $new_h / $size_ratio );
  143. $s_x = floor( ( $orig_w - $crop_w ) / 2 );
  144. $s_y = floor( ( $orig_h - $crop_h ) / 2 );
  145. return array(0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h);
  146. }
  147. }
  148. }
  149. if ( ! function_exists( 'dslc_aq_resize' ) ) {
  150. /**
  151. * Resize an image using DSLC_Aq_Resize Class
  152. *
  153. * @since 1.0
  154. *
  155. * @param string $url The URL of the image
  156. * @param int $width The new width of the image
  157. * @param int $height The new height of the image
  158. * @param bool $crop To crop or not to crop, the question is now
  159. * @param bool $single If true only returns the URL, if false returns array
  160. * @param bool $upscale If image not big enough for new size should it upscale
  161. * @return mixed If $single is true return new image URL, if it is false return array
  162. * Array contains 0 = URL, 1 = width, 2 = height
  163. */
  164. function dslc_aq_resize( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) {
  165. if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
  166. $args = array(
  167. 'resize' => "$width,$height"
  168. );
  169. if ( $single == true ) {
  170. return jetpack_photon_url( $url, $args );
  171. } else {
  172. $image = array(
  173. 0 => $img_url,
  174. 1 => $width,
  175. 2 => $height
  176. );
  177. return jetpack_photon_url( $url, $args );
  178. }
  179. } else {
  180. $aq_resize = DSLC_Aq_Resize::getInstance();
  181. return $aq_resize->process( $url, $width, $height, $crop, $single, $upscale );
  182. }
  183. }
  184. }
  185. /**
  186. * Returns amount of social shares a page has
  187. *
  188. * @since 1.0.4
  189. *
  190. * @param int $post_ID ID of the post/page. Default false, uses get_the_ID()
  191. * @param int $refresh_in Amount of seconds for cached info to be stored. Default 3600.
  192. * @return array Array containing amount of shares. Keys are fb, twitter and pinterest.
  193. */
  194. function dslc_get_social_count( $post_ID = false, $refresh_in = 3600 ) {
  195. // If ID nt supplied use current
  196. if ( $post_ID == false ) {
  197. $post_ID = get_the_ID();
  198. }
  199. // Transient
  200. $transient_id = 'dslc_social_shares_count_' . $post_ID;
  201. if ( false === ( $share_info = get_transient( $transient_id ) ) ) {
  202. $the_url = get_permalink( $post_ID );
  203. // Defaults
  204. $share_info = array(
  205. 'fb' => 0,
  206. 'twitter' => 0,
  207. 'pinterest' => 0
  208. );
  209. // Facebook
  210. $fb_get = wp_remote_get( 'http://graph.facebook.com/?id=' . $the_url );
  211. $fb_count = 0;
  212. if ( is_array( $fb_get ) ) {
  213. $fb_get_body = json_decode( $fb_get['body'] );
  214. if ( isset( $fb_get_body->shares ) ) {
  215. $fb_count = $fb_get_body->shares;
  216. } else {
  217. $fb_count = 0;
  218. }
  219. $share_info['fb'] = $fb_count;
  220. }
  221. // Twitter
  222. $twitter_get = wp_remote_get( 'http://cdn.api.twitter.com/1/urls/count.json?url=' . $the_url );
  223. $twitter_count = 0;
  224. if ( is_array( $twitter_get ) ) {
  225. $twitter_get_body = json_decode( $twitter_get['body'] );
  226. if ( isset( $twitter_get_body->count ) ) {
  227. $twitter_count = $twitter_get_body->count;
  228. } else {
  229. $twitter_count = 0;
  230. }
  231. $share_info['twitter'] = $twitter_count;
  232. }
  233. // Pinterest
  234. $pinterest_get = wp_remote_get( 'http://api.pinterest.com/v1/urls/count.json?url=' . $the_url );
  235. $pinterest_count = 0;
  236. if ( is_array( $pinterest_get ) ) {
  237. $pinterest_get_body = json_decode( preg_replace( '/^receiveCount\((.*)\)$/', "\\1", $pinterest_get['body'] ) );
  238. if ( isset( $pinterest_get_body->count ) ) {
  239. $pinterest_count = $pinterest_get_body->count;
  240. } else {
  241. $pinterest_count = 0;
  242. }
  243. $share_info['pinterest'] = $pinterest_count;
  244. }
  245. // Check if there is data
  246. if ( isset( $share_info ) ) {
  247. set_transient( $transient_id, $share_info, $refresh_in );
  248. } else {
  249. $share_info = false;
  250. }
  251. }
  252. // Pass the data back
  253. return $share_info;
  254. }
  255. /**
  256. * Returns the ID of the currently used set based on icon
  257. *
  258. * @since 1.0.4
  259. *
  260. * @param string $icon The icon name
  261. * @return string Current ID of the icon set
  262. */
  263. function dslc_icons_current_set( $icon = false ) {
  264. // If no icon set return to the default "fontawesome"
  265. // If empty icon return default
  266. // If there is no "-" in icon, there is no set, return default
  267. if ( $icon == false || strlen( $icon ) == 0 || strpos( $icon, '-' ) === false ) {
  268. return 'fontawesome';
  269. }
  270. // Get array with available icons
  271. global $dslc_var_icons;
  272. // Get the first part of the icon ( representing the set )
  273. $icon_parts = explode( '-', $icon );
  274. $icon_set = $icon_parts[0];
  275. // If there is an icon set by that name return it
  276. if ( isset( $dslc_var_icons[$icon_set] ) ) {
  277. return $icon_set;
  278. // Otherwise return the default
  279. } else {
  280. return 'fontawesome';
  281. }
  282. }
  283. /**
  284. * Returns the ALT attribute for an attachment
  285. *
  286. * @since 1.0.7
  287. *
  288. * @param string $attachment_ID The ID of the attachment
  289. * @return string The ALT attribute text
  290. */
  291. function dslc_get_attachment_alt( $attachment_ID ) {
  292. // Get ALT
  293. $thumb_alt = trim( strip_tags( get_post_meta( $attachment_ID, '_wp_attachment_image_alt', true ) ) );
  294. // No ALT supplied get attachment info
  295. if ( empty( $thumb_alt ) )
  296. $attachment = get_post( $attachment_ID );
  297. // Use caption if no ALT supplied
  298. if ( empty( $thumb_alt ) )
  299. $thumb_alt = trim( strip_tags( $attachment->post_excerpt ) );
  300. // Use title if no caption supplied either
  301. if ( empty( $thumb_alt ) )
  302. $thumb_alt = trim( strip_tags( $attachment->post_title ) );
  303. // Return ALT
  304. return esc_attr( $thumb_alt );
  305. }
  306. /**
  307. * Dismissable notices
  308. *
  309. * @since 1.0.8
  310. */
  311. function dslc_dismiss_notice() {
  312. // Verify nonce
  313. if ( ! wp_verify_nonce( $_REQUEST['nonce'], "dslc_" . $_REQUEST['notice_id'] . "_nonce" ) ) {
  314. wp_die( "No naughty business please" );
  315. }
  316. // Check access permissions
  317. if ( ! current_user_can( 'install_themes' ) ) {
  318. wp_die( 'You do not have rights to do this' );
  319. }
  320. if ( $_REQUEST['notice_id'] ) {
  321. $stored_notices = get_option( 'dslc_notices' );
  322. $stored_notices[get_current_user_id()][$_REQUEST['notice_id'] . '_notice_dismissed'] = 1;
  323. update_option( 'dslc_notices', $stored_notices );
  324. }
  325. wp_die();
  326. }
  327. add_action( "wp_ajax_dslc_dismiss_notice", "dslc_dismiss_notice" );
  328. /**
  329. * Inline JS to attach click action for disisable notices
  330. *
  331. * @since 1.0.7.2
  332. *
  333. * Call Ajax action to dismiss a particular admin notice
  334. */
  335. function dslc_adminjs_dismiss_notice() { ?>
  336. <script type="text/javascript">
  337. jQuery(document).on( 'click', '.dslc-notice .notice-dismiss', function(event) {
  338. var notice_id = event.target.parentNode.id;
  339. var nonce = event.target.parentNode.getAttribute("data-nonce");
  340. jQuery.ajax({
  341. url: ajaxurl,
  342. data: {
  343. action: 'dslc_dismiss_notice',
  344. nonce: nonce,
  345. notice_id: notice_id,
  346. }
  347. })
  348. })
  349. </script>
  350. <?php }
  351. add_action( 'admin_footer', 'dslc_adminjs_dismiss_notice' );
  352. /**
  353. * Checks if notice dismissed
  354. *
  355. * @since 1.0.8
  356. *
  357. * @param string $notice_id Unique id of the notice
  358. * @return boolean true if notice is being dismissed
  359. */
  360. function dslc_notice_dismissed( $notice_id ) {
  361. $stored_notices = get_option( 'dslc_notices' );
  362. $notice_dismissed = 0;
  363. $usr_id = get_current_user_id();
  364. if ( isset( $stored_notices[$usr_id][$notice_id . '_notice_dismissed'] ) && $stored_notices[$usr_id][$notice_id . '_notice_dismissed'] = 1 ) {
  365. $notice_dismissed = 1;
  366. }
  367. return $notice_dismissed;
  368. }
  369. /**
  370. * Generate nonce for the notice
  371. *
  372. * @since 1.0.8
  373. *
  374. * @param string $notice_id Unique id of the notice
  375. * @return string nonce
  376. */
  377. function dslc_generate_notice_nonce( $notice_id ) {
  378. $notice_nonce = wp_create_nonce( 'dslc_' . $notice_id . '_nonce' );
  379. return $notice_nonce;
  380. }