PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/jigoshop-admin.php

https://github.com/jusbell/Jigoshop
PHP | 446 lines | 293 code | 72 blank | 81 comment | 59 complexity | 1e12a977519955ab3fb949dd46dfd543 MD5 | raw file
  1. <?php
  2. /**
  3. * Main admin file which loads all settings panels and sets up the menus.
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Admin
  13. * @author Jigowatt
  14. * @copyright Copyright (c) 2011 Jigowatt Ltd.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. require_once ( 'jigoshop-install.php' );
  18. require_once ( 'jigoshop-admin-dashboard.php' );
  19. require_once ( 'jigoshop-write-panels.php' );
  20. require_once ( 'jigoshop-admin-settings.php' );
  21. require_once ( 'jigoshop-admin-attributes.php' );
  22. require_once ( 'jigoshop-admin-post-types.php' );
  23. function jigoshop_admin_init () {
  24. require_once ( 'jigoshop-admin-settings-options.php' );
  25. }
  26. add_action('admin_init', 'jigoshop_admin_init');
  27. /**
  28. * Admin Menus
  29. *
  30. * Sets up the admin menus in wordpress.
  31. *
  32. * @since 1.0
  33. */
  34. function jigoshop_admin_menu() {
  35. global $menu;
  36. $menu[] = array( '', 'read', 'separator-jigoshop', '', 'wp-menu-separator jigoshop' );
  37. add_menu_page(__('Jigoshop'), __('Jigoshop'), 'manage_options', 'jigoshop' , 'jigoshop_dashboard', jigoshop::plugin_url() . '/assets/images/icons/menu_icons.png', 55);
  38. add_submenu_page('jigoshop', __('Dashboard', 'jigoshop'), __('Dashboard', 'jigoshop'), 'manage_options', 'jigoshop', 'jigoshop_dashboard');
  39. add_submenu_page('jigoshop', __('General Settings', 'jigoshop'), __('Settings', 'jigoshop') , 'manage_options', 'settings', 'jigoshop_settings');
  40. add_submenu_page('jigoshop', __('System Info','jigoshop'), __('System Info','jigoshop'), 'manage_options', 'sysinfo', 'jigoshop_system_info');
  41. add_submenu_page('edit.php?post_type=product', __('Attributes','jigoshop'), __('Attributes','jigoshop'), 'manage_options', 'attributes', 'jigoshop_attributes');
  42. }
  43. function jigoshop_admin_menu_order( $menu_order ) {
  44. // Initialize our custom order array
  45. $jigoshop_menu_order = array();
  46. // Get the index of our custom separator
  47. $jigoshop_separator = array_search( 'separator-jigoshop', $menu_order );
  48. $jigoshop_product = array_search( 'edit.php?post_type=product', $menu_order );
  49. $jigoshop_order = array_search( 'edit.php?post_type=shop_order', $menu_order );
  50. // Loop through menu order and do some rearranging
  51. foreach ( $menu_order as $index => $item ) :
  52. if ( 'jigoshop' == $item ) :
  53. $jigoshop_menu_order[] = 'separator-jigoshop';
  54. $jigoshop_menu_order[] = $item;
  55. $jigoshop_menu_order[] = 'edit.php?post_type=product';
  56. $jigoshop_menu_order[] = 'edit.php?post_type=shop_order';
  57. unset( $menu_order[$jigoshop_separator] );
  58. unset( $menu_order[$jigoshop_product] );
  59. unset( $menu_order[$jigoshop_order] );
  60. elseif ( !in_array( $item, array( 'separator-jigoshop' ) ) ) :
  61. $jigoshop_menu_order[] = $item;
  62. endif;
  63. endforeach;
  64. // Return order
  65. return $jigoshop_menu_order;
  66. }
  67. function jigoshop_admin_custom_menu_order() {
  68. return current_user_can( 'manage_options' );
  69. }
  70. add_action('admin_menu', 'jigoshop_admin_menu');
  71. add_action('menu_order', 'jigoshop_admin_menu_order');
  72. add_action('custom_menu_order', 'jigoshop_admin_custom_menu_order');
  73. /**
  74. * Admin Head
  75. *
  76. * Outputs some styles in the admin <head> to show icons on the jigoshop admin pages
  77. *
  78. * @since 1.0
  79. */
  80. function jigoshop_admin_head() {
  81. ?>
  82. <style type="text/css">
  83. <?php if ( isset($_GET['taxonomy']) && $_GET['taxonomy']=='product_cat' ) : ?>
  84. .icon32-posts-product { background-position: -243px -5px !important; }
  85. <?php elseif ( isset($_GET['taxonomy']) && $_GET['taxonomy']=='product_tag' ) : ?>
  86. .icon32-posts-product { background-position: -301px -5px !important; }
  87. <?php endif; ?>
  88. </style>
  89. <?php
  90. }
  91. add_action('admin_head', 'jigoshop_admin_head');
  92. /**
  93. * System info
  94. *
  95. * Shows the system info panel which contains version data and debug info
  96. *
  97. * @since 1.0
  98. * @usedby jigoshop_settings()
  99. */
  100. function jigoshop_system_info() {
  101. ?>
  102. <div class="wrap jigoshop">
  103. <div class="icon32 icon32-jigoshop-debug" id="icon-jigoshop"><br/></div>
  104. <h2><?php _e('System Information','jigoshop') ?></h2>
  105. <p>Use the information below when submitting technical support requests via <a href="http://support.jigoshop.com/" title="Jigoshop Support" target="_blank">Jigoshop Support</a>.</p>
  106. <div id="tabs-wrap">
  107. <ul class="tabs">
  108. <li><a href="#versions"><?php _e('Environment', 'jigoshop'); ?></a></li>
  109. <li><a href="#debugging"><?php _e('Debugging', 'jigoshop'); ?></a></li>
  110. </ul>
  111. <div id="versions" class="panel">
  112. <table class="widefat fixed">
  113. <thead>
  114. <tr>
  115. <th scope="col" width="200px"><?php _e('Software Versions','jigoshop')?></th>
  116. <th scope="col">&nbsp;</th>
  117. </tr>
  118. </thead>
  119. <tbody>
  120. <tr>
  121. <td class="titledesc"><?php _e('Jigoshop Version','jigoshop')?></td>
  122. <td class="forminp"><?php echo jigoshop::get_var('version'); ?></td>
  123. </tr>
  124. <tr>
  125. <td class="titledesc"><?php _e('WordPress Version','jigoshop')?></td>
  126. <td class="forminp"><?php if (is_multisite()) echo 'WPMU'; else echo 'WP'; ?> <?php echo bloginfo('version'); ?></td>
  127. </tr>
  128. </tbody>
  129. <thead>
  130. <tr>
  131. <th scope="col" width="200px"><?php _e('Server','jigoshop')?></th>
  132. <th scope="col"><?php echo (defined('PHP_OS')) ? (string)(PHP_OS) : 'N/A'; ?></th>
  133. </tr>
  134. </thead>
  135. <tbody>
  136. <tr>
  137. <td class="titledesc"><?php _e('PHP Version','jigoshop')?></td>
  138. <td class="forminp"><?php if(function_exists('phpversion')) echo phpversion(); ?></td>
  139. </tr>
  140. <tr>
  141. <td class="titledesc"><?php _e('Server Software','jigoshop')?></td>
  142. <td class="forminp"><?php echo $_SERVER['SERVER_SOFTWARE']; ?></td>
  143. </tr>
  144. </tbody>
  145. </table>
  146. </div>
  147. <div id="debugging" class="panel">
  148. <table class="widefat fixed">
  149. <tbody>
  150. <tr>
  151. <th scope="col" width="200px"><?php _e('Debug Information','jigoshop')?></th>
  152. <th scope="col">&nbsp;</th>
  153. </tr>
  154. <tr>
  155. <td class="titledesc"><?php _e('UPLOAD_MAX_FILESIZE','jigoshop')?></td>
  156. <td class="forminp"><?php
  157. if(function_exists('phpversion')) echo (jigoshop_let_to_num(ini_get('upload_max_filesize'))/(1024*1024))."MB";
  158. ?></td>
  159. </tr>
  160. <tr>
  161. <td class="titledesc"><?php _e('POST_MAX_SIZE','jigoshop')?></td>
  162. <td class="forminp"><?php
  163. if(function_exists('phpversion')) echo (jigoshop_let_to_num(ini_get('post_max_size'))/(1024*1024))."MB";
  164. ?></td>
  165. </tr>
  166. <tr>
  167. <td class="titledesc"><?php _e('WordPress Memory Limit','jigoshop')?></td>
  168. <td class="forminp"><?php
  169. echo (jigoshop_let_to_num(WP_MEMORY_LIMIT)/(1024*1024))."MB";
  170. ?></td>
  171. </tr>
  172. <tr>
  173. <td class="titledesc"><?php _e('WP_DEBUG','jigoshop')?></td>
  174. <td class="forminp"><?php echo (WP_DEBUG) ? __('On', 'jigoshop') : __('Off', 'jigoshop'); ?></td>
  175. </tr>
  176. <tr>
  177. <td class="titledesc"><?php _e('DISPLAY_ERRORS','jigoshop')?></td>
  178. <td class="forminp"><?php echo (ini_get('display_errors')) ? 'On (' . ini_get('display_errors') . ')' : 'N/A'; ?></td>
  179. </tr>
  180. <tr>
  181. <td class="titledesc"><?php _e('FSOCKOPEN','jigoshop')?></td>
  182. <td class="forminp"><?php if(function_exists('fsockopen')) echo '<span style="color:green">' . __('Your server supports fsockopen.', 'jigoshop'). '</span>'; else echo '<span style="color:red">' . __('Your server does not support fsockopen.', 'jigoshop'). '</span>'; ?></td>
  183. </tr>
  184. </tbody>
  185. </table>
  186. </div>
  187. </div>
  188. </div>
  189. <script type="text/javascript">
  190. jQuery(function() {
  191. // Tabs
  192. jQuery('ul.tabs').show();
  193. jQuery('ul.tabs li:first').addClass('active');
  194. jQuery('div.panel:not(div.panel:first)').hide();
  195. jQuery('ul.tabs a').click(function(){
  196. jQuery('ul.tabs li').removeClass('active');
  197. jQuery(this).parent().addClass('active');
  198. jQuery('div.panel').hide();
  199. jQuery( jQuery(this).attr('href') ).show();
  200. return false;
  201. });
  202. });
  203. </script>
  204. <?php
  205. }
  206. function jigoshop_feature_product () {
  207. if( !is_admin() ) die;
  208. if( !current_user_can('edit_posts') ) wp_die( __('You do not have sufficient permissions to access this page.') );
  209. if( !check_admin_referer()) wp_die( __('You have taken too long. Please go back and retry.', 'jigoshop') );
  210. $post_id = isset($_GET['product_id']) && (int)$_GET['product_id'] ? (int)$_GET['product_id'] : '';
  211. if(!$post_id) die;
  212. $post = get_post($post_id);
  213. if(!$post) die;
  214. if($post->post_type !== 'product') die;
  215. $product = new jigoshop_product($post->ID);
  216. if ($product->is_featured()) update_post_meta($post->ID, 'featured', 'no');
  217. else update_post_meta($post->ID, 'featured', 'yes');
  218. $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
  219. wp_safe_redirect( $sendback );
  220. }
  221. add_action('wp_ajax_jigoshop-feature-product', 'jigoshop_feature_product');
  222. /**
  223. * Returns proper post_type
  224. */
  225. function jigoshop_get_current_post_type() {
  226. global $post, $typenow, $current_screen;
  227. if( $current_screen && @$current_screen->post_type ) return $current_screen->post_type;
  228. if( $typenow ) return $typenow;
  229. if( !empty($_REQUEST['post_type']) ) return sanitize_key( $_REQUEST['post_type'] );
  230. if ( !empty($post) && !empty($post->post_type) ) return $post->post_type;
  231. if( ! empty($_REQUEST['post']) && (int)$_REQUEST['post'] ) {
  232. $p = get_post( $_REQUEST['post'] );
  233. return $p ? $p->post_type : '';
  234. }
  235. return '';
  236. }
  237. /**
  238. * Permalink structure needs to be saved twice for structure to take effect
  239. * Common bug with wordpress 3.1+ as of yet unresolved
  240. *
  241. * @returns notice
  242. */
  243. function permalink_save_twice_notice() {
  244. if( isset($_POST['_wp_http_referer']) && strpos($_POST['_wp_http_referer'], 'options-permalink.php') ) {
  245. print_r('<div id="message" class="updated"><p>'.__('Note: Please make sure you save your permalink settings <strong>twice</strong> in order for them to be applied correctly in Jigoshop', 'jigoshop' ).'</p></div>');
  246. }
  247. }
  248. add_action('admin_notices', 'permalink_save_twice_notice');
  249. /**
  250. * Categories ordering
  251. */
  252. /**
  253. * Load needed scripts to order categories
  254. */
  255. function jigoshop_categories_scripts () {
  256. if( !isset($_GET['taxonomy']) || $_GET['taxonomy'] !== 'product_cat') return;
  257. wp_register_script('jigoshop-categories-ordering', jigoshop::plugin_url() . '/assets/js/categories-ordering.js', array('jquery-ui-sortable'));
  258. wp_print_scripts('jigoshop-categories-ordering');
  259. }
  260. add_action('admin_footer-edit-tags.php', 'jigoshop_categories_scripts');
  261. /**
  262. * Load needed scripts for Settings Coupons
  263. */
  264. function jigoshop_admin_coupons_scripts () {
  265. wp_register_script('jigoshop-date', jigoshop::plugin_url() . '/assets/js/date.js');
  266. wp_register_script('jigoshop-datepicker', jigoshop::plugin_url() . '/assets/js/datepicker.js', array('jquery', 'jigoshop-date'));
  267. wp_enqueue_script('jigoshop-datepicker');
  268. }
  269. add_action("admin_print_scripts-jigoshop_page_settings", 'jigoshop_admin_coupons_scripts');
  270. /**
  271. * Ajax request handling for categories ordering
  272. */
  273. function jigoshop_categories_ordering () {
  274. global $wpdb;
  275. $id = (int)$_POST['id'];
  276. $next_id = isset($_POST['nextid']) && (int) $_POST['nextid'] ? (int) $_POST['nextid'] : null;
  277. if( ! $id || ! $term = get_term_by('id', $id, 'product_cat') ) die(0);
  278. jigoshop_order_categories ( $term, $next_id);
  279. $children = get_terms('product_cat', "child_of=$id&menu_order=ASC&hide_empty=0");
  280. if( $term && sizeof($children) ) {
  281. echo 'children';
  282. die;
  283. }
  284. }
  285. add_action('wp_ajax_jigoshop-categories-ordering', 'jigoshop_categories_ordering');
  286. /**
  287. * Outputs the media upload image as a preview
  288. * @param array $args The attachment id or url to use for the image
  289. * @return string
  290. */
  291. function jigoshop_custom_image($args=array()) {
  292. require_once (ABSPATH . '/wp-admin/includes/image.php');
  293. $defaults = array('image'=>null, 'echo'=>true, 'width'=>60, 'height'=>60);
  294. $args = wp_parse_args($args, $defaults);
  295. extract($args);
  296. if (isset($_POST['image']) && defined('DOING_AJAX') && DOING_AJAX){
  297. $image = isset($_POST['image']) ? $_POST['image'] : null;
  298. $width = isset($_POST['width']) ? $_POST['width'] : $width;
  299. $height = isset($_POST['height']) ? $_POST['height'] : $height;
  300. }
  301. $src = jigoshop_custom_image_src($image, $width, $height);
  302. if ($echo){
  303. echo '<img src="' . $src . '" width="' . $width . '" height="' . $height . '" class="jigoshop-media-preview"/>';
  304. }
  305. if (defined('DOING_AJAX') && DOING_AJAX){
  306. exit();
  307. } else {
  308. return $src;
  309. }
  310. }
  311. add_action('wp_ajax_jigoshop_media_preview', 'jigoshop_custom_image');
  312. function jigoshop_custom_image_src($file, $width, $height){
  313. // If no file is provided just send the placeholder image back
  314. if ($file === null){
  315. return jigoshop::plugin_url().'/assets/images/placeholder.png';
  316. }
  317. $upload_dir = wp_upload_dir();
  318. // Its an attachment image id
  319. if (is_numeric($file)){
  320. $file = wp_get_attachment_url($file);
  321. }
  322. // Any http url image
  323. if (preg_match('/^http/', $file)){
  324. $upload_dir = wp_upload_dir();
  325. $file = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file);
  326. }
  327. // Check to see if the custom size image file was already created
  328. $suffix = "{$width}x{$height}";
  329. $info = pathinfo($file);
  330. $dir = $info['dirname'];
  331. $ext = $info['extension'];
  332. $name = wp_basename($file, ".$ext");
  333. $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
  334. if (file_exists($destfilename)){
  335. $imagepath = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $destfilename);
  336. return $imagepath;
  337. } else {
  338. $thumb = image_resize($file, $width, $height, true);
  339. if (is_wp_error($thumb)){
  340. $imagepath = jigoshop::plugin_url().'/assets/images/placeholder.png';
  341. } else {
  342. $imagepath = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $thumb);
  343. }
  344. return $imagepath;
  345. }
  346. }
  347. if (!function_exists('boolval')) {
  348. /**
  349. * Helper function to get the boolean value of a variable. If not strict, this function will return true
  350. * if the variable is not false and not empty. If strict, the value of the variable must exactly match a
  351. * value in the true test array to evaluate to true
  352. *
  353. * @param $in The input variable
  354. * @param bool $strict
  355. * @return bool|null|string
  356. */
  357. function boolval($in, $strict = false) {
  358. if (is_bool($in)){
  359. return $in;
  360. }
  361. $in = strtolower($in);
  362. $out = null;
  363. if (in_array($in, array('false', 'no', 'n', 'off', '0', 0, null), true)) {
  364. $out = false;
  365. } else if ($strict) {
  366. if (in_array($in, array('true', 'yes', 'y', 'on', '1', 1), true)) {
  367. $out = true;
  368. }
  369. } else {
  370. $out = ($in ? true : false);
  371. }
  372. return $out;
  373. }
  374. }