Advertisement
Guest User

Untitled

a guest
Aug 9th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.66 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Copyright (c) 2018. Code by https://kiro.tech
  4.  */
  5.  
  6. if(!defined('ABSPATH')) {
  7.     exit; // Exit if accessed directly
  8. }
  9.  
  10. class VeroDate_DemoImport
  11. {
  12.  
  13.  
  14.     public function __construct()
  15.     {
  16.  
  17. //Just do nothing here
  18.  
  19.     }
  20.  
  21.  
  22.     public function createPosts($title, $type)
  23.     {
  24.  
  25.         $author_id = 1;
  26.         $slug      = $title;
  27.  
  28.         $get_posts = get_page_by_title($title);
  29.  
  30.         if(NULL == $get_posts) {
  31.             $post_id = wp_insert_post(
  32.                 array(
  33.                     'comment_status' => 'closed',
  34.                     'ping_status'    => 'closed',
  35.                     'post_author'    => $author_id,
  36.                     'post_name'      => $slug,
  37.                     'post_title'     => $title,
  38.                     'post_status'    => 'publish',
  39.                     'post_type'      => $type
  40.                 )
  41.             );
  42.             echo '<p>' . __("Post type : {$type} created", 'verodate') . '</p>';
  43.         }
  44.     }
  45.  
  46.  
  47.     /**
  48.      * @param $args
  49.      *
  50.      * Insert default VeroDate pages
  51.      */
  52.  
  53.     protected function __insertPages($args)
  54.     {
  55.  
  56.         $page = array();
  57.  
  58.         foreach($args as $arg) {
  59.  
  60.             $page['name'] = $arg['post_title'];
  61.             $page['id']   = wp_insert_post($arg);
  62.  
  63.             echo '<p>' . __("Page {$arg['post_title']} created", 'verodate') . '</p>';
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69.  
  70.     /**
  71.      * Import demo content from xml file.
  72.      */
  73.     public function importDemoXml($xml)
  74.     {
  75.  
  76.         $xmlImported = get_option('verodate_xml_import_' . $xml);
  77.  
  78.         if(empty($xmlImported)):
  79.  
  80.             define('WP_LOAD_IMPORTERS', TRUE);
  81.  
  82.             require_once(get_template_directory() . '/inc/demo_import/wordpress-importer.php');
  83.  
  84.             $wp_import = new WP_Import();
  85.  
  86.             $wp_import->base_url = get_site_url();
  87.  
  88.             $wp_import->fetch_attachments = TRUE;
  89.  
  90.             $wp_import->import(get_template_directory() . '/demo/' . $xml);
  91.  
  92.             /* Option for check or XML file already imported */
  93.             update_option('verodate_xml_import_' . $xml, 1);
  94.  
  95.         else:
  96.  
  97.             echo '<p>' . __('Posts already imported.', 'verodate') . '</p>';
  98.  
  99.         endif;
  100.  
  101.     }
  102.     /*
  103.      * @since 1.0.1
  104.      */
  105.     public function import_widget()
  106.     {
  107.  
  108.         /*
  109.          * Create Category widget for Left Sidebar.
  110.          * Category object widget : WP_Widget_Categories
  111.          * List widget which will be needed : WP_Widget_Categories , WP_Widget_Tag_Cloud , WP_Widget_Recent_Posts , WC_Widget_Price_Filter , WC_Widget_Products
  112.          */
  113.  
  114.         /* Blog left sidebar*/
  115.         $blog_sidebars = [
  116.             'blog-left-sidebar',
  117.             'blog-right-sidebar',
  118.             'page-left-sidebar',
  119.             'page-right-sidebar',
  120.         ];
  121.  
  122.         $this->__create_widget($blog_sidebars, 'WP_Widget_Categories', 'categories');
  123.  
  124.         $this->__create_widget($blog_sidebars, 'WP_Widget_Recent_Posts', 'recent-posts');
  125.  
  126.         $this->__create_widget($blog_sidebars, 'WP_Widget_Tag_Cloud', 'tag_cloud');
  127.  
  128.         /*
  129.          * Shop widgets
  130.          */
  131.         if(class_exists('WooCommerce')) {
  132.  
  133.             $shop_sidebars = [
  134.                 'shop-left-sidebar',
  135.                 'shop-right-sidebar',
  136.             ];
  137.  
  138.             $this->__create_widget($shop_sidebars, 'WP_Widget_Categories', 'categories');
  139.  
  140.             $this->__create_widget($shop_sidebars, 'WC_Widget_Price_Filter', 'woocommerce_price_filter');
  141.  
  142.             $this->__create_widget($shop_sidebars, 'WC_Widget_Products', 'woocommerce_products');
  143.  
  144.             $this->__create_widget($shop_sidebars, 'WC_Widget_Product_Tag_Cloud', 'woocommerce_product_tag_cloud');
  145.  
  146.         }
  147.     }
  148.  
  149.  
  150.     /**
  151.      * @param $sidebar
  152.      * @param $object
  153.      * @param $widget
  154.      *
  155.      * @since 1.0.1
  156.      * Get all widgets and insert new widget here.
  157.      */
  158.     protected function __create_widget($sidebars = array(), $object, $widget)
  159.     {
  160.  
  161.         /* Get all sidebars */
  162.         $sidebars_widgets = get_option('sidebars_widgets', array());
  163.         /* Get all widgets */
  164.         $widgets = $GLOBALS['wp_widget_factory']->widgets;
  165.  
  166.         /* Get widget base name */
  167.         $control = $widgets[$object]->control_options['id_base'];
  168.  
  169.         /* Generate new widget name */
  170.  
  171.         /* insert new widget name into sidebar array */
  172.  
  173.         $number = $widgets[$object]->number + 1;
  174.  
  175.         $full_widget_name = $control . '-' . $number;
  176.  
  177.         foreach($sidebars as $sidebar):
  178.  
  179.             $sidebars_widgets[$sidebar][] = $full_widget_name;
  180.             /* Get widget settings content */
  181.             $pages[$number] = $this->__widget_content($widget);
  182.  
  183.             update_option('widget_' . $widget, $pages);
  184.  
  185.             update_option('sidebars_widgets', $sidebars_widgets);
  186.             $number++;
  187.         endforeach;
  188.  
  189.     }
  190.  
  191.  
  192.     /**
  193.      * @param $widget
  194.      * Create widget settings content.
  195.      *
  196.      * @return array
  197.      * @since 1.0.1
  198.      */
  199.     protected function __widget_content($widget)
  200.     {
  201.  
  202.         $widget_content = array();
  203.  
  204.         switch($widget) {
  205.  
  206.             case 'categories':
  207.  
  208.                 $widget_content = [
  209.  
  210.                     'title'        => 'Categories',
  211.                     'count'        => 0,
  212.                     'hierarchical' => 0,
  213.                     'dropdown'     => 0
  214.  
  215.                 ];
  216.  
  217.                 break;
  218.  
  219.             case 'tag_cloud':
  220.                 $widget_content = [
  221.  
  222.                     'title'    => 'Tags',
  223.                     'count'    => 0,
  224.                     'taxonomy' => 'post_tag',
  225.  
  226.                 ];
  227.                 break;
  228.             case 'recent-posts':
  229.  
  230.                 $widget_content = [
  231.  
  232.                     'title'     => 'Recent Post',
  233.                     'number'    => 5,
  234.                     'show_date' => 0,
  235.  
  236.                 ];
  237.  
  238.                 break;
  239.             case 'woocommerce_price_filter':
  240.  
  241.                 $widget_content = [
  242.                     'title' => 'Select Price'
  243.                 ];
  244.  
  245.                 break;
  246.  
  247.             case 'woocommerce_products':
  248.  
  249.                 $widget_content = [
  250.                     'title'       => 'Latest Products',
  251.                     'number'      => 6,
  252.                     'show'        => 0,
  253.                     'orderby'     => 'date',
  254.                     'order'       => 'desc',
  255.                     'hide_free'   => 1,
  256.                     'show_hidden' => 1,
  257.                 ];
  258.  
  259.                 break;
  260.             case 'woocommerce_product_tag_cloud':
  261.  
  262.                 $widget_content = [
  263.                     'title' => 'Tags',
  264.                 ];
  265.  
  266.                 break;
  267.         }
  268.  
  269.         return $widget_content;
  270.  
  271.     }
  272.  
  273.  
  274.     /*
  275.      * Import groups
  276.      */
  277.  
  278.     public function verodate_import_groups($users = FALSE)
  279.     {
  280.  
  281.         $groups    = array();
  282.         $group_ids = array();
  283.  
  284.         if(!bp_is_active('groups')) {
  285.             return $group_ids;
  286.         }
  287.  
  288.         // Use currently available users from DB if no default were specified.
  289.         if(empty($users)) {
  290.             $users = get_users();
  291.         }
  292.  
  293.         $groups = array(
  294.             0 => array(
  295.                 'name'         => 'Gone in 60 Seconds',
  296.                 'description'  => 'A retired master car thief must come back to the industry and steal 50 cars with his crew in one night to save his brother\'s life.',
  297.                 'status'       => 'public',
  298.                 'enable_forum' => 1,
  299.             ),
  300.             1 => array(
  301.                 'name'         => 'Next',
  302.                 'description'  => 'A Las Vegas magician who can see into the future is pursued by FBI agents seeking to use his abilities to prevent a nuclear terrorist attack.',
  303.                 'status'       => 'hidden',
  304.                 'enable_forum' => 0,
  305.             ),
  306.             2 => array(
  307.                 'name'         => 'RocknRolla',
  308.                 'description'  => 'In London, a real-estate scam puts millions of pounds up for grabs, attracting some of the city\'s scrappiest tough guys and its more established underworld types, all of whom are looking to get rich quick. While the city\'s seasoned criminals vie for the cash, an unexpected player -- a drugged out rock \'n\' roller presumed to be dead but very much alive...',
  309.                 'status'       => 'public',
  310.                 'enable_forum' => 1,
  311.             ),
  312.             3 => array(
  313.                 'name'         => 'Sherlock Holmes',
  314.                 'description'  => 'Detective Sherlock Holmes and his stalwart partner Watson engage in a battle of wits and brawn with a nemesis whose plot is a threat to all of England.',
  315.                 'status'       => 'public',
  316.                 'enable_forum' => 0,
  317.             ),
  318.             4 => array(
  319.                 'name'         => 'Snatch',
  320.                 'description'  => 'Unscrupulous boxing promoters, violent bookmakers, a Russian gangster, incompetent amateur robbers, and supposedly Jewish jewelers fight to track down a priceless stolen diamond.',
  321.                 'status'       => 'public',
  322.                 'enable_forum' => 1,
  323.             )
  324.         );
  325.  
  326.         foreach($groups as $group) {
  327.             $creator_id = is_object($users[array_rand($users)]) ? $users[array_rand($users)]->ID : $users[array_rand($users)];
  328.             $cur        = groups_create_group(array(
  329.                 'creator_id'   => $creator_id,
  330.                 'name'         => $group['name'],
  331.                 'description'  => $group['description'],
  332.                 'slug'         => groups_check_slug(sanitize_title(esc_attr($group['name']))),
  333.                 'status'       => $group['status'],
  334.                 'date_created' => verodate_get_random_date(30, 5),
  335.                 'enable_forum' => $group['enable_forum']
  336.             ));
  337.  
  338.             groups_update_groupmeta($cur, 'last_activity', verodate_get_random_date(10));
  339.  
  340.             // create forums if Forum Component is active
  341.             if(bp_is_active('forums') && bp_forums_is_installed_correctly()) {
  342.                 groups_new_group_forum($cur, $group['name'], $group['description']);
  343.             }
  344.  
  345.             $group_ids[] = $cur;
  346.         }
  347.  
  348.         return $group_ids;
  349.     }
  350.  
  351.  
  352.     /**
  353.      * GENERATE RANDOM DUMMY DATA
  354.      * Generating random users with some content
  355.      */
  356.     public function generate_users()
  357.     {
  358.  
  359.         for($i = 1; $i <= 25; $i++) {
  360.  
  361.             $role = get_option('default_role');
  362.  
  363.             $password = '1234566778';
  364.  
  365.             $firstname     = $this->verodate_generate_word(mt_rand(6, 12));
  366.             $lastname      = $this->verodate_generate_word(mt_rand(8, 12));
  367.             $name          = $this->verodate_generate_word(mt_rand(6, 12));
  368.             $email_address = $name . '@' . $_SERVER['SERVER_NAME'] . '.invalid';
  369.  
  370.             $name = $this->verodate_generate_word(mt_rand(6, 12));
  371.  
  372.             $website = 'http://' . $name . '.com';
  373.  
  374.             $description = $this->verodate_generate_word(mt_rand(6, 12));
  375.  
  376.             $user_id = wp_create_user($email_address, $password, $email_address);
  377.  
  378.             wp_update_user(
  379.                 array(
  380.                     'ID'          => $user_id,
  381.                     'nickname'    => $email_address,
  382.                     'first_name'  => $firstname,
  383.                     'last_name'   => $lastname,
  384.                     'user_url'    => $website,
  385.                     'description' => $description,
  386.                 )
  387.             );
  388.  
  389.             // Set the role
  390.             $user = new WP_User($user_id);
  391.  
  392.             $user->set_role($role);
  393.  
  394.             $avater = $this->verodate_random(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
  395.  
  396.             avatar_image_upload('avatars', $user_id, $avater, 'members');
  397.         }
  398.  
  399.         $this->_set_users_recently_active();
  400.  
  401.     }
  402.  
  403.  
  404.     /*
  405.      * It's for BuddyPress.
  406.      * Without recently active BuddyPress won't show users in members list.
  407.      */
  408.     private function _set_users_recently_active()
  409.     {
  410.  
  411.         global $wpdb;
  412.         foreach($wpdb->get_col("SELECT ID FROM $wpdb->users") as $user_id) {
  413.             bp_update_user_meta($user_id, 'last_activity', verodate_get_random_date(10));
  414.         }
  415.     }
  416.  
  417.  
  418.     /**
  419.      * @param $length
  420.      *
  421.      * @return bool|string
  422.      *
  423.      * Generate random word.
  424.      */
  425.     public static function verodate_generate_word($length)
  426.     {
  427.  
  428.         mt_srand((double)microtime() * 1000000);
  429.  
  430.         $vowels = array("a", "e", "i", "o", "u");
  431.         $cons   = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
  432.                         "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
  433.  
  434.         $num_vowels = count($vowels);
  435.         $num_cons   = count($cons);
  436.         $word       = '';
  437.  
  438.         while(strlen($word) < $length) {
  439.             $word .= $cons[mt_rand(0, $num_cons - 1)] . $vowels[mt_rand(0, $num_vowels - 1)];
  440.         }
  441.  
  442.         return substr($word, 0, $length);
  443.     }
  444.  
  445.  
  446.     /**
  447.      * @return string
  448.      * Generate random date
  449.      */
  450.     public static function verodate_random_date()
  451.     {
  452.  
  453.         $curr_year = date('Y');
  454.         $dob_year  = rand($curr_year - 18, $curr_year - 47);
  455.         $dob_month = rand(01, 12);
  456.         $dob_day   = rand(01, 30);
  457.         $dob_month = str_pad($dob_month, 2, '0', STR_PAD_LEFT);
  458.         $dob_day   = str_pad($dob_day, 2, '0', STR_PAD_LEFT);
  459.  
  460.         return $dob_year . '-' . $dob_month . '-' . $dob_day . ' ' . '00:00:00';
  461.     }
  462.  
  463.  
  464.     /**
  465.      * @param array $array
  466.      *
  467.      * @return mixed
  468.      */
  469.     public static function verodate_random($array = array('Male', 'Female'))
  470.     {
  471.  
  472.         $array_number = array_rand($array);
  473.         $array_value  = $array[$array_number];
  474.  
  475.         return $array_value;
  476.     }
  477.  
  478.  
  479.     /**
  480.      *
  481.      */
  482.     public function verodate_update_options()
  483.     {
  484.  
  485.         $this->_verodate_set_wordpress_settings();
  486.  
  487.         if(class_exists('BuddyPress')) {
  488.  
  489.             $this->_set_active_BuddyPress_components();
  490.         }
  491.  
  492.     }
  493.  
  494.  
  495.     /*
  496.      * Set wordpress setings.
  497.      */
  498.     private function _verodate_set_wordpress_settings()
  499.     {
  500.  
  501.         update_option('users_can_register', 1);
  502.     }
  503.     /*
  504.      * Set BuddyPress settings
  505.      */
  506.  
  507.     /* Set all BuddyPress components to active.
  508.      */
  509.     private function _set_active_BuddyPress_components()
  510.     {
  511.  
  512.         $bp = buddypress();
  513.  
  514.         $buddypress_components = [
  515.             'xprofile'      => '1',
  516.             'settings'      => '1',
  517.             'friends'       => '1',
  518.             'messages'      => '1',
  519.             'activity'      => '1',
  520.             'notifications' => '1',
  521.             'groups'        => '1',
  522.             'blogs'         => '1',
  523.             'members'       => '1'
  524.         ];
  525.  
  526.         // Save settings and upgrade schema.
  527.         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  528.         require_once($bp->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php');
  529.  
  530.         $bp->active_components = bp_core_admin_get_active_components_from_submitted_settings($buddypress_components);
  531.  
  532.         bp_core_install($bp->active_components);
  533.         bp_core_add_page_mappings($bp->active_components);
  534.         bp_update_option('bp-active-components', $bp->active_components);
  535.  
  536.         /* Set BuddyPress template to legacy template.
  537.         *  Default is : nouveau.
  538.         */
  539.         bp_update_option('_bp_theme_package_id', 'legacy');
  540.  
  541.         echo '<p>' . __('BuddyPress components activated') . '</p>';
  542.  
  543.     }
  544.  
  545.  
  546.  
  547.     /*
  548.      * INSERT DEMO MENU
  549.      *
  550.      * @since 1.0.1
  551.      */
  552.  
  553.     public function set_menu_items($menus)
  554.     {
  555.         // Check if the menu exists
  556.         $verodate_menu = 'verodate menu';
  557.  
  558.         $menu_exists = wp_get_nav_menu_object($verodate_menu);
  559.  
  560.         if(!$menu_exists) {
  561.             /* Create new menu */
  562.             $menu_id = wp_create_nav_menu($verodate_menu);
  563.  
  564.             foreach($menus as $key => $menu):
  565.  
  566.                 /* Create menu parents */
  567.  
  568.                 if(is_array($menu)):
  569.  
  570.                     $menu_item = $this->verodate_update_menu($menu_id, $menu[0], '#');
  571.  
  572.                     foreach($menu as $men):
  573.  
  574.                         /* Get page object */
  575.  
  576.                         if($men === 'Blog' || $men === 'Shop' || $men === 'Members') {
  577.  
  578.  
  579.                             $this->set_page_templates($menu_id, $menu_item, $men);
  580.  
  581.                         } else {
  582.                             $page_item = get_page_by_title($men);
  583.                             /* Create menu item  */
  584.                             $this->verodate_update_menu($menu_id, $men, $page_item->guid, $menu_item);
  585.                         }
  586.  
  587.                         /* Set different templates for these pages */
  588.                     endforeach;
  589.  
  590.                 else:
  591.  
  592.                     $page_item = get_page_by_title($menu);
  593.  
  594.                     $this->verodate_update_menu($menu_id, $page_item->post_title, $page_item->guid);
  595.  
  596.                 endif;
  597.             endforeach;
  598.  
  599.             /* set menu as primary */
  600.             $locations                 = get_theme_mod('nav_menu_locations');
  601.  
  602.             $locations['primary-menu'] = $menu_id;
  603.  
  604.             set_theme_mod('nav_menu_locations', $locations);
  605.  
  606.         }
  607.     }
  608.  
  609.     /*
  610.      * Create page templates for parent page
  611.      */
  612.     public function set_page_templates($menu_id, $menu_item, $item)
  613.     {
  614.         /* BLOG */
  615.         if($item === 'Blog') {
  616.  
  617.             /* Blog full width, left sidebar, right sidebar */
  618.             $menu_template[] = get_page_by_title($item);
  619.             /* Post full width, left sidebar, right sidebar */
  620.             $menu_template[] = get_page_by_title('Ea Immitto', OBJECT, 'post');
  621.  
  622.         } /* Shop */
  623.         elseif ($item === 'Shop') {
  624.             /* Shop full width, left sidebar , right sidebar */
  625.             $menu_template[] = get_page_by_title($item);
  626.             /* Product full width, left sidebar, right sidebar */
  627.             $menu_template[] = get_page_by_title('Woo Logo', OBJECT, 'product');
  628.  
  629.             error_log(print_r($menu_template,1));
  630.  
  631.         }
  632.  
  633.         /* Members */
  634.  
  635.         else{
  636.  
  637.             $page_members = get_page_by_title($item);
  638.  
  639.             $member_details = get_userdata(1);
  640.  
  641.             $this->verodate_update_menu($menu_id, $item, $page_members->guid , $menu_item);
  642.  
  643.             $this->verodate_update_menu($menu_id, 'Members Details', $page_members->guid .''.$member_details->user_login , $menu_item);
  644.  
  645.             return TRUE;
  646.         }
  647.  
  648.         /* Add items to menu */
  649.         foreach($menu_template as $item) {
  650.  
  651.             $template_style = [
  652.                 'Full width',
  653.                 'Right Sidebar',
  654.                 'Left Sidebar',
  655.             ];
  656.             for($i = 1; $i < 4; $i++){
  657.  
  658.                 $this->verodate_update_menu($menu_id, $item->post_title .' ' . $template_style[$i - 1], $item->guid . '?template=' . $i, $menu_item);
  659.             }
  660.         }
  661.     }
  662.     /**
  663.      * Insert item to menu.
  664.      * @param        $menu_id
  665.      * @param        $item
  666.      * @param        $url
  667.      * @param string $menu_item
  668.      *
  669.      * @return int|WP_Error
  670.      */
  671.     public function verodate_update_menu($menu_id, $item, $url, $menu_item = '')
  672.     {
  673.  
  674.         $menuID = wp_update_nav_menu_item($menu_id, 0, array(
  675.                 'menu-item-title'     => $item,
  676.                 'menu-item-url'       => $url,
  677.                 'menu-item-status'    => 'publish',
  678.                 'menu-item-parent-id' => $menu_item)
  679.         );
  680.  
  681.         return $menuID;
  682.  
  683.     }
  684.  
  685.     /* Setting up menu items
  686.      * Working only with existing pages.
  687.      * If page not exist error will be : Trying to get property of non-object.
  688.      */
  689.     public function createMenu()
  690.     {
  691.         /*
  692.         Primary VeroDate menu items
  693.         */
  694.         $menu_items = [
  695.             'front_page' => 'Front Page',
  696.             'blog'       => ['Blog'],
  697.             'shop'       => ['Shop', 'Shop Cart', 'Checkout'],
  698.             'commnity'   => ['Community', 'Members', 'Discussions', 'Forums', 'Groups'],
  699.             'pages'      => ['Stories', 'Why us', 'About us', 'Pricing', 'Search'],
  700.             'contact_us' => 'Contact us',
  701.         ];
  702.  
  703.         $this->set_menu_items($menu_items);
  704.  
  705.     }
  706.     /*
  707.      *
  708.      * Insert demo menu END
  709.      *
  710.      */
  711. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement