woocommerce/admin/woocommerce-admin-install.php

376 lines
13 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce Install
2012-08-14 12:21:34 +00:00
*
* Plugin install script which adds default pages, taxonomies, and database tables to WordPress. Runs on activation and upgrade.
2011-08-09 15:16:18 +00:00
*
2011-08-10 17:11:11 +00:00
* @author WooThemes
2011-08-09 15:16:18 +00:00
* @category Admin
2012-08-14 12:21:34 +00:00
* @package WooCommerce/Admin/Install
2012-12-03 19:19:58 +00:00
* @version 2.0.0
2011-08-09 15:16:18 +00:00
*/
2012-10-15 10:32:24 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2011-08-09 15:16:18 +00:00
/**
2012-08-14 12:21:34 +00:00
* Runs the installer.
*
* @access public
* @return void
2011-08-09 15:16:18 +00:00
*/
2011-12-12 16:34:56 +00:00
function do_install_woocommerce() {
global $woocommerce_settings, $woocommerce;
2012-11-27 16:22:47 +00:00
2011-08-09 15:16:18 +00:00
// Do install
2011-08-10 17:11:11 +00:00
woocommerce_default_options();
woocommerce_tables_install();
2012-09-20 15:35:15 +00:00
woocommerce_init_roles();
2012-11-27 16:22:47 +00:00
// Register post types
$woocommerce->init_taxonomy();
// Add default taxonomies
2011-08-10 17:11:11 +00:00
woocommerce_default_taxonomies();
2012-08-14 12:21:34 +00:00
// Cron jobs
wp_clear_scheduled_hook( 'woocommerce_scheduled_sales' );
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
wp_clear_scheduled_hook( 'woocommerce_cleanup_sessions' );
2012-12-23 12:29:16 +00:00
$ve = get_option('gmt_offset') > 0 ? '+' : '-';
wp_schedule_event( strtotime( 'tomorrow ' . $ve . get_option('gmt_offset') . ' HOURS' ), 'daily', 'woocommerce_scheduled_sales');
$held_duration = get_option( 'woocommerce_hold_stock_minutes', null );
if ( is_null( $held_duration ) )
$held_duration = '60';
if ( $held_duration != '' )
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
wp_schedule_event( time(), 'twicedaily', 'woocommerce_cleanup_sessions' );
2012-10-18 09:33:02 +00:00
// Install files and folders for uploading files and prevent hotlinking
2012-05-17 11:58:17 +00:00
$upload_dir = wp_upload_dir();
2012-11-27 16:22:47 +00:00
2012-10-18 09:33:02 +00:00
$files = array(
array(
'base' => $upload_dir['basedir'] . '/woocommerce_uploads',
'file' => '.htaccess',
'content' => 'deny from all'
),
array(
'base' => $upload_dir['basedir'] . '/woocommerce_uploads',
'file' => 'index.html',
'content' => ''
),
array(
'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs',
'file' => '.htaccess',
'content' => 'deny from all'
),
array(
'base' => WP_PLUGIN_DIR . "/" . plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/logs',
'file' => 'index.html',
'content' => ''
)
);
2012-11-27 16:22:47 +00:00
2012-10-18 09:33:02 +00:00
foreach ( $files as $file ) {
if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {
fwrite( $file_handle, $file['content'] );
fclose( $file_handle );
}
}
}
2012-08-14 12:21:34 +00:00
2012-02-18 13:25:51 +00:00
// Clear transient cache
$woocommerce->clear_product_transients();
2012-08-14 12:21:34 +00:00
2012-05-01 18:28:45 +00:00
// Recompile LESS styles if they are custom
if ( get_option( 'woocommerce_frontend_css' ) == 'yes' ) {
2012-08-14 12:21:34 +00:00
2012-05-17 11:58:17 +00:00
$colors = get_option( 'woocommerce_frontend_css_colors' );
2012-08-14 12:21:34 +00:00
2012-10-18 09:33:02 +00:00
if ( ( ! empty( $colors['primary'] ) && ! empty( $colors['secondary'] ) && ! empty( $colors['highlight'] ) && ! empty( $colors['content_bg'] ) && ! empty( $colors['subtext'] ) ) && ( $colors['primary'] != '#ad74a2' || $colors['secondary'] != '#f7f6f7' || $colors['highlight'] != '#85ad74' || $colors['content_bg'] != '#ffffff' || $colors['subtext'] != '#777777' ) )
2012-05-01 18:28:45 +00:00
woocommerce_compile_less_styles();
2012-11-27 16:22:47 +00:00
2012-05-01 18:28:45 +00:00
}
2012-10-18 09:33:02 +00:00
// Queue upgrades
2012-11-14 12:07:12 +00:00
$current_version = get_option( 'woocommerce_version', null );
$current_db_version = get_option( 'woocommerce_db_version', null );
2012-11-27 16:22:47 +00:00
2013-04-24 14:37:49 +00:00
if ( version_compare( $current_db_version, '2.0.9', '<' ) && null !== $current_db_version ) {
2013-03-01 16:03:10 +00:00
update_option( '_wc_needs_update', 1 );
2012-10-18 09:33:02 +00:00
} else {
update_option( 'woocommerce_db_version', $woocommerce->version );
}
2012-11-27 16:22:47 +00:00
2012-11-14 12:07:12 +00:00
// Update version
update_option( 'woocommerce_version', $woocommerce->version );
2013-03-05 18:50:44 +00:00
// Flush rewrite rules
flush_rewrite_rules();
2011-08-09 15:16:18 +00:00
}
2012-08-14 12:21:34 +00:00
2011-08-09 15:16:18 +00:00
/**
* Default options
2012-08-14 12:21:34 +00:00
*
2011-08-09 15:16:18 +00:00
* Sets up the default options used on the settings page
2012-08-14 12:21:34 +00:00
*
* @access public
* @return void
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_default_options() {
2011-09-13 14:17:58 +00:00
global $woocommerce_settings;
2012-08-14 12:21:34 +00:00
2011-09-26 18:05:29 +00:00
// Include settings so that we can run through defaults
include_once( 'settings/settings-init.php' );
2012-08-14 12:21:34 +00:00
foreach ( $woocommerce_settings as $section ) {
2012-05-17 11:58:17 +00:00
foreach ( $section as $value ) {
if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
add_option( $value['id'], $value['default'] );
2012-05-17 11:58:17 +00:00
}
}
}
2011-08-09 15:16:18 +00:00
}
2012-08-14 12:21:34 +00:00
/**
* Create a page
2012-08-14 12:21:34 +00:00
*
* @access public
* @param mixed $slug Slug for the new page
* @param mixed $option Option name to store the page's ID
* @param string $page_title (default: '') Title for the new page
* @param string $page_content (default: '') Content for the new page
* @param int $post_parent (default: 0) Parent for the new page
* @return void
*/
function woocommerce_create_page( $slug, $option, $page_title = '', $page_content = '', $post_parent = 0 ) {
global $wpdb;
2012-08-14 12:21:34 +00:00
$option_value = get_option( $option );
if ( $option_value > 0 && get_post( $option_value ) )
2012-05-17 11:58:17 +00:00
return;
2012-08-14 12:21:34 +00:00
2012-10-16 14:46:21 +00:00
$page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->posts . " WHERE post_name = %s LIMIT 1;", $slug ) );
if ( $page_found ) {
2012-08-14 12:21:34 +00:00
if ( ! $option_value )
2012-05-17 11:58:17 +00:00
update_option( $option, $page_found );
return;
}
2012-08-14 12:21:34 +00:00
$page_data = array(
2012-05-17 11:58:17 +00:00
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $slug,
'post_title' => $page_title,
'post_content' => $page_content,
'post_parent' => $post_parent,
'comment_status' => 'closed'
);
2012-05-17 11:58:17 +00:00
$page_id = wp_insert_post( $page_data );
2012-08-14 12:21:34 +00:00
2012-05-17 11:58:17 +00:00
update_option( $option, $page_id );
}
2012-08-14 12:21:34 +00:00
2011-08-09 15:16:18 +00:00
/**
2012-08-14 12:21:34 +00:00
* Create pages that the plugin relies on, storing page id's in variables.
*
* @access public
* @return void
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_create_pages() {
2012-08-14 12:21:34 +00:00
// Shop page
woocommerce_create_page( esc_sql( _x( 'shop', 'page_slug', 'woocommerce' ) ), 'woocommerce_shop_page_id', __( 'Shop', 'woocommerce' ), '' );
2012-08-14 12:21:34 +00:00
// Cart page
woocommerce_create_page( esc_sql( _x( 'cart', 'page_slug', 'woocommerce' ) ), 'woocommerce_cart_page_id', __( 'Cart', 'woocommerce' ), '[woocommerce_cart]' );
2012-08-14 12:21:34 +00:00
// Checkout page
woocommerce_create_page( esc_sql( _x( 'checkout', 'page_slug', 'woocommerce' ) ), 'woocommerce_checkout_page_id', __( 'Checkout', 'woocommerce' ), '[woocommerce_checkout]' );
2012-08-14 12:21:34 +00:00
// My Account page
woocommerce_create_page( esc_sql( _x( 'my-account', 'page_slug', 'woocommerce' ) ), 'woocommerce_myaccount_page_id', __( 'My Account', 'woocommerce' ), '[woocommerce_my_account]' );
2011-08-09 15:16:18 +00:00
// Lost password page
woocommerce_create_page( esc_sql( _x( 'lost-password', 'page_slug', 'woocommerce' ) ), 'woocommerce_lost_password_page_id', __( 'Lost Password', 'woocommerce' ), '[woocommerce_lost_password]', woocommerce_get_page_id( 'myaccount' ) );
// Edit address page
woocommerce_create_page( esc_sql( _x( 'edit-address', 'page_slug', 'woocommerce' ) ), 'woocommerce_edit_address_page_id', __( 'Edit My Address', 'woocommerce' ), '[woocommerce_edit_address]', woocommerce_get_page_id( 'myaccount' ) );
2012-08-14 12:21:34 +00:00
// View order page
woocommerce_create_page( esc_sql( _x( 'view-order', 'page_slug', 'woocommerce' ) ), 'woocommerce_view_order_page_id', __( 'View Order', 'woocommerce' ), '[woocommerce_view_order]', woocommerce_get_page_id( 'myaccount' ) );
2011-08-09 15:16:18 +00:00
// Change password page
woocommerce_create_page( esc_sql( _x( 'change-password', 'page_slug', 'woocommerce' ) ), 'woocommerce_change_password_page_id', __( 'Change Password', 'woocommerce' ), '[woocommerce_change_password]', woocommerce_get_page_id( 'myaccount' ) );
2011-08-09 15:16:18 +00:00
// Logout page
woocommerce_create_page( esc_sql( _x( 'logout', 'page_slug', 'woocommerce' ) ), 'woocommerce_logout_page_id', __( 'Logout', 'woocommerce' ), '', woocommerce_get_page_id( 'myaccount' ) );
// Pay page
woocommerce_create_page( esc_sql( _x( 'pay', 'page_slug', 'woocommerce' ) ), 'woocommerce_pay_page_id', __( 'Checkout &rarr; Pay', 'woocommerce' ), '[woocommerce_pay]', woocommerce_get_page_id( 'checkout' ) );
2012-08-14 12:21:34 +00:00
// Thanks page
woocommerce_create_page( esc_sql( _x( 'order-received', 'page_slug', 'woocommerce' ) ), 'woocommerce_thanks_page_id', __( 'Order Received', 'woocommerce' ), '[woocommerce_thankyou]', woocommerce_get_page_id( 'checkout' ) );
2011-08-09 15:16:18 +00:00
}
2012-08-14 12:21:34 +00:00
2011-08-09 15:16:18 +00:00
/**
2012-08-14 12:21:34 +00:00
* Set up the database tables which the plugin needs to function.
*
* Tables:
* woocommerce_attribute_taxonomies - Table for storing attribute taxonomies - these are user defined
* woocommerce_termmeta - Term meta table - sadly WordPress does not have termmeta so we need our own
* woocommerce_downloadable_product_permissions - Table for storing user and guest download permissions.
* KEY(order_id, product_id, download_id) used for organizing downloads on the My Account page
* woocommerce_order_items - Order line items are stored in a table to make them easily queryable for reports
* woocommerce_order_itemmeta - Order line item meta is stored in a table for storing extra data.
* woocommerce_tax_rates - Tax Rates are stored inside 2 tables making tax queries simple and efficient.
* woocommerce_tax_rate_locations - Each rate can be applied to more than one postcode/city hence the second table.
*
2012-08-14 12:21:34 +00:00
* @access public
* @return void
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_tables_install() {
2012-01-24 16:56:10 +00:00
global $wpdb, $woocommerce;
2012-08-14 12:21:34 +00:00
$wpdb->hide_errors();
2011-08-10 17:11:11 +00:00
$collate = '';
if ( $wpdb->has_cap( 'collation' ) ) {
2012-11-27 16:22:47 +00:00
if( ! empty($wpdb->charset ) )
$collate .= "DEFAULT CHARACTER SET $wpdb->charset";
2012-11-27 16:22:47 +00:00
if( ! empty($wpdb->collate ) )
$collate .= " COLLATE $wpdb->collate";
2011-08-09 15:16:18 +00:00
}
2012-08-14 12:21:34 +00:00
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
2012-04-10 01:00:02 +00:00
// WooCommerce Tables
$woocommerce_tables = "
CREATE TABLE {$wpdb->prefix}woocommerce_attribute_taxonomies (
2012-04-10 01:00:02 +00:00
attribute_id bigint(20) NOT NULL auto_increment,
attribute_name varchar(200) NOT NULL,
attribute_label longtext NULL,
attribute_type varchar(200) NOT NULL,
2012-10-09 14:57:02 +00:00
attribute_orderby varchar(200) NOT NULL,
PRIMARY KEY (attribute_id),
KEY attribute_name (attribute_name)
2012-04-10 01:00:02 +00:00
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
2013-03-22 20:47:07 +00:00
meta_id bigint(20) NOT NULL auto_increment,
woocommerce_term_id bigint(20) NOT NULL,
meta_key varchar(255) NULL,
meta_value longtext NULL,
PRIMARY KEY (meta_id),
KEY woocommerce_term_id (woocommerce_term_id),
KEY meta_key (meta_key)
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_downloadable_product_permissions (
download_id varchar(32) NOT NULL,
2012-04-10 01:00:02 +00:00
product_id bigint(20) NOT NULL,
order_id bigint(20) NOT NULL DEFAULT 0,
order_key varchar(200) NOT NULL,
user_email varchar(200) NOT NULL,
user_id bigint(20) NULL,
downloads_remaining varchar(9) NULL,
access_granted datetime NOT NULL default '0000-00-00 00:00:00',
access_expires datetime NULL default null,
download_count bigint(20) NOT NULL DEFAULT 0,
PRIMARY KEY (product_id,order_id,order_key,download_id),
KEY download_order_product (download_id,order_id,product_id)
2012-04-10 01:00:02 +00:00
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_order_items (
order_item_id bigint(20) NOT NULL auto_increment,
2012-12-13 17:32:41 +00:00
order_item_name longtext NOT NULL,
order_item_type varchar(200) NOT NULL DEFAULT '',
2012-11-27 16:22:47 +00:00
order_id bigint(20) NOT NULL,
PRIMARY KEY (order_item_id),
KEY order_id (order_id)
2012-10-18 10:19:03 +00:00
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_order_itemmeta (
2012-10-18 10:19:03 +00:00
meta_id bigint(20) NOT NULL auto_increment,
order_item_id bigint(20) NOT NULL,
2012-10-18 10:19:03 +00:00
meta_key varchar(255) NULL,
meta_value longtext NULL,
PRIMARY KEY (meta_id),
KEY order_item_id (order_item_id),
KEY meta_key (meta_key)
2012-10-18 10:19:03 +00:00
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_tax_rates (
2012-11-30 15:11:28 +00:00
tax_rate_id bigint(20) NOT NULL auto_increment,
tax_rate_country varchar(200) NOT NULL DEFAULT '',
tax_rate_state varchar(200) NOT NULL DEFAULT '',
tax_rate varchar(200) NOT NULL DEFAULT '',
tax_rate_name varchar(200) NOT NULL DEFAULT '',
tax_rate_priority bigint(20) NOT NULL,
tax_rate_compound int(1) NOT NULL DEFAULT 0,
tax_rate_shipping int(1) NOT NULL DEFAULT 1,
tax_rate_order bigint(20) NOT NULL,
tax_rate_class varchar(200) NOT NULL DEFAULT '',
PRIMARY KEY (tax_rate_id),
KEY tax_rate_country (tax_rate_country),
KEY tax_rate_state (tax_rate_state),
KEY tax_rate_class (tax_rate_class),
KEY tax_rate_priority (tax_rate_priority)
2012-11-30 15:11:28 +00:00
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_tax_rate_locations (
2012-11-30 15:11:28 +00:00
location_id bigint(20) NOT NULL auto_increment,
location_code varchar(255) NOT NULL,
tax_rate_id bigint(20) NOT NULL,
location_type varchar(40) NOT NULL,
PRIMARY KEY (location_id),
KEY location_type (location_type),
KEY location_type_code (location_type,location_code)
2012-11-30 15:11:28 +00:00
) $collate;
2012-04-10 01:00:02 +00:00
";
dbDelta( $woocommerce_tables );
2011-08-09 15:16:18 +00:00
}
2012-08-14 12:21:34 +00:00
2011-08-09 15:16:18 +00:00
/**
2012-08-14 12:21:34 +00:00
* Add the default terms for WC taxonomies - product types and order statuses. Modify this at your own risk.
*
* @access public
* @return void
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function woocommerce_default_taxonomies() {
2012-08-14 12:21:34 +00:00
2012-10-18 09:33:02 +00:00
$taxonomies = array(
'product_type' => array(
'simple',
'grouped',
'variable',
'external'
),
'shop_order_status' => array(
'pending',
'failed',
'on-hold',
'processing',
'completed',
'refunded',
'cancelled'
)
2011-08-09 15:16:18 +00:00
);
2012-08-14 12:21:34 +00:00
2012-10-18 09:33:02 +00:00
foreach ( $taxonomies as $taxonomy => $terms ) {
foreach ( $terms as $term ) {
if ( ! get_term_by( 'slug', sanitize_title( $term ), $taxonomy ) ) {
wp_insert_term( $term, $taxonomy );
}
2012-05-17 11:58:17 +00:00
}
}
2013-04-24 14:37:49 +00:00
}