/lib/functions.php

  1. <?php 
  2.  
  3. /** 
  4. * Filters elements of array 
  5. * @param bool $value 
  6. * @return boolean 
  7. */ 
  8. function wsoe_array_filter( $value ) { 
  9.  
  10. if( $value == true ) { 
  11. return true; 
  12.  
  13. return false; 
  14.  
  15. /** 
  16. * WSOE upload path 
  17. */ 
  18. function wsoe_upload_dir() { 
  19.  
  20. $upload_dir = wp_upload_dir(); 
  21. wp_mkdir_p( $upload_dir['basedir']. '/wsoe' ); 
  22. $path = $upload_dir['basedir'] . '/wsoe'; 
  23.  
  24. return apply_filters( 'wsoe_get_upload_dir', $path ); 
  25.  
  26. /** 
  27. * Hook for wp_schedule event in wsoe_admin_notices class 
  28. */ 
  29. function wsoe_call_notices_func() { 
  30. wsoe_admin_notices::update_notices(); 
  31. add_action( 'wsoe_call_notices', 'wsoe_call_notices_func' ); 
  32.  
  33. /** 
  34. * Create .htaccess file for protecting files. 
  35. *  
  36. * IMPORTANT NOTE: .htaccess would not work in nginx, it needs to add rule manually to the server block. 
  37. *  
  38. * @param bool $force 
  39. * @param bool $method 
  40. */ 
  41. function wsoe_create_protection_files( $force = false ) { 
  42.  
  43. if ( false === get_transient( 'wsoe_check_protection_files' ) || $force ) { 
  44.  
  45. $upload_path = wsoe_upload_dir(); 
  46.  
  47. // Top level .htaccess file 
  48. $rules = wsoe_get_htaccess_rules(); 
  49.  
  50. if ( wsoe_htaccess_exists() ) { 
  51. $contents = @file_get_contents( $upload_path . '/.htaccess' ); 
  52. if ( $contents !== $rules || ! $contents ) { 
  53. // Update the .htaccess rules if they don't match 
  54. @file_put_contents( $upload_path . '/.htaccess', $rules ); 
  55. } elseif( wp_is_writable( $upload_path ) ) { 
  56. // Create the file if it doesn't exist 
  57. @file_put_contents( $upload_path . '/.htaccess', $rules ); 
  58.  
  59. // Top level blank index.php 
  60. if ( ! file_exists( $upload_path . '/index.php' ) && wp_is_writable( $upload_path ) ) { 
  61. @file_put_contents( $upload_path . '/index.php', '<?php' . PHP_EOL . '// Silence is golden.' ); 
  62.  
  63. // Now place index.php files in all sub folders 
  64. $folders = wsoe_scan_folders( $upload_path ); 
  65. foreach ( $folders as $folder ) { 
  66. // Create index.php, if it doesn't exist 
  67. if ( ! file_exists( $folder . 'index.php' ) && wp_is_writable( $folder ) ) { 
  68. @file_put_contents( $folder . 'index.php', '<?php' . PHP_EOL . '// Silence is golden.' ); 
  69.  
  70. // Check for the files once per day 
  71. set_transient( 'wsoe_check_protection_files', true, 3600 * 24 ); 
  72. add_action( 'admin_init', 'wsoe_create_protection_files' ); 
  73.  
  74. /** 
  75. * Rules for .htaccess files 
  76. */ 
  77. function wsoe_get_htaccess_rules() { 
  78.  
  79. // Prevent directory browsing and direct access to all files, except images 
  80. $rules = "Options -Indexes\n"; 
  81. $rules .= "deny from all\n"; 
  82. $rules .= "<FilesMatch '\.(jpg|png|gif|mp3|ogg)$'>\n"; 
  83. $rules .= "Order Allow, Deny\n"; 
  84. $rules .= "Allow from all\n"; 
  85. $rules .= "</FilesMatch>\n"; 
  86.  
  87. return apply_filters( 'wsoe_protected_directory_htaccess_rules', $rules ); 
  88.  
  89. /** 
  90. * Checks if the .htaccess file exists in wp-content/uploads/wsoe 
  91. * 
  92. * @since 1.3 
  93. * @return bool 
  94. */ 
  95. function wsoe_htaccess_exists() { 
  96.  
  97. $upload_path = wsoe_upload_dir(); 
  98.  
  99. return file_exists( $upload_path . '/.htaccess' ); 
  100.  
  101. /** 
  102. * Scans all folders inside of /uploads/wsoe 
  103. * 
  104. * @since 1.3 
  105. * @return array $return List of files inside directory 
  106. */ 
  107. function wsoe_scan_folders( $path = '', $return = array() ) { 
  108.  
  109. $path = $path == ''? dirname( __FILE__ ) : $path; 
  110. $lists = @scandir( $path ); 
  111.  
  112. if ( ! empty( $lists ) ) { 
  113. foreach ( $lists as $f ) { 
  114. if ( is_dir( $path . DIRECTORY_SEPARATOR . $f ) && $f != "." && $f != ".." ) { 
  115. if ( ! in_array( $path . DIRECTORY_SEPARATOR . $f, $return ) ) 
  116. $return[] = trailingslashit( $path . DIRECTORY_SEPARATOR . $f ); 
  117.  
  118. wsoe_scan_folders( $path . DIRECTORY_SEPARATOR . $f, $return); 
  119.  
  120. return $return; 
  121.  
  122. /** 
  123. * Determine if user is shop manager 
  124. *  
  125. * @return bool 
  126. */ 
  127. function wsoe_is_shop_manager() { 
  128.  
  129. if( current_user_can( 'manage_woocommerce' ) ) { 
  130. return true; 
  131.  
  132. return false; 
  133.  
  134. /** 
  135. * Returns formatted price 
  136. */ 
  137. function wsoe_formatted_price( $amount, $order_details ) { 
  138.  
  139. if( is_a( $order_details, 'WC_Order' ) && apply_filters( 'wsoe_formatted_price', true ) ) { 
  140.  
  141. // Support for php versions older than PHP 5.4.0 
  142. if( !defined( 'ENT_HTML5' ) ) { 
  143. return strip_tags( html_entity_decode( wc_price( $amount , apply_filters( 'wsoe_formatted_price_args', array() ) ) ) ) ; 
  144. }else { 
  145. $charset = get_option('blog_charset'); 
  146. return strip_tags( html_entity_decode( wc_price( $amount , apply_filters( 'wsoe_formatted_price_args', array() ) ), ENT_HTML5, $charset ) ) ; 
  147.  
  148. return $amount; 
  149.  
  150. /** 
  151. * Fix weird characters in CSV 
  152. */ 
  153. function wsoe_fix_weird_chars() { 
  154.  
  155. $settings = WSOE()->settings['plugin_settings']; ?> 
  156.  
  157. <tr> 
  158.  
  159. <th> 
  160. <?php _e( 'Fix weird charactes in CSV', 'woocommerce-simply-order-export') ?> 
  161. <img class="help_tip" data-tip="<?php _e('Check this option only if you are getting some weird characters in exported CSV file', 'woocommerce-simply-order-export') ?>" src="<?php echo WSOE_IMG; ?>help.png" height="16" width="16"> 
  162. </th> 
  163.  
  164. <td> 
  165. <input type="checkbox" name="wpg_fix_chars" value="1" <?php checked( $settings['wsoe_fix_chars'], 1, true ); ?> /> 
  166. </td> 
  167.  
  168. </tr><?php 
  169.  
  170. add_action( 'advanced_options_end', 'wsoe_fix_weird_chars', 9 ); 
  171.  
  172. /** 
  173. * Runs the script for update. If any update of the plugin has update script available,  
  174. * it would seek for the script and if script is available, it will run the script. 
  175. */ 
  176. if( !function_exists('wsoe_core_run_update_scripts') ) { 
  177.  
  178. function wsoe_core_run_update_scripts() { 
  179.  
  180. // Run this block if current user is woocommerce manager and is in admin/dashboard. 
  181. if( is_admin() && wsoe_is_shop_manager() ) { 
  182.  
  183. $db_version = get_option( 'wsoe_core_version', '1.0.0' ); // Check what was the last version 
  184. $upgrade_ran = $upgrade_ran_new = get_option( 'wsoe_core_upgrade_ran', array() ); 
  185.  
  186. $update_scripts = array('2.1.6'); // Array of version numbers for which update scripts are present. 
  187.  
  188. if( version_compare( $db_version, WSOE()->version, '<' ) ) { 
  189.  
  190. foreach( $update_scripts as $script ) { 
  191.  
  192. // Check if this update script has already been executed, if it is, do not execute it again. 
  193. if( (version_compare( $script, WSOE()->version, '<=' )) && ( !in_array( $script, $upgrade_ran ) ) ) { 
  194.  
  195. require_once trailingslashit( WSOE_BASE ).'updater/wsoe-core-'.$script.'.php'; 
  196. array_push( $upgrade_ran_new, $script ); 
  197.  
  198. $diff = array_diff( $upgrade_ran_new, $upgrade_ran ); 
  199.  
  200. if( !empty( $diff ) ) { 
  201. update_option( 'wsoe_core_upgrade_ran', $upgrade_ran_new ); 
  202.  
  203. update_option( 'wsoe_core_version', WSOE()->version ); 
  204. add_action( 'wp_loaded', 'wsoe_core_run_update_scripts' ); 
.