YITH_WCWL_Install

Install plugin table and create the wishlist page.

Defined (1)

The class is defined in the following location(s).

/includes/class.yith-wcwl-install.php  
  1. class YITH_WCWL_Install { 
  2.  
  3. /** 
  4. * Single instance of the class 
  5. * @var \YITH_WCWL_Install 
  6. * @since 2.0.0 
  7. */ 
  8. protected static $instance; 
  9.  
  10. /** 
  11. * Items table name 
  12. * @var string 
  13. * @access private 
  14. * @since 1.0.0 
  15. */ 
  16. private $_table_items; 
  17.  
  18. /** 
  19. * Items table name 
  20. * @var string 
  21. * @access private 
  22. * @since 1.0.0 
  23. */ 
  24. private $_table_wishlists; 
  25.  
  26. /** 
  27. * Returns single instance of the class 
  28. * @return \YITH_WCWL_Install 
  29. * @since 2.0.0 
  30. */ 
  31. public static function get_instance() { 
  32. if( is_null( self::$instance ) ) { 
  33. self::$instance = new self(); 
  34.  
  35. return self::$instance; 
  36.  
  37. /** 
  38. * Constructor. 
  39. * @since 1.0.0 
  40. */ 
  41. public function __construct() { 
  42. global $wpdb; 
  43.  
  44. // define local private attribute 
  45. $this->_table_items = $wpdb->prefix . 'yith_wcwl'; 
  46. $this->_table_wishlists = $wpdb->prefix . 'yith_wcwl_lists'; 
  47.  
  48. // add custom field to global $wpdb 
  49. $wpdb->yith_wcwl_items = $this->_table_items; 
  50. $wpdb->yith_wcwl_wishlists = $this->_table_wishlists; 
  51.  
  52. // define constant to use allover the application 
  53. define( 'YITH_WCWL_ITEMS_TABLE', $this->_table_items ); 
  54. define( 'YITH_WCWL_WISHLISTS_TABLE', $this->_table_wishlists ); 
  55.  
  56. /** 
  57. * @deprecated 
  58. */ 
  59. define( 'YITH_WCWL_TABLE', $this->_table_items ); 
  60.  
  61. /** 
  62. * Initiator. Replace the constructor. 
  63. * @since 1.0.0 
  64. */ 
  65. public function init() { 
  66. $this->_add_tables(); 
  67. $this->_add_pages(); 
  68.  
  69. update_option( 'yith_wcwl_version', YITH_WCWL_VERSION ); 
  70. update_option( 'yith_wcwl_db_version', YITH_WCWL_DB_VERSION ); 
  71.  
  72. /** 
  73. * Update db from version 1.0 to 2.0 
  74. * @since 1.0.0 
  75. */ 
  76. public function update() { 
  77. $this->_add_tables(); 
  78.  
  79. update_option( 'yith_wcwl_version', YITH_WCWL_VERSION ); 
  80. update_option( 'yith_wcwl_db_version', YITH_WCWL_DB_VERSION ); 
  81.  
  82. /** 
  83. * Set options to their default value. 
  84. * @param array $options 
  85. * @return bool 
  86. * @since 1.0.0 
  87. */ 
  88. public function default_options( $options ) { 
  89. foreach( $options as $section ) { 
  90. foreach( $section as $value ) { 
  91. if ( isset( $value['std'] ) && isset( $value['id'] ) ) { 
  92. add_option($value['id'], $value['std']); 
  93.  
  94. /** 
  95. * Check if the table of the plugin already exists. 
  96. * @return bool 
  97. * @since 1.0.0 
  98. */ 
  99. public function is_installed() { 
  100. global $wpdb; 
  101. $number_of_tables = $wpdb->query("SHOW TABLES LIKE '{$this->_table_items}%'" ); 
  102.  
  103. return (bool) ( $number_of_tables == 2 ); 
  104.  
  105. /** 
  106. * Add tables for a fresh installation 
  107. * @return void 
  108. * @access private 
  109. * @since 1.0.0 
  110. */ 
  111. private function _add_tables() { 
  112. $this->_add_wishlists_table(); 
  113. $this->_add_items_table(); 
  114.  
  115. /** 
  116. * Add the wishlists table to the database. 
  117. * @return void 
  118. * @access private 
  119. * @since 1.0.0 
  120. */ 
  121. private function _add_wishlists_table() { 
  122. global $wpdb; 
  123.  
  124. if( ! $this->is_installed() ) { 
  125. $sql = "CREATE TABLE {$this->_table_wishlists} ( 
  126. ID INT( 11 ) NOT NULL AUTO_INCREMENT,  
  127. user_id INT( 11 ) NOT NULL,  
  128. wishlist_slug VARCHAR( 200 ) NOT NULL,  
  129. wishlist_name TEXT,  
  130. wishlist_token VARCHAR( 64 ) NOT NULL UNIQUE,  
  131. wishlist_privacy TINYINT( 1 ) NOT NULL DEFAULT 0,  
  132. is_default TINYINT( 1 ) NOT NULL DEFAULT 0,  
  133. PRIMARY KEY ( ID ),  
  134. KEY ( wishlist_slug ) 
  135. ) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"; 
  136.  
  137. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  138. dbDelta( $sql ); 
  139.  
  140. return; 
  141.  
  142. /** 
  143. * Add the items table to the database. 
  144. * @return void 
  145. * @access private 
  146. * @since 1.0.0 
  147. */ 
  148. private function _add_items_table() { 
  149. global $wpdb; 
  150.  
  151. if( ! $this->is_installed() || get_option( 'yith_wcwl_db_version' ) != '2.0.0' ) { 
  152. $sql = "CREATE TABLE {$this->_table_items} ( 
  153. ID int( 11 ) NOT NULL AUTO_INCREMENT,  
  154. prod_id int( 11 ) NOT NULL,  
  155. quantity int( 11 ) NOT NULL,  
  156. user_id int( 11 ) NOT NULL,  
  157. wishlist_id int( 11 ) NULL,  
  158. dateadded timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
  159. PRIMARY KEY ( ID ),  
  160. KEY ( prod_id ) 
  161. ) DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"; 
  162.  
  163. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
  164. dbDelta( $sql ); 
  165.  
  166. return; 
  167.  
  168. /** 
  169. * Add a page "Wishlist". 
  170. * @return void 
  171. * @since 1.0.0 
  172. */ 
  173. private function _add_pages() { 
  174. global $wpdb; 
  175.  
  176. $option_value = get_option( 'yith-wcwl-page-id' ); 
  177.  
  178. if ( $option_value > 0 && get_post( $option_value ) ) { 
  179. return; 
  180.  
  181. $page_found = $wpdb->get_var( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_name` = 'wishlist' LIMIT 1;" ); 
  182.  
  183. if ( $page_found ) { 
  184. if ( ! $option_value ) { 
  185. update_option( 'yith-wcwl-page-id', $page_found ); 
  186.  
  187. return; 
  188.  
  189. $page_data = array( 
  190. 'post_status' => 'publish',  
  191. 'post_type' => 'page',  
  192. 'post_author' => 1,  
  193. 'post_name' => esc_sql( _x( 'wishlist', 'page_slug', 'yith-woocommerce-wishlist' ) ),  
  194. 'post_title' => __( 'Wishlist', 'yith-woocommerce-wishlist' ),  
  195. 'post_content' => '[yith_wcwl_wishlist]',  
  196. 'post_parent' => 0,  
  197. 'comment_status' => 'closed' 
  198. ); 
  199. $page_id = wp_insert_post( $page_data ); 
  200.  
  201. update_option( 'yith-wcwl-page-id', $page_id ); 
  202. update_option( 'yith_wcwl_wishlist_page_id', $page_id );