Advertisement
dominicanster

404-ajax-post-plugin-tutorial

Jan 8th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.02 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: Solana Jewelry Email Subscription
  4. * Plugin URI: http://www.woothemes.com/woocommerce/
  5. * Description: An email subscription toolkit that helps users subscribe to storefront.
  6. * Version: 1.0
  7. * Author: Allen Rodriguez
  8. * Author URI: http://psieinteractive.com
  9. * Requires at least: 4.1
  10. * Tested up to: 4.4
  11. * text-domain: solana-jewelry-email-subscribers
  12. * */
  13.  
  14.  
  15. /* !0. TABLE OF CONTENTS */
  16.  
  17. /*
  18.  
  19. 1. HOOKS
  20.  
  21. 2. SHORTCODES
  22.  
  23. 3. FILTERS
  24.  
  25. 4. EXTERNAL SCRIPTS
  26.  
  27. 5. ACTIONS
  28.  
  29. 6. HELPERS
  30.  
  31. 7. CUSTOM POST TYPES
  32.  
  33. 8. ADMIN PAGES
  34.  
  35. 9. SETTINGS
  36.  
  37. 10. MISCELLANEOUS
  38.  
  39. */
  40.  
  41.  
  42. /* !1. HOOKS */
  43. add_action('init', 'solj_register_shortcodes');
  44.  
  45. //register custom admin column headers
  46. add_filter('manage_edit-solj_subscriber_columns', 'solj_subscriber_column_headers');
  47.  
  48. //register custom admin column data
  49. add_filter('manage_edit-solj_subscriber_posts_custom_columns', 'solj_subscriber_column_data',1,2);
  50.  
  51. //register custom admin list olumn headers
  52. add_filter('manage_edit-solj_list_columns', 'solj_list_column_headers');
  53.  
  54. //register custom admin list column data
  55. add_filter('manage_edit-solj_list_posts_custom_columns', 'solj_list_column_data',1,2);
  56.  
  57. // 1.4
  58. // hint: register ajax actions
  59. add_action('wp_ajax_nopriv_solj_save_subscription', 'solj_save_subscription'); // regular website visitor
  60. add_action('wp_ajax_solj_save_subscription', 'solj_save_subscription'); // admin user
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. /* !2. SHORTCODES */
  69.  
  70. // registers custom shortcode
  71. function solj_register_shortcodes() {
  72.  
  73. add_shortcode('solj-subscribe-form', 'solj_form_shortcode');
  74.  
  75. }
  76.  
  77. function solj_form_shortcode ($args, $content="") {
  78.  
  79. // get the list id
  80. $list_id = 0;
  81. if( isset($args['id'])) $list_id = (int)$args['id'];
  82.  
  83. // setup output variable - the form html
  84. $output = '
  85. <div class="solj">
  86. <form id="solj_form" name="solj_form" action="/wp-admin/admin-ajax.php?action=solj_save_subscription" method="post" >
  87.  
  88. <input type="hidden" name="solj_list" value = "' . $list_id .'" >
  89.  
  90. <p class="solj-input-container" >
  91.  
  92. <label>your name</label><br />
  93. <input type="text" name="solj_fname" placeholder="first name" />
  94. <input type="text" name="solj_lname" placeholder="last name" />
  95.  
  96. </p>
  97.  
  98. <p class="solj-input-container" >
  99.  
  100. <label>email</label><br />
  101. <input type="email" name="solj_email" placeholder="ex. you@email.com" />
  102.  
  103. </p>';
  104.  
  105. if( strlen($content) ):
  106.  
  107. $output .= '<div class="solj_content">' . wpautop($content) . '</div>';
  108.  
  109. endif;
  110.  
  111. //completing form html
  112. $output .= '<p class="solj-input-container" >
  113.  
  114. <input type="submit" name="solj_submit" value="Sign Me Up!" />
  115.  
  116. </p>
  117.  
  118. </form>
  119. </div>
  120.  
  121.  
  122. ';
  123.  
  124. // return our result/html
  125. return $output;
  126.  
  127.  
  128. }
  129.  
  130. /* !3. FILTERS */
  131.  
  132. // 3.6
  133. function solj_subscriber_column_headers ( $columns ) {
  134.  
  135. //creating custom column header data
  136. $columns = array (
  137.  
  138. 'cb'=>'<input type="checkbox" />',
  139. 'title'=>__('Subscriber Name'),
  140. 'email'=>__('Email Address'),
  141.  
  142. );
  143.  
  144. //returning new columns
  145. return $columns;
  146.  
  147. }
  148.  
  149.  
  150. // 3.2
  151. function solj_subscriber_column_data( $column, $post_id ) {
  152.  
  153.  
  154.  
  155.  
  156. // setup our return text
  157. $output = '';
  158.  
  159. switch( $column ) {
  160.  
  161.  
  162. case 'title':
  163. // get the custom name data
  164. $fname = get_field('solj_fname', $post_id );
  165. $lname = get_field('solj_lname', $post_id );
  166. $output .= $fname .' '. $lname;
  167.  
  168. break;
  169. case 'email':
  170. // get the custom email data
  171. $email = get_field('solj_email', $post_id );
  172. $output .= $email;
  173.  
  174. break;
  175.  
  176. }
  177.  
  178. // echo the output
  179. echo $output;
  180.  
  181. }
  182.  
  183.  
  184. // 3.3
  185. function solj_list_column_headers ( $columns ) {
  186.  
  187. //creating custom column header data
  188. $columns = array (
  189.  
  190. 'cb'=>'<input type="checkbox" />',
  191. 'title'=>__('List Name'),
  192. 'shortcode'=>__('Shortcode'),
  193.  
  194.  
  195. );
  196.  
  197. //returning new columns
  198. return $columns;
  199.  
  200. }
  201.  
  202. //3.4
  203.  
  204. function solj_list_column_data( $column, $post_id ) {
  205.  
  206. // setup our return text
  207. $output = '';
  208.  
  209. switch( $column ) {
  210.  
  211. case 'shortcode':
  212. $output .= '[solj_form id="'. $post_id .'"]';
  213. break;
  214.  
  215. }
  216.  
  217. // echo the output
  218. echo $output;
  219.  
  220. }
  221.  
  222.  
  223.  
  224. /* !4. EXTERNAL SCRIPTS */
  225.  
  226.  
  227.  
  228.  
  229. /* !5. ACTIONS */
  230.  
  231. //save subscription data to existing or new subscriber
  232. function solj_save_subscription() {
  233.  
  234. //setup default result data
  235. $result = array (
  236. 'status' => 0,
  237. 'message'=> 'Subscription was not saved. ',
  238.  
  239. );
  240.  
  241. // array fo storing errors
  242. $errors = array();
  243.  
  244. try {
  245.  
  246. // get the list id
  247. $list_id = (int)$_POST['solj_list'];
  248.  
  249. // prepare subscriber data
  250. $subscriber_data = array (
  251. 'fname' => esc_attr( $_POST[ 'solj_fname' ] ),
  252. 'lname' => esc_attr( $_POST[ 'solj_lname' ] ),
  253. 'email' => esc_attr( $_POST[ 'solj_email' ] ),
  254.  
  255. );
  256.  
  257. // attempt to create/save object
  258. $subscriber_id = solj_save_subscription($subscriber_data);
  259.  
  260. // IF subscriber was saved successfully $subscriber_id will be greater than 0
  261. if ($subscriber_id) :
  262.  
  263. // IF subscriber already has this subscription
  264. if (solj_subscriber_has_subscription( $subscriber_id, $list_id)) :
  265.  
  266. //get list object
  267. $list = get_post( $list_id );
  268.  
  269. //return detailed error
  270. $resut['message'] .= esc_attr( $subscriber_data['email'] . ' is already subscribed to ' . $list>post_title . '.');
  271.  
  272. else:
  273.  
  274. //save new subscription
  275. $subscription_saved = solj_add_subscription( $subscriber_id, $list_id );
  276.  
  277. // IF subscription was saved successfully
  278. if ( $subscription_saved) :
  279.  
  280. // subscription saved
  281. $result['status']=1;
  282. $result['message']='subscription saved';
  283.  
  284. endif;
  285.  
  286. endif;
  287.  
  288. endif;
  289.  
  290.  
  291. } catch ( Exception $e) {
  292.  
  293. // a php error occurred
  294. $result['error']= 'Caught exception' . $e->getMessage();
  295.  
  296. }
  297.  
  298. // return result as json
  299. solj_return_json($result);
  300.  
  301.  
  302.  
  303. }
  304.  
  305. // 5.2
  306. // hint: creates a new subscriber or updates and existing one
  307. function solj_save_subscriber( $subscriber_data ) {
  308.  
  309. // setup default subscriber id
  310. // 0 means the subscriber was not saved
  311. $subscriber_id = 0;
  312.  
  313. try {
  314.  
  315. $subscriber_id = solj_get_subscriber_id( $subscriber_data['email'] );
  316.  
  317. // IF the subscriber does not already exists...
  318. if( !$subscriber_id ):
  319.  
  320. // add new subscriber to database
  321. $subscriber_id = wp_insert_post(
  322. array(
  323. 'post_type'=>'solj_subscriber',
  324. 'post_title'=>$subscriber_data['fname'] .' '. $subscriber_data['lname'],
  325. 'post_status'=>'publish',
  326. ),
  327. true
  328. );
  329.  
  330. endif;
  331.  
  332. // add/update custom meta data
  333. update_field(solj_get_acf_key('solj_fname'), $subscriber_data['fname'], $subscriber_id);
  334. update_field(solj_get_acf_key('solj_lname'), $subscriber_data['lname'], $subscriber_id);
  335. update_field(solj_get_acf_key('solj_email'), $subscriber_data['email'], $subscriber_id);
  336.  
  337. } catch( Exception $e ) {
  338.  
  339. // a php error occurred
  340.  
  341. }
  342.  
  343. // return subscriber_id
  344. return $subscriber_id;
  345.  
  346. }
  347.  
  348. // 5.3
  349. // hint: adds list to subscribers subscriptions
  350. function solj_add_subscription( $subscriber_id, $list_id ) {
  351.  
  352. // setup default return value
  353. $subscription_saved = false;
  354.  
  355. // IF the subscriber does NOT have the current list subscription
  356. if( !solj_subscriber_has_subscription( $subscriber_id, $list_id ) ):
  357.  
  358. // get subscriptions and append new $list_id
  359. $subscriptions = solj_get_subscriptions( $subscriber_id );
  360. $subscriptions[]=$list_id;
  361.  
  362. // update solj_subscriptions
  363. update_field( solj_get_acf_key('solj_subscriptions'), $subscriptions, $subscriber_id );
  364.  
  365. // subscriptions updated!
  366. $subscription_saved = true;
  367.  
  368. endif;
  369.  
  370. // return result
  371. return $subscription_saved;
  372.  
  373. }
  374.  
  375.  
  376.  
  377.  
  378.  
  379. /* !6. HELPERS */
  380.  
  381. // 6.1
  382. // hint: returns true or false
  383. function solj_subscriber_has_subscription( $subscriber_id, $list_id ) {
  384.  
  385. // setup default return value
  386. $has_subscription = false;
  387.  
  388. // get subscriber
  389. $subscriber = get_post($subscriber_id);
  390.  
  391. // get subscriptions
  392. $subscriptions = solj_get_subscriptions( $subscriber_id );
  393.  
  394. // check subscriptions for $list_id
  395. if( in_array($list_id, $subscriptions) ):
  396.  
  397. // found the $list_id in $subscriptions
  398. // this subscriber is already subscribed to this list
  399. $has_subscription = true;
  400.  
  401. else:
  402.  
  403. // did not find $list_id in $subscriptions
  404. // this subscriber is not yet subscribed to this list
  405.  
  406. endif;
  407.  
  408. return $has_subscription;
  409.  
  410. }
  411.  
  412. // 6.2
  413. // hint: retrieves a subscriber_id from an email address
  414. function solj_get_subscriber_id( $email ) {
  415.  
  416. $subscriber_id = 0;
  417.  
  418. try {
  419.  
  420. // check if subscriber already exists
  421. $subscriber_query = new WP_Query(
  422. array(
  423. 'post_type' => 'solj_subscriber',
  424. 'posts_per_page' => 1,
  425. 'meta_key' => 'solj_email',
  426. 'meta_query' => array(
  427. array(
  428. 'key' => 'solj_email',
  429. 'value' => $email, // or whatever it is you're using here
  430. 'compare' => '=',
  431. ),
  432. ),
  433. )
  434. );
  435.  
  436. // IF the subscriber exists...
  437. if( $subscriber_query->have_posts() ):
  438.  
  439. // get the subscriber_id
  440. $subscriber_query->the_post();
  441. $subscriber_id = get_the_ID();
  442.  
  443. endif;
  444.  
  445. } catch( Exception $e ) {
  446.  
  447. // a php error occurred
  448.  
  449. }
  450.  
  451. // reset the Wordpress post object
  452. wp_reset_query();
  453.  
  454. return (int)$subscriber_id;
  455.  
  456. }
  457.  
  458. // 6.3
  459. // hint: returns an array of list_id's
  460. function solj_get_subscriptions( $subscriber_id ) {
  461.  
  462. $subscriptions = array();
  463.  
  464. // get subscriptions (returns array of list objects)
  465. $lists = get_field( solj_get_acf_key('solj_subscriptions'), $subscriber_id );
  466.  
  467. // IF $lists returns something
  468. if( $lists ):
  469.  
  470. // IF $lists is an array and there is one or more items
  471. if( is_array($lists) && count($lists) ):
  472. // build subscriptions: array of list id's
  473. foreach( $lists as &$list):
  474. $subscriptions[]= (int)$list->ID;
  475. endforeach;
  476. elseif( is_numeric($lists) ):
  477. // single result returned
  478. $subscriptions[]= $lists;
  479. endif;
  480.  
  481. endif;
  482.  
  483. return (array)$subscriptions;
  484.  
  485. }
  486.  
  487. // 6.4
  488. function solj_return_json( $php_array ) {
  489.  
  490. // encode result as json string
  491. $json_result = json_encode( $php_array );
  492.  
  493. // return result
  494. die( $json_result );
  495.  
  496. // stop all other processing
  497. exit;
  498.  
  499. }
  500.  
  501.  
  502. //6.5
  503. // hint: gets the unique act field key from the field name
  504. function solj_get_acf_key( $field_name ) {
  505.  
  506. $field_key = $field_name;
  507.  
  508. switch( $field_name ) {
  509.  
  510. case 'solj_fname':
  511. $field_key = 'field_568f22de20e31';
  512. break;
  513. case 'solj_lname':
  514. $field_key = 'field_568f232520e32';
  515. break;
  516. case 'solj_email':
  517. $field_key = 'field_568f233920e33';
  518. break;
  519. case 'solj_subscriptions':
  520. $field_key = 'field_568f235b20e34';
  521. break;
  522.  
  523. }
  524.  
  525. return $field_key;
  526.  
  527. }
  528.  
  529.  
  530. // 6.6
  531. // hint: returns an array of subscriber data including subscriptions
  532. function solj_get_subscriber_data( $subscriber_id ) {
  533.  
  534. // setup subscriber_data
  535. $subscriber_data = array();
  536.  
  537. // get subscriber object
  538. $subscriber = get_post( $subscriber_id );
  539.  
  540. // IF subscriber object is valid
  541. if( isset($subscriber->post_type) && $subscriber->post_type == 'solj_subscriber' ):
  542.  
  543. $fname = get_field( solj_get_acf_key('solj_fname'), $subscriber_id);
  544. $lname = get_field( solj_get_acf_key('solj_lname'), $subscriber_id);
  545.  
  546. // build subscriber_data for return
  547. $subscriber_data = array(
  548. 'name'=> $fname .' '. $lname,
  549. 'fname'=>$fname,
  550. 'lname'=>$lname,
  551. 'email'=>get_field( solj_get_acf_key('solj_email'), $subscriber_id),
  552. 'subscriptions'=>solj_get_subscriptions( $subscriber_id )
  553. );
  554.  
  555.  
  556. endif;
  557.  
  558. // return subscriber_data
  559. return $subscriber_data;
  560.  
  561. }
  562.  
  563.  
  564.  
  565.  
  566.  
  567. /* !7. CUSTOM POST TYPES */
  568.  
  569.  
  570.  
  571.  
  572. /* !8. ADMIN PAGES */
  573.  
  574.  
  575.  
  576.  
  577. /* !9. SETTINGS */
  578.  
  579.  
  580.  
  581.  
  582. /* !10. MISCELLANEOUS */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement