woocommerce/admin/woocommerce-admin-functions...

505 lines
13 KiB
PHP
Raw Normal View History

2011-12-12 16:34:56 +00:00
<?php
/**
* WooCommerce Admin Functions
*
2011-12-12 16:34:56 +00:00
* Hooked-in functions for WooCommerce related events in admin.
*
2012-08-14 12:21:34 +00:00
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin
* @version 1.6.4
2011-12-12 16:34:56 +00:00
*/
2012-10-15 10:32:24 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Checks which method we're using to serve downloads
*
* If using force or x-sendfile, this ensures the .htaccess is in place
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
*/
function woocomerce_check_download_folder_protection() {
$upload_dir = wp_upload_dir();
$downloads_url = $upload_dir['basedir'] . '/woocommerce_uploads';
$download_method = get_option('woocommerce_file_download_method');
if ($download_method=='redirect') :
// Redirect method - don't protect
if (file_exists($downloads_url.'/.htaccess')) :
unlink( $downloads_url . '/.htaccess' );
endif;
flush_rewrite_rules( true );
else :
// Force method - protect, add rules to the htaccess file
if (!file_exists($downloads_url.'/.htaccess')) :
2012-02-15 17:50:02 +00:00
if ($file_handle = @fopen( $downloads_url . '/.htaccess', 'w' )) :
fwrite($file_handle, 'deny from all');
fclose($file_handle);
endif;
endif;
flush_rewrite_rules( true );
endif;
}
2012-08-14 12:21:34 +00:00
/**
* Protect downlodas from ms-files.php in multisite
2012-08-14 12:21:34 +00:00
*
* @access public
* @param mixed $rewrite
* @return string
*/
function woocommerce_ms_protect_download_rewite_rules( $rewrite ) {
global $wp_rewrite;
$download_method = get_option('woocommerce_file_download_method');
if (!is_multisite() || $download_method=='redirect') return $rewrite;
$rule = "\n# WooCommerce Rules - Protect Files from ms-files.php\n\n";
$rule .= "<IfModule mod_rewrite.c>\n";
$rule .= "RewriteEngine On\n";
$rule .= "RewriteCond %{QUERY_STRING} file=woocommerce_uploads/ [NC]\n";
$rule .= "RewriteRule /ms-files.php$ - [F]\n";
$rule .= "</IfModule>\n\n";
return $rule . $rewrite;
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
* Removes variations etc belonging to a deleted post, and clears transients
2012-08-14 12:21:34 +00:00
*
* @access public
* @param mixed $id ID of post being deleted
* @return void
2011-12-12 16:34:56 +00:00
*/
function woocommerce_delete_post( $id ) {
if ( ! current_user_can( 'delete_posts' ) ) return;
2011-12-12 16:34:56 +00:00
if ( $id > 0 ) :
2011-12-12 16:34:56 +00:00
if ( $children_products =& get_children( 'post_parent='.$id.'&post_type=product_variation' ) ) :
2011-12-12 16:34:56 +00:00
if ($children_products) :
2011-12-12 16:34:56 +00:00
foreach ($children_products as $child) :
2011-12-12 16:34:56 +00:00
wp_delete_post( $child->ID, true );
2011-12-12 16:34:56 +00:00
endforeach;
2011-12-12 16:34:56 +00:00
endif;
2011-12-12 16:34:56 +00:00
endif;
2011-12-12 16:34:56 +00:00
endif;
delete_transient( 'woocommerce_processing_order_count' );
2011-12-12 16:34:56 +00:00
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
2012-08-14 12:21:34 +00:00
* Preview Emails in WP admin
*
* @access public
* @return void
*/
2011-12-12 16:34:56 +00:00
function woocommerce_preview_emails() {
if ( isset( $_GET['preview_woocommerce_mail'] ) ) {
2011-12-12 16:34:56 +00:00
$nonce = $_REQUEST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'preview-mail') )
die( 'Security check' );
2011-12-12 16:34:56 +00:00
global $woocommerce, $email_heading;
2011-12-12 16:34:56 +00:00
$mailer = $woocommerce->mailer();
2012-10-16 09:45:33 +00:00
$email_heading = __( 'Order Received', 'woocommerce' );
2012-10-16 14:46:21 +00:00
$message = wpautop( __( 'Thank you, we are now processing your order. Your order\'s details are below.', 'woocommerce' ) );
2012-10-16 09:45:33 +00:00
$message .= '<h2>' . __( 'Order:', 'woocommerce' ) . ' ' . '#1000</h2>';
$message .= '
<table cellspacing="0" cellpadding="6" style="width: 100%; border: 1px solid #eee; margin: 0 0 20px" border="1" bordercolor="#eee">
<thead>
<tr>
2012-10-16 09:45:33 +00:00
<th scope="col" style="text-align:left; border: 1px solid #eee;">' . __( 'Product', 'woocommerce' ) . '</th>
<th scope="col" style="text-align:left; border: 1px solid #eee;">' . __( 'Quantity', 'woocommerce' ) . '</th>
<th scope="col" style="text-align:left; border: 1px solid #eee;">' . __( 'Price', 'woocommerce' ) . '</th>
</tr>
</thead>
<tbody>
<tr>
<td>An awesome product</td>
<td>1</td>
<td>$9.99</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="2">' . __( 'Order total:', 'woocommerce' ) . '</td>
<td>$9.99</td>
</tr>
</tfoot>
</table>';
2012-10-16 09:45:33 +00:00
$message .= '<h2>' . __( 'Customer details', 'woocommerce' ) . '</h2>';
$message .= '
<table cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top;" border="0">
<tr>
<td valign="top" width="50%">
2012-10-16 09:45:33 +00:00
<h3>' . __( 'Billing address', 'woocommerce' ) . '</h3>
<p>Some Guy
1 infinite loop
Cupertino
CA 95014</p>
</td>
<td valign="top" width="50%">
2012-10-16 09:45:33 +00:00
<h3>' . __( 'Shipping address', 'woocommerce' ) . '</h3>
<p>Some Guy
1 infinite loop
Cupertino
CA 95014</p>
</td>
</tr>
</table>';
2011-12-12 16:34:56 +00:00
echo $mailer->wrap_message( $email_heading, $message );
2011-12-12 16:34:56 +00:00
exit;
}
2011-12-12 16:34:56 +00:00
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
* Prevent non-admin access to backend
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
2011-12-12 16:34:56 +00:00
*/
function woocommerce_prevent_admin_access() {
2012-08-14 12:21:34 +00:00
if ( get_option('woocommerce_lock_down_admin') == 'yes' && ! is_ajax() && ! current_user_can('edit_posts') ) {
2012-01-06 17:14:31 +00:00
wp_safe_redirect(get_permalink(woocommerce_get_page_id('myaccount')));
2011-12-12 16:34:56 +00:00
exit;
}
2011-12-12 16:34:56 +00:00
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
* Fix 'insert into post' buttons for images
2012-08-14 12:21:34 +00:00
*
* @access public
* @param mixed $vars
* @return array
*/
function woocommerce_allow_img_insertion( $vars ) {
2011-12-12 16:34:56 +00:00
$vars['send'] = true; // 'send' as in "Send to Editor"
return($vars);
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
2012-08-14 12:21:34 +00:00
* Filter the directory for uploads.
*
* @access public
* @param mixed $pathdata
* @return void
2011-12-12 16:34:56 +00:00
*/
function woocommerce_downloads_upload_dir( $pathdata ) {
if (isset($_POST['type']) && $_POST['type'] == 'downloadable_product') :
2011-12-12 16:34:56 +00:00
// Uploading a downloadable file
$subdir = '/woocommerce_uploads'.$pathdata['subdir'];
$pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']);
$pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']);
$pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']);
return $pathdata;
2011-12-12 16:34:56 +00:00
endif;
2011-12-12 16:34:56 +00:00
return $pathdata;
}
2012-08-14 12:21:34 +00:00
/**
* Run a filter when uploading a downloadable product.
*
* @access public
* @return void
*/
2011-12-12 16:34:56 +00:00
function woocommerce_media_upload_downloadable_product() {
do_action('media_upload_file');
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
2012-08-14 12:21:34 +00:00
* Add a button for shortcodes to the WP editor.
*
* @access public
* @return void
*/
2011-12-12 16:34:56 +00:00
function woocommerce_add_shortcode_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) return;
if ( get_user_option('rich_editing') == 'true') :
add_filter('mce_external_plugins', 'woocommerce_add_shortcode_tinymce_plugin');
add_filter('mce_buttons', 'woocommerce_register_shortcode_button');
endif;
}
2012-08-14 12:21:34 +00:00
/**
* woocommerce_add_tinymce_lang function.
*
* @access public
* @param mixed $arr
* @return void
*/
function woocommerce_add_tinymce_lang( $arr ) {
global $woocommerce;
$arr[] = $woocommerce->plugin_path() . '/assets/js/admin/editor_plugin_lang.php';
return $arr;
}
add_filter( 'mce_external_languages', 'woocommerce_add_tinymce_lang', 10, 1 );
2012-08-14 12:21:34 +00:00
/**
* Register the shortcode button.
*
* @access public
* @param mixed $buttons
* @return array
*/
2011-12-12 16:34:56 +00:00
function woocommerce_register_shortcode_button($buttons) {
array_push($buttons, "|", "woocommerce_shortcodes_button");
return $buttons;
}
2012-08-14 12:21:34 +00:00
/**
* Add the shortcode button to TinyMCE
*
* @access public
* @param mixed $plugin_array
* @return array
*/
2011-12-12 16:34:56 +00:00
function woocommerce_add_shortcode_tinymce_plugin($plugin_array) {
global $woocommerce;
$plugin_array['WooCommerceShortcodes'] = $woocommerce->plugin_url() . '/assets/js/admin/editor_plugin.js';
return $plugin_array;
}
2012-08-14 12:21:34 +00:00
/**
* Force TinyMCE to refresh.
*
* @access public
* @param mixed $ver
* @return int
*/
function woocommerce_refresh_mce( $ver ) {
2011-12-12 16:34:56 +00:00
$ver += 3;
return $ver;
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
2012-10-06 18:21:25 +00:00
* Order term when created (put in position 0).
2012-08-14 12:21:34 +00:00
*
* @access public
* @param mixed $term_id
* @param mixed $tt_id
* @param mixed $taxonomy
* @return void
2011-12-12 16:34:56 +00:00
*/
function woocommerce_create_term( $term_id, $tt_id, $taxonomy ) {
2012-10-06 18:21:25 +00:00
if ( ! $taxonomy == 'product_cat' && ! strstr( $taxonomy, 'pa_' ) )
return;
2012-10-06 18:21:25 +00:00
$meta_name = strstr( $taxonomy, 'pa_' ) ? 'order_' . esc_attr( $taxonomy ) : 'order';
2011-12-12 16:34:56 +00:00
2012-10-06 18:21:25 +00:00
update_woocommerce_term_meta( $term_id, $meta_name, 0 );
2011-12-12 16:34:56 +00:00
}
2012-08-14 12:21:34 +00:00
2011-12-12 16:34:56 +00:00
/**
2012-08-14 12:21:34 +00:00
* When a term is deleted, delete its meta.
*
* @access public
* @param mixed $term_id
* @param mixed $tt_id
* @param mixed $taxonomy
* @return void
2011-12-12 16:34:56 +00:00
*/
function woocommerce_delete_term( $term_id, $tt_id, $taxonomy ) {
2011-12-12 16:34:56 +00:00
$term_id = (int) $term_id;
2011-12-12 16:34:56 +00:00
if(!$term_id) return;
2011-12-12 16:34:56 +00:00
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->woocommerce_termmeta} WHERE `woocommerce_term_id` = " . $term_id);
2012-05-01 18:28:45 +00:00
}
2012-08-14 12:21:34 +00:00
2012-05-01 18:28:45 +00:00
/**
2012-08-14 12:21:34 +00:00
* Generate CSS from the less file when changing colours.
*
* @access public
* @return void
2012-05-01 18:28:45 +00:00
*/
function woocommerce_compile_less_styles() {
global $woocommerce;
2012-10-16 14:46:21 +00:00
$colors = array_map( 'esc_attr', (array) get_option( 'woocommerce_frontend_css_colors' ) );
2012-05-01 18:28:45 +00:00
$base_file = $woocommerce->plugin_path() . '/assets/css/woocommerce-base.less';
$less_file = $woocommerce->plugin_path() . '/assets/css/woocommerce.less';
$css_file = $woocommerce->plugin_path() . '/assets/css/woocommerce.css';
// Write less file
2012-05-01 18:28:45 +00:00
if ( is_writable( $base_file ) && is_writable( $css_file ) ) {
2012-05-01 18:28:45 +00:00
// Colours changed - recompile less
if ( ! class_exists( 'lessc' ) )
2012-09-10 11:26:26 +00:00
include_once('includes/class-lessc.php');
2012-05-13 20:51:10 +00:00
if ( ! class_exists( 'cssmin' ) )
2012-09-10 11:26:26 +00:00
include_once('includes/class-cssmin.php');
2012-05-01 18:28:45 +00:00
try {
// Set default if colours not set
if ( ! $colors['primary'] ) $colors['primary'] = '#ad74a2';
if ( ! $colors['secondary'] ) $colors['secondary'] = '#f7f6f7';
if ( ! $colors['highlight'] ) $colors['highlight'] = '#85ad74';
if ( ! $colors['content_bg'] ) $colors['content_bg'] = '#ffffff';
if ( ! $colors['subtext'] ) $colors['subtext'] = '#777777';
2012-05-01 18:28:45 +00:00
// Write new color to base file
$color_rules = "
@primary: " . $colors['primary'] . ";
@primarytext: " . woocommerce_light_or_dark( $colors['primary'], 'desaturate(darken(@primary,50%),18%)', 'desaturate(lighten(@primary,50%),18%)' ) . ";
@secondary: " . $colors['secondary'] . ";
@secondarytext: " . woocommerce_light_or_dark( $colors['secondary'], 'desaturate(darken(@secondary,60%),18%)', 'desaturate(lighten(@secondary,60%),18%)' ) . ";
@highlight: " . $colors['highlight'] . ";
@highlightext: " . woocommerce_light_or_dark( $colors['highlight'], 'desaturate(darken(@highlight,60%),18%)', 'desaturate(lighten(@highlight,60%),18%)' ) . ";
@contentbg: " . $colors['content_bg'] . ";
@subtext: " . $colors['subtext'] . ";
";
2012-05-01 18:28:45 +00:00
file_put_contents( $base_file, $color_rules );
2012-05-01 18:28:45 +00:00
$less = new lessc( $less_file );
$compiled_css = $less->parse();
2012-05-01 18:28:45 +00:00
$compiled_css = CssMin::minify( $compiled_css );
2012-05-01 18:28:45 +00:00
if ( $compiled_css )
file_put_contents( $css_file, $compiled_css );
2012-05-01 18:28:45 +00:00
} catch ( exception $ex ) {
2012-10-16 09:45:33 +00:00
wp_die( __( 'Could not compile woocommerce.less:', 'woocommerce' ) . ' ' . $ex->getMessage() );
2012-05-01 18:28:45 +00:00
}
}
}
2012-08-14 12:21:34 +00:00
/**
* Add extra bulk action options to mark orders as complete or processing
2012-08-14 12:21:34 +00:00
*
* Using Javascript until WordPress core fixes: http://core.trac.wordpress.org/ticket/16031
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
*/
function woocommerce_bulk_admin_footer() {
global $post_type;
if ( 'shop_order' == $post_type ) {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('<option>').val('mark_processing').text('<?php _e( 'Mark processing', 'woocommerce' )?>').appendTo("select[name='action']");
jQuery('<option>').val('mark_processing').text('<?php _e( 'Mark processing', 'woocommerce' )?>').appendTo("select[name='action2']");
jQuery('<option>').val('mark_completed').text('<?php _e( 'Mark completed', 'woocommerce' )?>').appendTo("select[name='action']");
jQuery('<option>').val('mark_completed').text('<?php _e( 'Mark completed', 'woocommerce' )?>').appendTo("select[name='action2']");
});
</script>
<?php
}
}
2012-08-14 12:21:34 +00:00
/**
* Process the new bulk actions for changing order status
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
*/
function woocommerce_order_bulk_action() {
$wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
$action = $wp_list_table->current_action();
switch ( $action ) {
case 'mark_completed':
$new_status = 'completed';
$report_action = 'marked_completed';
break;
case 'mark_processing':
$new_status = 'processing';
$report_action = 'marked_processing';
break;
default:
return;
}
$changed = 0;
foreach( $_REQUEST['post'] as $post_id ) {
$order = new WC_Order( $post_id );
$order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ) );
$changed++;
}
$sendback = add_query_arg( array( 'post_type' => 'shop_order', $report_action => $changed, 'ids' => join( ',', $post_ids ) ), '' );
wp_redirect( $sendback );
exit();
}
2012-08-14 12:21:34 +00:00
/**
* Show confirmation message that order status changed for number of orders
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
*/
function woocommerce_order_bulk_admin_notices() {
global $post_type, $pagenow;
if ( isset( $_REQUEST['marked_completed'] ) || isset( $_REQUEST['marked_processing'] ) ) {
2012-10-16 14:46:21 +00:00
$number = isset( $_REQUEST['marked_processing'] ) ? absint( $_REQUEST['marked_processing'] ) : absint( $_REQUEST['marked_completed'] );
if ( 'edit.php' == $pagenow && 'shop_order' == $post_type ) {
$message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $number ), number_format_i18n( $number ) );
echo '<div class="updated"><p>' . $message . '</p></div>';
}
}
2011-12-12 16:34:56 +00:00
}