Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Bidoo
  4. Plugin URI: none
  5. Description: Bidoo Bid management
  6. Version: 1.0 (Cooper)
  7. Author: Alex F
  8. Author URI: alexf.com
  9. */
  10.  
  11. class aidee_bidoo {
  12.  
  13. function __construct () {
  14.  
  15. $this->self_define();
  16.  
  17. $this->load_dependencies();
  18.  
  19. $this->register_shortcodes();
  20.  
  21. $this->action_hooks(); // add action hooks.
  22.  
  23. }
  24.  
  25. function register_shortcodes () {
  26.  
  27. add_shortcode('generate_form_code', array($this, 'generate_form_code'));
  28.  
  29. add_shortcode('nohost_landing_page', array($this, 'nohost_landing_page'));
  30.  
  31. add_shortcode('account_interface', array($this, 'account_interface'));
  32.  
  33. }
  34.  
  35. function add_rewrite_rules($aRules) {
  36.  
  37. $aNewRules = array('cid/([^/]+)/?$' => 'index.php?pagename=cid&cid=$matches[1]');
  38.  
  39. $aRules = $aNewRules + $aRules;
  40.  
  41. return $aRules;
  42.  
  43. }
  44.  
  45. function add_query_vars($aVars) {
  46.  
  47. $aVars[] = "cid"; // represents the name of the product category as shown in the URL
  48. return $aVars;
  49.  
  50. }
  51.  
  52. function plugin_install () {
  53.  
  54. $db = new aidee_bidoo_db();
  55.  
  56. $db->db_install();
  57.  
  58. }
  59.  
  60. /**
  61. *
  62. * function get_single_company_by_cid
  63. *
  64. * @param $cid = company id. Stored in aidee_company_id meta_data
  65. *
  66. * Does double duty. Checks to see if Company with CID exists via post query
  67. *
  68. * Since we're already invoking a query anyways, might as well pass back the post object for iteration as well.
  69. *
  70. * @return array('has_posts'=>bool, 'post_obj'=>post_object if found, if not null.)
  71. */
  72.  
  73. function get_single_company_by_cid($cid) {
  74.  
  75. $meta_query = array();
  76.  
  77. $meta_query[] = array(
  78. 'key' => 'aidee_company_id',
  79. 'value' => $cid,
  80. 'compare' => '=',
  81. );
  82.  
  83. $posts = new wp_query(array(
  84. 'posts_per_page' => 1,
  85. 'post_type' => 'company',
  86. 'post_status'=>'any',
  87. 'meta_key'=>'aidee_company_id',
  88. 'meta_query'=>$meta_query,
  89. 'orderby' => 'ASC'
  90. ));
  91.  
  92. if($posts->have_posts()){
  93.  
  94. return array('has_posts'=>true, 'post_obj'=>$posts);
  95. }
  96.  
  97. return array('has_posts'=>false, 'post_obj'=>null);
  98.  
  99. }
  100. function register_post_types () {
  101.  
  102. require_once($this->plugin_base_dir.'/classes/register_post_types.php');
  103.  
  104. new aidee_register_cpt('Company', 'Companies');
  105.  
  106. }
  107.  
  108. function action_hooks () {
  109.  
  110. add_action('wp_ajax_bid_board', array($this, 'get_bid_board'));
  111.  
  112. add_action( 'init', array($this, 'register_post_types'));
  113.  
  114. add_filter('query_vars', array($this, 'add_query_vars')); // hooks in and adds cid to query var for grabbing for form.
  115.  
  116. add_filter('rewrite_rules_array', array($this, 'add_rewrite_rules')); // hooks in and adds cid to query var for grabbing for form.
  117.  
  118. register_activation_hook( __FILE__, array( $this, 'plugin_install' ) );
  119.  
  120. if(is_admin()) {
  121.  
  122. add_action('admin_enqueue_scripts', array($this, 'register_scripts_back' ));
  123.  
  124. add_action('admin_enqueue_scripts', array($this, 'register_styles_back' ));
  125.  
  126. }
  127.  
  128. if(!is_admin()) {
  129.  
  130. add_action('wp_enqueue_scripts', array($this, 'register_styles_front'));
  131.  
  132. add_action('wp_enqueue_scripts', array($this, 'register_scripts_front'));
  133.  
  134. }
  135.  
  136. }
  137.  
  138.  
  139. function build_form ($cid='', $no_host=false) {
  140.  
  141. $site_url = get_site_url();
  142.  
  143. $form.="<form action='{$site_url}' id='bidoo_bidding_form' method='post'>";
  144.  
  145. $form.="<input type='hidden' id='' name='cid' value='{$cid}'>";
  146.  
  147. $form.="<div id='load_number'>";
  148.  
  149. $form.="<input required type='text' name='load_number' placeholder='Load Number'>";
  150.  
  151. $form.="</div>";
  152.  
  153. $form.="<div id='company_name'>";
  154.  
  155. $form.="<input required type='text' name='company_name' placeholder='Company Name'>";
  156.  
  157. $form.="</div>";
  158.  
  159. $form.="<div id='company_base_city'>";
  160.  
  161. $form.="<input required type='text' name='company_base_city' placeholder='City Company is located in'>";
  162.  
  163. $form.="</div>";
  164.  
  165. $form.="<div id='load_date'>";
  166.  
  167. $form.="<input required type='text' class='datepicker' data-select='datepicker' name='load_date' placeholder='Load Date'>";
  168.  
  169. $form.="</div>";
  170.  
  171. $form.="<div id='delivery_date'>";
  172.  
  173. $form.="<input required type='text' data-select='datepicker' name='delivery_date' placeholder='Delivery Date'>";
  174.  
  175. $form.="</div>";
  176.  
  177. $form.="<div id='contact_first_name'>";
  178.  
  179. $form.="<input required type='text' name='contact_first_name' placeholder='Contact person (First Name)'>";
  180.  
  181. $form.="</div>";
  182.  
  183. $form.="<div id='contact_last_name'>";
  184.  
  185. $form.="<input required type='text' name='contact_last_name' placeholder='Contact person (Last Name)'>";
  186.  
  187. $form.="</div>";
  188.  
  189. $form.="<div id='company_contact_number'>";
  190.  
  191. $form.="<input required type='text' name='company_contact_number' placeholder='Callback Phone Number'>";
  192.  
  193. $form.="</div>";
  194.  
  195. $form.="<div id='company_contact_email'>";
  196.  
  197. $form.="<input required type='text' name='company_contact_email' placeholder='Contact Email'>";
  198.  
  199. $form.="</div>";
  200.  
  201. $form.="<div id='company_contact_number'>";
  202.  
  203. $form.="<textarea name='comments' id='comments' placeholder='Questions/Comments'></textarea>";
  204.  
  205. $form.="</div>";
  206.  
  207. $form.="<input type='submit' name='submit'>";
  208.  
  209. $form.="</form>";
  210.  
  211. return $form;
  212.  
  213. }
  214.  
  215. /**
  216. *
  217. * function generate_form_code
  218. *
  219. * @param null
  220. *
  221. * used to generate embed code.
  222. *
  223. */
  224. function generate_form_code() {
  225.  
  226. $customer_id = 'generic_customer_id';
  227.  
  228. $string.="<div id='bidoo_form_wrapper' data-title='{$customer_id}'></div>";
  229. $string.="<script type='text/javascript'>";
  230.  
  231. $string.="var script = document.createElement('script');";
  232. $string.= "script.src = '{$this->plugin_base_url}assets/js/payload.min.js';";
  233. $string.="var head = document.getElementsByTagName('head')[0];";
  234. $string.="script.type = 'text/javascript';";
  235. $string.="head.appendChild(script);";
  236. $string.="</script>";
  237.  
  238. return apply_filters('the_content', "[sourcecode language='plain']{$string}[/sourcecode]");
  239.  
  240. }
  241.  
  242. function load_dependencies() {
  243.  
  244. require_once($this->plugin_base_dir.'/classes/filters.php');
  245.  
  246. $this->filters = new aidee_bidoo_filters();
  247.  
  248. require_once($this->plugin_base_dir.'/classes/api.php');
  249.  
  250. require_once($this->plugin_base_dir.'/classes/aidee_db.php');
  251.  
  252.  
  253. }
  254.  
  255. function self_define () {
  256.  
  257. $this->plugin_base_dir = plugin_dir_path(__FILE__);
  258.  
  259. $this->plugin_base_url = plugin_dir_url(__FILE__);
  260.  
  261. $this->parent_theme_dir = get_template_directory();
  262.  
  263. $this->parent_theme_url = get_template_directory_uri();
  264.  
  265. $this->child_theme_dir = get_stylesheet_directory();
  266.  
  267. $this->child_theme_url = get_stylesheet_directory_uri();
  268.  
  269. }
  270.  
  271. function register_scripts_back () {
  272.  
  273. }
  274.  
  275. function register_styles_back () {
  276.  
  277. }
  278.  
  279. function register_scripts_front () {
  280.  
  281. wp_register_script('bidoo_local_form', $this->plugin_base_url. '/assets/js/form.js', null, null, true);
  282.  
  283. wp_register_script('bidoo_form_base', $this->plugin_base_url. '/assets/js/form-base.js', null, null, true);
  284.  
  285. wp_register_script('bidoo_tabify', $this->plugin_base_url. '/assets/js/tabify.js', null, null, true);
  286.  
  287. wp_register_script('bidoo_bid_board', $this->plugin_base_url. '/assets/js/bid_board.js', null, null, true);
  288.  
  289.  
  290. }
  291.  
  292. function register_styles_front () {
  293.  
  294. wp_register_style('bidoo_css', $this->plugin_base_url . '/assets/css/bidoo.css');
  295.  
  296. }
  297.  
  298. /**
  299. *
  300. * function nohost_landing_page
  301. *
  302. * This builds content for the companies that do not self host.
  303. *
  304. * We use the same method for populating the form remotely, as we do here. We add a simple <div> to the markup, and then call our script
  305. * which makes the same ajax cb, loads the form and calls everything else. Voila. We do this for the sake of consistency.
  306. *
  307. * we also load a template for displaying company logo, title etc. The company ID will be grabbed from query param
  308. * cid eg: ?cid=12345. Of course. validation is done before hand to ensure the company id is valid.
  309. *
  310. *
  311. */
  312. function nohost_landing_page() {
  313.  
  314. wp_enqueue_script('bidoo_local_form');
  315.  
  316. global $wp_query;
  317.  
  318. $cid = $this->filters->filter_customer_id($wp_query->query_vars['cid']);
  319.  
  320. $has_company = $this->get_single_company_by_cid($cid);
  321.  
  322. if($has_company['has_posts']) {
  323.  
  324. $content.="<div id='bidoo_form_wrapper' data-title='{$cid}'></div>";
  325.  
  326. return $content;
  327.  
  328. } else {
  329.  
  330. return "Sorry, we could not find that company.";
  331.  
  332. }
  333.  
  334.  
  335.  
  336. }
  337.  
  338. function get_bid_board() {
  339.  
  340. $db = new aidee_bidoo_db();
  341.  
  342. require_once($this->plugin_base_dir.'/templates/account/bid_board.php');
  343.  
  344. wp_send_json( array('error'=>false, 'results'=>$layout));
  345.  
  346. die;
  347.  
  348. }
  349.  
  350. function account_interface () {
  351.  
  352. $db = new aidee_bidoo_db();
  353.  
  354. wp_enqueue_style('bidoo_css');
  355.  
  356. wp_enqueue_script('bidoo_tabify');
  357.  
  358. wp_enqueue_script('bidoo_form_base');
  359.  
  360. wp_enqueue_script('bidoo_bid_board');
  361.  
  362. wp_localize_script('bidoo_bid_board', 'core_obj', array('ajax_url'=>admin_url('admin-ajax.php')));
  363.  
  364. require_once($this->plugin_base_dir.'/templates/account/head.php');
  365.  
  366. require_once($this->plugin_base_dir.'/templates/account/bid_board.php');
  367.  
  368. return $layout;
  369.  
  370. }
  371. }
  372.  
  373. new aidee_bidoo();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement