NinjaFormsCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. if( ! class_exists( 'WP_CLI_Command' ) ) exit;
  3. /**
  4. * The Ninja Forms WP-CLI Command
  5. */
  6. class NF_WPCLI_NinjaFormsCommand extends WP_CLI_Command
  7. {
  8. /**
  9. * Display Ninja Forms Information
  10. *
  11. * @subcommand info
  12. */
  13. function info()
  14. {
  15. $this->peeking_ninja();
  16. WP_CLI::success( 'Welcome to the Ninja Forms WP-CLI Extension!' );
  17. WP_CLI::line( '' );
  18. WP_CLI::line( '- Ninja Forms Version: ' . Ninja_Forms::VERSION );
  19. WP_CLI::line( '- Ninja Forms Directory: ' . Ninja_Forms::$dir );
  20. WP_CLI::line( '- Ninja Forms Public URL: ' . Ninja_Forms::$url );
  21. WP_CLI::line( '' );
  22. }
  23. /**
  24. * Creates a Form
  25. *
  26. * ## OPTIONS
  27. *
  28. * <title>
  29. * : The form title.
  30. *
  31. * ## EXAMPLES
  32. *
  33. * wp ninja-forms form "My New Form"
  34. *
  35. * @synopsis <title>
  36. * @subcommand form
  37. * @alias create-form
  38. */
  39. public function create_form( $args, $assoc_args )
  40. {
  41. list( $title ) = $args;
  42. $form = Ninja_Forms()->form()->get();
  43. $form->update_setting( 'title', $title );
  44. $form->save();
  45. }
  46. /**
  47. * @subcommand list
  48. * @alias list-forms
  49. */
  50. public function list_forms( $args, $assoc_args )
  51. {
  52. foreach( Ninja_Forms()->form()->get_forms() as $form ){
  53. WP_CLI::line( '#' . $form->get_id() . ' - ' . $form->get_setting( 'title' ) );
  54. }
  55. }
  56. /**
  57. * @synopsis <id>
  58. * @subcommand get
  59. * @alias get-form
  60. */
  61. public function get_form( $args, $assoc_args )
  62. {
  63. list( $id ) = $args;
  64. $form = Ninja_Forms()->form( $id )->get();
  65. WP_CLI::line( '#' . $form->get_id() . ' - ' . $form->get_setting( 'title' ) );
  66. foreach( Ninja_Forms()->form( $id )->get_fields() as $field ){
  67. $key = $field->get_setting( 'key' );
  68. $label = $field->get_setting( 'label' );
  69. if( ! $key ) $key = strtolower( str_replace( ' ', '', $label ) );
  70. WP_CLI::line( "'$key': $label" );
  71. }
  72. }
  73. /**
  74. * Installs mock form data
  75. */
  76. public function mock()
  77. {
  78. $mock_data = new NF_Database_MockData();
  79. $mock_data->form_contact_form_1();
  80. $mock_data->form_contact_form_2();
  81. $mock_data->form_email_submission();
  82. $mock_data->form_long_form();
  83. }
  84. private function peeking_ninja()
  85. {
  86. $output = file_get_contents( Ninja_Forms::$dir . 'includes/Templates/wpcli-header-art.txt' );
  87. WP_CLI::line( $output );
  88. }
  89. } // END CLASS NF_WPCLI_NinjaFormsCommand