Shortcodes.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. final class NF_Display_Shortcodes
  3. {
  4. public function __construct()
  5. {
  6. add_shortcode( 'nf_preview', array( $this, 'display_form_preview' ) );
  7. add_shortcode( 'ninja_form', array( $this, 'display_form_front_end' ) );
  8. add_shortcode( 'ninja_forms', array( $this, 'display_form_front_end' ) );
  9. add_shortcode( 'ninja_forms_display_form', array( $this, 'display_form_front_end' ) );
  10. }
  11. public function display_form_preview( $atts = array() )
  12. {
  13. if( ! isset( $atts[ 'id' ] ) ) return $this->display_no_id();
  14. ob_start();
  15. Ninja_Forms()->display( $atts['id'], TRUE );
  16. return ob_get_clean();
  17. }
  18. public function display_form_front_end( $atts = array() )
  19. {
  20. if( ! isset( $atts[ 'id' ] ) ) return $this->display_no_id();
  21. ob_start();
  22. Ninja_Forms()->display( $atts['id'] );
  23. return ob_get_clean();
  24. }
  25. /**
  26. * TODO: Extract output to template files.
  27. * @return string
  28. */
  29. private function display_no_id()
  30. {
  31. $output = __( 'Notice: Ninja Forms shortcode used without specifying a form.', 'ninja-forms' );
  32. // TODO: Maybe support filterable permissions.
  33. if( ! current_user_can( 'manage_options' ) ) return "<!-- $output -->";
  34. // TODO: Log error for support reference.
  35. // TODO: Maybe display notice if not logged in.
  36. trigger_error( __( 'Ninja Forms shortcode used without specifying a form.', 'ninja-forms' ) );
  37. return "<div style='border: 3px solid red; padding: 1em; margin: 1em auto;'>$output</div>";
  38. }
  39. }