PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/yith-woocommerce-zoom-magnifier/yit-common/yit-functions.php

https://gitlab.com/Haithamgit/Project
PHP | 233 lines | 135 code | 37 blank | 61 comment | 23 complexity | 0778b300ed5263da1957dff82b03fdd6 MD5 | raw file
  1. <?php
  2. /**
  3. * Your Inspiration Themes common functions
  4. *
  5. * @author Your Inspiration Themes
  6. * @version 0.0.1
  7. */
  8. define( 'YITH_FUNCTIONS', true);
  9. /* === Include Common Framework File === */
  10. require_once( 'google_fonts.php' );
  11. require_once( 'yith-panel.php' );
  12. if ( ! function_exists( 'yit_is_woocommerce_active' ) ) {
  13. /**
  14. * WC Detection
  15. */
  16. function yit_is_woocommerce_active() {
  17. $active_plugins = (array) get_option( 'active_plugins', array() );
  18. if ( is_multisite() ) {
  19. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  20. }
  21. $woo = yit_get_plugin_basename_from_slug( 'woocommerce' );
  22. return in_array( $woo, $active_plugins ) || array_key_exists( $woo, $active_plugins );
  23. }
  24. }
  25. if( ! function_exists( 'yit_get_plugin_basename_from_slug' ) ) {
  26. /**
  27. * Helper function to extract the file path of the plugin file from the
  28. * plugin slug, if the plugin is installed.
  29. *
  30. * @param string $slug Plugin slug (typically folder name) as provided by the developer
  31. * @return string Either file path for plugin if installed, or just the plugin slug
  32. */
  33. function yit_get_plugin_basename_from_slug( $slug ) {
  34. include_once ABSPATH . '/wp-admin/includes/plugin.php';
  35. $keys = array_keys( get_plugins() );
  36. foreach ( $keys as $key ) {
  37. if ( preg_match( '|^' . $slug .'|', $key ) )
  38. return $key;
  39. }
  40. return $slug;
  41. }
  42. }
  43. if( ! function_exists( 'yith_debug') ) {
  44. /**
  45. * Debug helper function. This is a wrapper for var_dump() that adds
  46. * the <pre /> tags, cleans up newlines and indents, and runs
  47. * htmlentities() before output.
  48. *
  49. * @param mixed $var The variable to dump.
  50. * @param mixed $var2 The second variable to dump
  51. * @param ...
  52. * @return string
  53. */
  54. function yith_debug() {
  55. $args = func_get_args();
  56. if( !empty( $args ) ) {
  57. foreach( $args as $k=>$arg ) {
  58. // var_dump the variable into a buffer and keep the output
  59. ob_start();
  60. var_dump($arg);
  61. $output = ob_get_clean();
  62. // neaten the newlines and indents
  63. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  64. if(!extension_loaded('xdebug')) {
  65. $output = htmlspecialchars($output, ENT_QUOTES);
  66. }
  67. $output = '<pre class="yit-debug">'
  68. . '<strong>$param_' . ($k+1) . ": </strong>"
  69. . $output
  70. . '</pre>';
  71. echo $output;
  72. }
  73. } else {
  74. trigger_error("yit_debug() expects at least 1 parameter, 0 given.", E_USER_WARNING);
  75. }
  76. return $args;
  77. }
  78. }
  79. if( ! function_exists('yit_get_options_from_prefix') ) {
  80. /**
  81. * Returns an array of all options that starts with a prefix
  82. *
  83. * @param string $prefix
  84. * @return array
  85. */
  86. function yit_get_options_from_prefix( $prefix ) {
  87. if( !$prefix ) return array();
  88. global $wpdb;
  89. $sql = "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '{$prefix}%'";
  90. $options = $wpdb->get_col( $sql );
  91. $return = array();
  92. foreach( $options as $option ) {
  93. $return[$option] = get_option( $option );
  94. }
  95. return $return;
  96. }
  97. }
  98. if( !function_exists('yit_wp_roles') ) {
  99. /**
  100. * Returns the roles of the site.
  101. *
  102. * @return array
  103. * @since 1.0.0
  104. */
  105. function yit_wp_roles() {
  106. global $wp_roles;
  107. if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
  108. $roles = array();
  109. foreach( $wp_roles->roles as $k=>$role ) {
  110. $roles[$k] = $role['name'];
  111. }
  112. return $roles;
  113. }
  114. }
  115. if( !function_exists('yit_user_roles') ) {
  116. /**
  117. * Returns the roles of the user
  118. *
  119. * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
  120. * @return array()
  121. * @since 1.0.0
  122. */
  123. function yit_user_roles( $user_id = null ) {
  124. if ( is_numeric( $user_id ) )
  125. $user = get_userdata( $user_id );
  126. else
  127. $user = wp_get_current_user();
  128. if ( empty( $user ) )
  129. return false;
  130. return (array) $user->roles;
  131. }
  132. }
  133. // ADMIN
  134. if( !function_exists('yit_typo_option_to_css') ) {
  135. /**
  136. * Change the typography option saved in database to attributes for css
  137. *
  138. * @param array $option The option as saved in the database
  139. * @return string
  140. * @since 1.0.0
  141. */
  142. function yit_typo_option_to_css( $option ) {
  143. $attrs = $variant = array();
  144. extract( $option );
  145. $attrs[] = "color: $color;";
  146. $attrs[] = "font-size: {$size}{$unit};";
  147. $attrs[] = "font-family: '{$family}';";
  148. switch ( $style ) {
  149. case 'regular':
  150. $attrs[] = 'font-weight: 400;';
  151. $attrs[] = 'font-style: normal;';
  152. $variant = 400;
  153. break;
  154. case 'bold':
  155. $attrs[] = 'font-weight: 700;';
  156. $attrs[] = 'font-style: normal;';
  157. $variant = 700;
  158. break;
  159. case 'extra-bold':
  160. $attrs[] = 'font-weight: 800;';
  161. $attrs[] = 'font-style: normal;';
  162. $variant = 800;
  163. break;
  164. case 'italic':
  165. $attrs[] = 'font-weight: 400;';
  166. $attrs[] = 'font-style: italic;';
  167. $variant = 400;
  168. break;
  169. case 'bold-italic':
  170. $attrs[] = 'font-weight: 700;';
  171. $attrs[] = 'font-style: italic;';
  172. $variant = 700;
  173. break;
  174. }
  175. yith_add_google_font( $family, $variant );
  176. return implode( "\n", $attrs ) . "\n";
  177. }
  178. }
  179. if( !function_exists('yit_curPageURL') ) {
  180. /**
  181. * Retrieve the current complete url
  182. *
  183. * @since 1.0
  184. */
  185. function yit_curPageURL() {
  186. $pageURL = 'http';
  187. if ( isset( $_SERVER["HTTPS"] ) AND $_SERVER["HTTPS"] == "on" )
  188. $pageURL .= "s";
  189. $pageURL .= "://";
  190. if ( isset( $_SERVER["SERVER_PORT"] ) AND $_SERVER["SERVER_PORT"] != "80" )
  191. $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  192. else
  193. $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  194. return $pageURL;
  195. }
  196. }