GFPaystationFeedAdmin

The Gravity Forms Paystation (3 party hosted) GFPaystationFeedAdmin class.

Defined (1)

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

/class.GFPaystationFeedAdmin.php  
  1. class GFPaystationFeedAdmin { 
  2.  
  3. protected $plugin; 
  4.  
  5. // ==================================================================================================================================== 
  6. /** 
  7. * @param GFPaystationPlugin $plugin handle to the plugin object 
  8. */ 
  9. // ==================================================================================================================================== 
  10. public function __construct($plugin) { 
  11.  
  12. $this->plugin = $plugin; 
  13.  
  14. // Add filters and actions to hook in to admin things 
  15. add_filter('wp_print_scripts', array($this, 'removeScripts')); 
  16. add_filter('parent_file', array($this, 'filterParentFile')); 
  17. add_action('add_meta_boxes_'.GFPAYSTATION_TYPE_FEED, array($this, 'actionAddMetaBoxes')); 
  18. add_action('save_post', array($this, 'saveCustomFields'), 10, 2); 
  19. add_filter('manage_'.GFPAYSTATION_TYPE_FEED.'_posts_columns', array($this, 'filterManageColumns')); 
  20. add_filter('post_row_actions', array($this, 'filterPostRowActions')); 
  21. add_filter('wp_insert_post_data', array($this, 'filterInsertPostData'), 10, 2); 
  22. add_filter('post_updated_messages', array($this, 'filterPostUpdatedMessages')); 
  23.  
  24. // Add the feed admin javscript file to the list of files to be included in the page source. 
  25. wp_enqueue_script('gfpaystation-feed-admin', $this->plugin->urlBase . 'js/feed-admin.js', array('jquery'), GFPAYSTATION_PLUGIN_VERSION, true); 
  26.  
  27. // ==================================================================================================================================== 
  28. /** 
  29. * remove some scripts we don't want loaded 
  30. */ 
  31. // ==================================================================================================================================== 
  32. public function removeScripts() { 
  33. // stop WordPress SEO breaking our tooltips! 
  34. wp_dequeue_script('wp-seo-metabox'); 
  35. wp_dequeue_script('jquery-qtip'); 
  36.  
  37. // ==================================================================================================================================== 
  38. /** 
  39. * Tell WordPress admin that Gravity Forms menu is parent page. 
  40. * @param string $parent_file 
  41. * @return string 
  42. */ 
  43. // ==================================================================================================================================== 
  44. public function filterParentFile($parent_file) { 
  45. global $submenu_file; 
  46.  
  47. // Set parent menu for filter return. 
  48. $parent_file = 'gf_edit_forms'; 
  49.  
  50. // Set submenu by side effect. 
  51. $submenu_file = 'gfpaystation-options'; 
  52.  
  53. return $parent_file; 
  54.  
  55. // ==================================================================================================================================== 
  56. /** 
  57. * Add meta boxes (these are the boxes seen on the mapping screen) for custom fields. 
  58. * @param WP_Post $post 
  59. */ 
  60. // ==================================================================================================================================== 
  61. public function actionAddMetaBoxes($post) { 
  62.  
  63. try { 
  64. $feed = new GFPaystationFeed(); 
  65. if ($post && $post->ID) { 
  66. $feed->loadFromPost($post); 
  67. catch (GFPaystationException $e) { 
  68. // NOP -- we'll have an empty feed 
  69.  
  70. // The 'metaboxForm', 'metaboxFields', etc are function names below which output the HTML for these boxes. 
  71. add_meta_box('meta_'.GFPAYSTATION_TYPE_FEED.'_form', 'Gravity Form', array($this, 'metaboxForm'),  
  72. GFPAYSTATION_TYPE_FEED, 'normal', 'high', array('feed' => $feed)); 
  73.  
  74. add_meta_box('meta_'.GFPAYSTATION_TYPE_FEED.'_fields', 'Form to Transaction Parameter Mapping', array($this, 'metaboxFields'),  
  75. GFPAYSTATION_TYPE_FEED, 'normal', 'high', array('feed' => $feed)); 
  76.  
  77. add_meta_box('meta_'.GFPAYSTATION_TYPE_FEED.'_urls', 'Redirect URLs', array($this, 'metaboxURLs'),  
  78. GFPAYSTATION_TYPE_FEED, 'normal', 'high', array('feed' => $feed)); 
  79.  
  80. add_meta_box('meta_'.GFPAYSTATION_TYPE_FEED.'_opts', 'Notification Settings', array($this, 'metaboxOpts'),  
  81. GFPAYSTATION_TYPE_FEED, 'normal', 'high', array('feed' => $feed)); 
  82.  
  83. // Replace standard Publish box with a custom one 
  84. remove_meta_box('submitdiv', GFPAYSTATION_TYPE_FEED, 'side'); 
  85.  
  86. add_meta_box('meta_'.GFPAYSTATION_TYPE_FEED.'_submit', 'Options', array($this, 'metaboxSave'),  
  87. GFPAYSTATION_TYPE_FEED, 'side', 'high', array('feed' => $feed)); 
  88.  
  89. // ==================================================================================================================================== 
  90. /** 
  91. * metabox for custom save/publish 
  92. * @param WP_Post $post 
  93. * @param array $metabox has metabox id, title, callback, and args elements. 
  94. */ 
  95. // ==================================================================================================================================== 
  96. public function metaboxSave($post, $metabox) { 
  97. global $action; 
  98. ?> 
  99. <div style="display:none;"> 
  100. <?php submit_button( __( 'Save' ), 'button', 'save' ); ?> 
  101. </div> 
  102. <div id="major-publishing-actions"> 
  103. <?php do_action('post_submitbox_start'); ?> 
  104. <div id="delete-action"> 
  105. <?php 
  106. if ( current_user_can( "delete_post", $post->ID ) ) { 
  107. if ( !EMPTY_TRASH_DAYS ) 
  108. $delete_text = __('Delete Permanently'); 
  109. else 
  110. $delete_text = __('Move to Trash'); 
  111. ?> 
  112. <a class="submitdelete deletion" href="<?php echo get_delete_post_link($post->ID); ?>"><?php echo $delete_text; ?></a><?php 
  113. } ?> 
  114. </div> 
  115. <div id="publishing-action"> 
  116. <span class="spinner"></span> 
  117. <input name="original_publish" type="hidden" id="original_publish" value="Save" /> 
  118. <?php submit_button('Save', 'primary button-large', 'publish', false, array() ); ?> 
  119. </div> 
  120. <div class="clear"></div> 
  121. <?php 
  122. $feedsURL = 'edit.php?post_type=' . GFPAYSTATION_TYPE_FEED; 
  123. echo "<a href=\"$feedsURL\">Return to feed list</a>.\n"; 
  124. ?> 
  125. </div> 
  126. <?php 
  127.  
  128. // ==================================================================================================================================== 
  129. /** 
  130. * Metabox for Gravity Form field, only listing forms that don't have a feed or are current feed's form (i.e. the select form feld). 
  131. * @param WP_Post $post 
  132. * @param array $metabox has metabox id, title, callback, and args elements. 
  133. */ 
  134. // ==================================================================================================================================== 
  135. public function metaboxForm($post, $metabox) { 
  136.  
  137. $feed = $metabox['args']['feed']; 
  138. $forms = GFFormsModel::get_forms(); 
  139.  
  140. $feeds = GFPaystationFeed::getList(); 
  141. $feedMap = array(); 
  142. foreach ($feeds as $f) { 
  143. $feedMap[$f->FormID] = 1; 
  144.  
  145. ?> 
  146. <select size="1" name="_gfpaystation_form"> 
  147. <option value="">(Select form)</option> 
  148. <?php 
  149. foreach ($forms as $form) { 
  150. // only if form for this feed, or without a feed 
  151. if ($form->id == $feed->FormID || !isset($feedMap[$form->id])) { 
  152. $selected = selected($feed->FormID, $form->id); 
  153. echo "<option value='{$form->id}' $selected>", htmlspecialchars($form->title), "</option>\n"; 
  154. ?> 
  155. </select> 
  156. <?php 
  157.  
  158. // ==================================================================================================================================== 
  159. /** 
  160. * Metabox for Redirect URL on failure - only really should be set when the Gravity form is set up to redirect to another page on 
  161. * success rather than re-display and output the confirmation message (or error message if there was an error). 
  162. * @param WP_Post $post 
  163. * @param array $metabox has metabox id, title, callback, and args elements. 
  164. */ 
  165. // ==================================================================================================================================== 
  166. public function metaboxURLs($post, $metabox) { 
  167. $feed = $metabox['args']['feed']; 
  168. $UrlFail = htmlspecialchars($feed->UrlFail); 
  169.  
  170. ?> 
  171. <strong>Notes:</strong>  
  172. <p> 
  173. If you have set the Gravity Form up to display the confirmation message after submission (this is the default) then you probably  
  174. don't need to specify a failure url here. This is because the error message from Paystation will be displayed instead of the confirmation  
  175. message if the payment failed. This also means the form confirmation message you type should say what you want the end-user to see when the payment was successful. 
  176. </p> 
  177. <p> 
  178. However, if you have specified on the Gravity Form that it is to redirect somewhere after submission (for example a "thank you" page) then it is 
  179. a good idea that you specify a failure url here (for example a "sorry payment failed" page) because this plugin cannot change the text displayed  
  180. on whatever page you redirect to in order to inform the user of success or failure. 
  181. </p> 
  182. <p><label>URL to redirect to on transaction failure (optional):</label><br /> 
  183. <input type="url" class='large-text' name="_gfpaystation_url_fail" value="<?php echo $UrlFail; ?>" /> 
  184. </p> 
  185. <?php 
  186.  
  187. // ==================================================================================================================================== 
  188. /** 
  189. * Metabox for options related to notification settings. 
  190. * @param WP_Post $post 
  191. * @param array $metabox has metabox id, title, callback, and args elements. 
  192. */ 
  193. // ==================================================================================================================================== 
  194. public function metaboxOpts($post, $metabox) { 
  195. $feed = $metabox['args']['feed']; 
  196. ?> 
  197. <strong>Notes:</strong>  
  198. <p> 
  199. Tick these to only get an email notification when the payment was successful. If you leave these un-ticked you will get a notification 
  200. as soon as the end-user has submitted the form, which means before the payment step. You may want a notification before the payment has 
  201. been made, but remember to check in the form Entries, or in your Paystation online admin, that the payment was successful before shipping any goods. 
  202. </p> 
  203. <p><label><input type="checkbox" name="_gfpaystation_delay_notify" value="1" <?php checked($feed->DelayNotify); ?> /> 
  204. Send admin notification only when payment is received.</label></p> 
  205. <p><label><input type="checkbox" name="_gfpaystation_delay_autorespond" value="1" <?php checked($feed->DelayAutorespond); ?> /> 
  206. Send user notification only when payment is received.</label></p> 
  207. <p><label><input type="checkbox" name="_gfpaystation_delay_post" value="1" <?php checked($feed->DelayPost); ?> /> 
  208. Create post only when payment is received.</label></p> 
  209. <?php 
  210.  
  211. // ==================================================================================================================================== 
  212. /** 
  213. * Metabox for Fields to Map to the parameters sent in the payment request. 
  214. * @param WP_Post $post 
  215. * @param array $metabox has metabox id, title, callback, and args elements. 
  216. */ 
  217. // ==================================================================================================================================== 
  218. public function metaboxFields($post, $metabox) { 
  219. wp_nonce_field(GFPAYSTATION_TYPE_FEED.'_save', GFPAYSTATION_TYPE_FEED.'_wpnonce', false, true); 
  220.  
  221. $feed = $metabox['args']['feed']; 
  222. $MerchantReference = htmlspecialchars($feed->MerchantReference); 
  223. $CustomerDetails = htmlspecialchars($feed->CustomerDetails); 
  224. $OrderDetails = htmlspecialchars($feed->OrderDetails); 
  225. $PaystationOverrideId = htmlspecialchars($feed->PaystationOverrideId); 
  226. $fields = $feed->FormID ? self::getFormFields($feed->FormID) : false; 
  227.  
  228. ?> 
  229. <strong>Notes:</strong>  
  230. <p> 
  231. All these fields are optional, but we highly recommend that you at least set the Merchant Reference to something help you identify 
  232. the customer, such as their email address. Customer details could be their name and Order details could be the name of the form. 
  233. </p> 
  234. <p> 
  235. The first three fields you map here are displayed in your Paystation Online Admin when looking at transactions. You can also search on the Merchant Reference. 
  236. </p> 
  237. <table class='gfpaystation-feed-fields gfpaystation-details'> 
  238. <tr> 
  239. <th>Merchant Reference:</th> 
  240. <td> 
  241. <select size="1" name="_gfpaystation_merchant_ref"> 
  242. <?php if ($fields) echo self::selectFields($MerchantReference, $fields); ?> 
  243. </select> 
  244. </td> 
  245. </tr> 
  246. <tr> 
  247. <th>Customer Details:</th> 
  248. <td> 
  249. <select size="1" name="_gfpaystation_customer_details"> 
  250. <?php if ($fields) echo self::selectFields($CustomerDetails, $fields); ?> 
  251. </select> 
  252. </td> 
  253. </tr> 
  254. <tr> 
  255. <th>Order Details:</th> 
  256. <td> 
  257. <select size="1" name="_gfpaystation_order_details"> 
  258. <?php if ($fields) echo self::selectFields($OrderDetails, $fields); ?> 
  259. </select> 
  260. </td> 
  261. </tr> 
  262. </table> 
  263. <br /> 
  264. <p> 
  265. <strong>Leave this field empty</strong> unless instructed otherwise. It is only used for a very specific feature to do with making the payment go in different  
  266. Paystation accounts based on user selection on the form such as region, branch, country, etc. Please contact us for more details if you are interested in being able to do this. 
  267. </p> 
  268. <table class='gfpaystation-feed-fields gfpaystation-details'> 
  269. <tr> 
  270. <th>Paystation Id Override:</th> 
  271. <td> 
  272. <select size="1" name="_gfpaystation_override_id"> 
  273. <?php if ($fields) echo self::selectFields($PaystationOverrideId, $fields); ?> 
  274. </select> 
  275. </td> 
  276. </tr> 
  277.  
  278. </table> 
  279. <?php 
  280.  
  281. // ==================================================================================================================================== 
  282. /** 
  283. * Filter insert fields, to set post title from form name. 
  284. * @param array $data the post insert data 
  285. * @param array $postarr data from the form post 
  286. * @return array 
  287. */ 
  288. // ==================================================================================================================================== 
  289. public function filterInsertPostData($data, $postarr) { 
  290.  
  291. $formID = isset($postarr['_gfpaystation_form']) ? intval($postarr['_gfpaystation_form']) : 0; 
  292.  
  293. if ($formID) { 
  294. $form = GFFormsModel::get_form($formID); 
  295. $data['post_title'] = $form->title; 
  296. $data['post_name'] = sanitize_title($form->title); 
  297.  
  298. return $data; 
  299.  
  300. // ==================================================================================================================================== 
  301. /** 
  302. * Save custom fields about the form. 
  303. */ 
  304. // ==================================================================================================================================== 
  305. public function saveCustomFields($postID) { 
  306.  
  307. // Check whether this is an auto save routine. If it is, our form has not been submitted, so we don't want to do anything 
  308. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
  309. return $postID; 
  310.  
  311. global $typenow; 
  312.  
  313. // Handle post type 
  314. if ($typenow == GFPAYSTATION_TYPE_FEED) { 
  315. // verify permission to edit post / page 
  316. if (!current_user_can('edit_post', $postID)) 
  317. return $postID; 
  318.  
  319. $fields = array ( 
  320. '_gfpaystation_form',  
  321. '_gfpaystation_url_fail',  
  322. '_gfpaystation_url_success',  
  323. '_gfpaystation_merchant_ref',  
  324. '_gfpaystation_customer_details',  
  325. '_gfpaystation_order_details',  
  326. '_gfpaystation_override_id',  
  327. '_gfpaystation_opt',  
  328. '_gfpaystation_delay_post',  
  329. '_gfpaystation_delay_notify',  
  330. '_gfpaystation_delay_autorespond',  
  331. ); 
  332.  
  333. if (isset($_POST['_gfpaystation_form'])) { 
  334. if (!wp_verify_nonce($_POST[GFPAYSTATION_TYPE_FEED.'_wpnonce'], GFPAYSTATION_TYPE_FEED.'_save')) 
  335. die('Security exception'); 
  336.  
  337. foreach ($fields as $fieldName) { 
  338. if (isset($_POST[$fieldName])) { 
  339.  
  340. $value = $_POST[$fieldName]; 
  341.  
  342. if (empty($value)) 
  343. delete_post_meta($postID, $fieldName); 
  344. else 
  345. update_post_meta($postID, $fieldName, $value); 
  346. else { 
  347. // checkboxes aren't set, so delete them 
  348. delete_post_meta($postID, $fieldName); 
  349.  
  350. return $postID; 
  351.  
  352. // ==================================================================================================================================== 
  353. /** 
  354. * Remove unwanted actions from list of feeds 
  355. * @param array $actions 
  356. * @return array 
  357. */ 
  358. // ==================================================================================================================================== 
  359. public function filterPostRowActions($actions) { 
  360. unset($actions['inline hide-if-no-js']); 
  361.  
  362. return $actions; 
  363.  
  364. // ==================================================================================================================================== 
  365. /** 
  366. * Change the post updated messages 
  367. * @param array $messages 
  368. * @return array 
  369. */ 
  370. // ==================================================================================================================================== 
  371. public function filterPostUpdatedMessages($messages) { 
  372. $messages[GFPAYSTATION_TYPE_FEED] = array( 
  373. 0 => '', // Unused. Messages start at index 1. 
  374. 1 => 'Feed updated.',  
  375. 2 => 'Custom field updated.',  
  376. 3 => 'Custom field deleted.',  
  377. 4 => 'Feed updated.',  
  378. /** translators: %s: date and time of the revision */ 
  379. 5 => isset($_GET['revision']) ? sprintf( 'Feed restored to revision from %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,  
  380. 6 => 'Feed published.',  
  381. 7 => 'Feed saved.',  
  382. 8 => 'Feed submitted.',  
  383. 9 => 'Feed scheduled for: ',  
  384. 10 => 'Feed draft updated.',  
  385. ); 
  386.  
  387. return $messages; 
  388.  
  389. // ==================================================================================================================================== 
  390. /** 
  391. * Filter to add columns to post list. 
  392. * @param array $posts_columns 
  393. * @return array 
  394. */ 
  395. // ==================================================================================================================================== 
  396. public function filterManageColumns($posts_columns) { 
  397.  
  398. // Date isn't useful for this post type 
  399. unset($posts_columns['date']); 
  400.  
  401. // stop File Gallery adding No. of Attachments 
  402. unset($posts_columns['attachment_count']); 
  403.  
  404. return $posts_columns; 
  405.  
  406. // ==================================================================================================================================== 
  407. /** 
  408. * get a map of GF form field IDs to field names, for populating drop-down lists on the scren where the fields are 
  409. * mapped to the gateway parameters 
  410. * @param int $formID 
  411. * @return array 
  412. */ 
  413. // ==================================================================================================================================== 
  414. public static function getFormFields($formID) { 
  415.  
  416. $form = GFFormsModel::get_form_meta($formID); 
  417.  
  418. $fields = array( 
  419. 'form' => $formID . ' (form ID)',  
  420. 'title' => $form['title'] . ' (form title)',  
  421. ); 
  422.  
  423. if (is_array($form['fields'])) { 
  424. foreach ($form['fields'] as $field) { 
  425. if (!rgar($field, 'displayOnly')) { 
  426. // pick up simple fields and selected compound fields 
  427. if (empty($field['inputs']) || in_array(GFFormsModel::get_input_type($field), array('name', 'address'))) { 
  428. $fields[(string) $field['id']] = GFCommon::get_label($field); 
  429.  
  430. // pick up subfields 
  431. if (isset($field['inputs']) && is_array($field['inputs'])) { 
  432. foreach($field['inputs'] as $input) { 
  433. $fields[(string) $input['id']] = GFCommon::get_label($field, $input['id']); 
  434.  
  435. return $fields; 
  436.  
  437. // ==================================================================================================================================== 
  438. /** 
  439. * Return a list of drop-down list items for field mappings 
  440. * @param string $current the currently selected option 
  441. * @param array $fields 
  442. * @return string 
  443. */ 
  444. // ==================================================================================================================================== 
  445. public static function selectFields($current, $fields) { 
  446. $opts = "<option value=''> (not selected) </option>\n"; 
  447.  
  448. foreach ($fields as $name => $title) { 
  449. $selected = selected($current, $name); 
  450. $title = htmlspecialchars($title); 
  451. $opts .= "<option value='$name' $selected>$title</option>\n"; 
  452.  
  453. return $opts;