forms.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Handles adding and removing forms.
  4. *
  5. * @package Ninja Forms
  6. * @subpackage Classes/Form
  7. * @copyright Copyright (c) 2014, WPNINJAS
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9. * @since 2.9
  10. */
  11. class NF_Forms {
  12. /**
  13. * Store our array of form ids
  14. *
  15. * @since 2.9
  16. */
  17. var $forms = array();
  18. /**
  19. * Get things started
  20. *
  21. * @access public
  22. * @since 2.9
  23. * @return void
  24. */
  25. public function __construct() {
  26. add_action( 'nf_maybe_delete_form', array( $this, 'maybe_delete' ) );
  27. }
  28. /**
  29. * Get all forms
  30. *
  31. * @access public
  32. * @since 2.9
  33. * @return array $forms
  34. */
  35. public function get_all( $show_new = false ) {
  36. global $wpdb;
  37. $debug = ! empty ( $_REQUEST['debug'] ) ? true : false;
  38. if ( empty ( $this->forms ) ) {
  39. $forms = nf_get_objects_by_type( 'form' );
  40. $tmp_array = array();
  41. foreach ( $forms as $form ) {
  42. $form_id = $form['id'];
  43. $status = Ninja_Forms()->form( $form_id )->get_setting( 'status' );
  44. if ( ( $status == 'new' && $show_new ) || $status != 'new' ) {
  45. $title = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' );
  46. if ( strpos( $title, '_' ) === 0 ) {
  47. if ( $debug )
  48. $tmp_array[] = $form_id;
  49. } else {
  50. $tmp_array[] = $form_id;
  51. }
  52. }
  53. }
  54. $this->forms = $tmp_array;
  55. }
  56. return $this->forms;
  57. }
  58. /**
  59. * Delete a form if it is created and not saved within 24 hrs.
  60. *
  61. * @access public
  62. * @since 2.9
  63. * @return void
  64. */
  65. public function maybe_delete( $form_id ) {
  66. $status = Ninja_Forms()->form( $form_id )->get_setting( 'status' );
  67. if ( 'new' == $status ) {
  68. Ninja_Forms()->form( $form_id )->delete();
  69. }
  70. }
  71. /**
  72. * Update cached forms
  73. *
  74. * @access public
  75. * @since 2.9
  76. * @return void
  77. */
  78. public function update_cache( $debug = false, $show_new = false ) {
  79. $this->forms = array();
  80. $this->get_all( $debug, $show_new );
  81. }
  82. }