2011-08-09 15:16:18 +00:00
< ? php
/**
2011-08-10 17:11:11 +00:00
* WooCommerce Install
2011-08-09 15:16:18 +00:00
*
* Plugin install script which adds default pages , taxonomies , and database tables
*
2011-08-10 17:11:11 +00:00
* @ author WooThemes
2011-08-09 15:16:18 +00:00
* @ category Admin
2011-08-10 17:11:11 +00:00
* @ package WooCommerce
2011-08-09 15:16:18 +00:00
*/
2011-09-30 12:05:38 +00:00
/**
* Activate woocommerce
*/
function activate_woocommerce () {
install_woocommerce ();
// Update installed variable
update_option ( " woocommerce_installed " , 1 );
}
2011-08-09 15:16:18 +00:00
/**
2011-08-10 17:11:11 +00:00
* Install woocommerce
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
function install_woocommerce () {
2011-08-09 15:16:18 +00:00
2011-09-20 13:04:40 +00:00
global $woocommerce_settings ;
2011-09-19 09:49:52 +00:00
2011-08-09 15:16:18 +00:00
// Do install
2011-08-10 17:11:11 +00:00
woocommerce_default_options ();
woocommerce_create_pages ();
woocommerce_tables_install ();
woocommerce_default_taxonomies ();
2011-08-09 15:16:18 +00:00
// Update version
2011-08-10 17:11:11 +00:00
update_option ( " woocommerce_db_version " , WOOCOMMERCE_VERSION );
2011-08-09 15:16:18 +00:00
}
2011-09-20 13:04:40 +00:00
/**
* Install woocommerce redirect
*/
add_action ( 'admin_init' , 'install_woocommerce_redirect' );
function install_woocommerce_redirect () {
2011-09-23 17:37:35 +00:00
global $pagenow ;
2011-09-26 12:10:31 +00:00
if ( is_admin () && isset ( $_GET [ 'activate' ] ) && ( $_GET [ 'activate' ] == true ) && $pagenow == 'plugins.php' && get_option ( " woocommerce_installed " ) == 1 ) :
2011-09-23 17:37:35 +00:00
2011-09-26 12:10:31 +00:00
update_option ( " woocommerce_installed " , 0 );
2011-09-20 13:04:40 +00:00
flush_rewrite_rules ( false );
wp_redirect ( admin_url ( 'admin.php?page=woocommerce&installed=true' ));
exit ;
2011-09-23 17:37:35 +00:00
2011-09-20 13:04:40 +00:00
endif ;
}
2011-08-09 15:16:18 +00:00
/**
* Default options
*
* Sets up the default options used on the settings page
*/
2011-08-10 17:11:11 +00:00
function woocommerce_default_options () {
2011-09-13 14:17:58 +00:00
global $woocommerce_settings ;
2011-08-27 19:20:28 +00:00
2011-09-26 18:05:29 +00:00
// Include settings so that we can run through defaults
include_once ( 'admin-settings.php' );
foreach ( $woocommerce_settings as $section ) :
foreach ( $section as $value ) :
if ( isset ( $value [ 'std' ])) :
if ( $value [ 'type' ] == 'image_width' ) :
add_option ( $value [ 'id' ] . '_width' , $value [ 'std' ]);
add_option ( $value [ 'id' ] . '_height' , $value [ 'std' ]);
else :
add_option ( $value [ 'id' ], $value [ 'std' ]);
endif ;
endif ;
2011-08-27 19:20:28 +00:00
2011-09-26 18:05:29 +00:00
endforeach ;
2011-08-27 19:20:28 +00:00
2011-09-26 18:05:29 +00:00
endforeach ;
2011-08-10 17:11:11 +00:00
add_option ( 'woocommerce_shop_slug' , 'shop' );
2011-08-09 15:16:18 +00:00
}
2011-09-24 21:14:52 +00:00
/**
* Create a page
*
* Creates a page
*/
function woocommerce_create_page ( $slug , $option , $page_title = '' , $page_content = '' , $post_parent = 0 ) {
global $wpdb ;
$option_value = get_option ( $option );
if ( $option_value > 0 ) :
if ( get_post ( $option_value )) :
// Page exists
return ;
endif ;
endif ;
$page_found = $wpdb -> get_var ( " SELECT ID FROM " . $wpdb -> posts . " WHERE post_name = ' $slug ' LIMIT 1; " );
if ( $page_found ) :
// Page exists
return ;
endif ;
$page_data = array (
'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'
);
$page_id = wp_insert_post ( $page_data );
update_option ( $option , $page_id );
}
2011-08-09 15:16:18 +00:00
/**
* Create pages
*
* Creates pages that the plugin relies on , storing page id ' s in variables .
*/
2011-08-10 17:11:11 +00:00
function woocommerce_create_pages () {
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Shop page
woocommerce_create_page ( esc_sql ( _x ( 'shop' , 'page_slug' , 'woothemes' ) ), 'woocommerce_shop_page_id' , __ ( 'Shop' , 'woothemes' ), '' );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Cart page
woocommerce_create_page ( esc_sql ( _x ( 'cart' , 'page_slug' , 'woothemes' ) ), 'woocommerce_cart_page_id' , __ ( 'Cart' , 'woothemes' ), '[woocommerce_cart]' );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Checkout page
woocommerce_create_page ( esc_sql ( _x ( 'checkout' , 'page_slug' , 'woothemes' ) ), 'woocommerce_checkout_page_id' , __ ( 'Checkout' , 'woothemes' ), '[woocommerce_checkout]' );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Order tracking page
woocommerce_create_page ( esc_sql ( _x ( 'order-tracking' , 'page_slug' , 'woothemes' ) ), 'woocommerce_order_tracking_page_id' , __ ( 'Track your order' , 'woothemes' ), '[woocommerce_order_tracking]' );
// My Account page
woocommerce_create_page ( esc_sql ( _x ( 'my-account' , 'page_slug' , 'woothemes' ) ), 'woocommerce_myaccount_page_id' , __ ( 'My Account' , 'woothemes' ), '[woocommerce_my_account]' );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Edit address page
woocommerce_create_page ( esc_sql ( _x ( 'edit-address' , 'page_slug' , 'woothemes' ) ), 'woocommerce_edit_address_page_id' , __ ( 'Edit My Address' , 'woothemes' ), '[woocommerce_edit_address]' , get_option ( 'woocommerce_myaccount_page_id' ) );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// View order page
woocommerce_create_page ( esc_sql ( _x ( 'view-order' , 'page_slug' , 'woothemes' ) ), 'woocommerce_view_order_page_id' , __ ( 'View Order' , 'woothemes' ), '[woocommerce_view_order]' , get_option ( 'woocommerce_myaccount_page_id' ) );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Change password page
woocommerce_create_page ( esc_sql ( _x ( 'change-password' , 'page_slug' , 'woothemes' ) ), 'woocommerce_change_password_page_id' , __ ( 'Change Password' , 'woothemes' ), '[woocommerce_change_password]' , get_option ( 'woocommerce_myaccount_page_id' ) );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Pay page
woocommerce_create_page ( esc_sql ( _x ( 'pay' , 'page_slug' , 'woothemes' ) ), 'woocommerce_pay_page_id' , __ ( 'Checkout → Pay' , 'woothemes' ), '[woocommerce_pay]' , get_option ( 'woocommerce_checkout_page_id' ) );
2011-08-09 15:16:18 +00:00
2011-09-24 21:14:52 +00:00
// Thanks page
woocommerce_create_page ( esc_sql ( _x ( 'order-received' , 'page_slug' , 'woothemes' ) ), 'woocommerce_thanks_page_id' , __ ( 'Order Received' , 'woothemes' ), '[woocommerce_thankyou]' , get_option ( 'woocommerce_checkout_page_id' ) );
2011-08-09 15:16:18 +00:00
}
/**
* Table Install
*
* Sets up the database tables which the plugin needs to function .
*/
2011-08-10 17:11:11 +00:00
function woocommerce_tables_install () {
2011-08-09 15:16:18 +00:00
global $wpdb ;
2011-08-10 17:11:11 +00:00
$collate = '' ;
2011-08-09 15:16:18 +00:00
if ( $wpdb -> supports_collation ()) {
if ( ! empty ( $wpdb -> charset )) $collate = " DEFAULT CHARACTER SET $wpdb->charset " ;
if ( ! empty ( $wpdb -> collate )) $collate .= " COLLATE $wpdb->collate " ;
}
2011-09-29 13:04:09 +00:00
require_once ( ABSPATH . 'wp-admin/includes/upgrade.php' );
2011-08-10 17:11:11 +00:00
$sql = " CREATE TABLE IF NOT EXISTS " . $wpdb -> prefix . " woocommerce_attribute_taxonomies " . " (
2011-08-09 15:16:18 +00:00
`attribute_id` mediumint ( 9 ) NOT NULL AUTO_INCREMENT ,
`attribute_name` varchar ( 200 ) NOT NULL ,
2011-08-31 12:31:47 +00:00
`attribute_label` longtext NULL ,
2011-08-09 15:16:18 +00:00
`attribute_type` varchar ( 200 ) NOT NULL ,
PRIMARY KEY id ( `attribute_id` )) $collate ; " ;
2011-09-29 13:04:09 +00:00
dbDelta ( $sql );
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
$sql = " CREATE TABLE IF NOT EXISTS " . $wpdb -> prefix . " woocommerce_downloadable_product_permissions " . " (
2011-08-09 15:16:18 +00:00
`product_id` mediumint ( 9 ) NOT NULL ,
`user_email` varchar ( 200 ) NOT NULL ,
`user_id` mediumint ( 9 ) NULL ,
`order_key` varchar ( 200 ) NOT NULL ,
`downloads_remaining` varchar ( 9 ) NULL ,
PRIMARY KEY id ( `product_id` , `order_key` )) $collate ; " ;
2011-09-29 13:04:09 +00:00
dbDelta ( $sql );
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
$sql = " CREATE TABLE IF NOT EXISTS " . $wpdb -> prefix . " woocommerce_termmeta " . " (
2011-08-09 15:16:18 +00:00
`meta_id` bigint ( 20 ) NOT NULL AUTO_INCREMENT ,
2011-08-10 17:11:11 +00:00
`woocommerce_term_id` bigint ( 20 ) NOT NULL ,
2011-08-09 15:16:18 +00:00
`meta_key` varchar ( 255 ) NULL ,
`meta_value` longtext NULL ,
PRIMARY KEY id ( `meta_id` )) $collate ; " ;
2011-09-29 13:04:09 +00:00
dbDelta ( $sql );
2011-08-09 15:16:18 +00:00
}
/**
* Default taxonomies
*
* Adds the default terms for taxonomies - product types and order statuses . Modify at your own risk .
*/
2011-08-10 17:11:11 +00:00
function woocommerce_default_taxonomies () {
2011-08-09 15:16:18 +00:00
2011-09-22 19:50:58 +00:00
if ( ! taxonomy_exists ( 'product_type' )) :
register_taxonomy ( 'product_type' , array ( 'post' ));
register_taxonomy ( 'shop_order_status' , array ( 'post' ));
endif ;
2011-08-09 15:16:18 +00:00
$product_types = array (
'simple' ,
'grouped' ,
2011-08-10 17:11:11 +00:00
'variable' ,
2011-08-09 15:16:18 +00:00
'downloadable' ,
'virtual'
);
foreach ( $product_types as $type ) {
2011-09-22 19:50:58 +00:00
if ( ! get_term_by ( 'slug' , sanitize_title ( $type ), 'product_type' )) {
2011-08-09 15:16:18 +00:00
wp_insert_term ( $type , 'product_type' );
}
}
$order_status = array (
'pending' ,
2011-09-22 19:50:58 +00:00
'failed' ,
2011-08-09 15:16:18 +00:00
'on-hold' ,
'processing' ,
'completed' ,
'refunded' ,
'cancelled'
);
foreach ( $order_status as $status ) {
2011-09-22 19:50:58 +00:00
if ( ! get_term_by ( 'slug' , sanitize_title ( $status ), 'shop_order_status' )) {
2011-08-09 15:16:18 +00:00
wp_insert_term ( $status , 'shop_order_status' );
}
}
2011-09-22 19:50:58 +00:00
2011-08-09 15:16:18 +00:00
}