add-form-modal.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Add a button to tinyMCE editors when eidting posts/pages.
  4. *
  5. * @since 2.9.22
  6. */
  7. class NF_Admin_AddFormModal {
  8. function __construct() {
  9. // Add a tinyMCE button to our post and page editor
  10. add_filter( 'media_buttons_context', array( $this, 'insert_form_tinymce_buttons' ) );
  11. }
  12. /**
  13. * Output our tinyMCE field buttons
  14. *
  15. * @access public
  16. * @since 2.8
  17. * @return void
  18. */
  19. public function insert_form_tinymce_buttons( $context ) {
  20. global $pagenow;
  21. if ( 'post.php' != $pagenow ) {
  22. return $context;
  23. }
  24. $html = '<style>
  25. span.nf-insert-form {
  26. color:#888;
  27. font: 400 18px/1 dashicons;
  28. -webkit-font-smoothing: antialiased;
  29. -moz-osx-font-smoothing: grayscale;
  30. display: inline-block;
  31. width: 18px;
  32. height: 18px;
  33. vertical-align: text-top;
  34. margin: 0 2px 0 0;
  35. }
  36. </style>';
  37. $html .= '<a href="#" class="button-secondary nf-insert-form"><span class="nf-insert-form dashicons dashicons-feedback"></span> ' . __( 'Add Form', 'ninja-forms' ) . '</a>';
  38. wp_enqueue_script( 'nf-combobox',
  39. NF_PLUGIN_URL . 'assets/js/min/combobox.min.js',
  40. array( 'jquery', 'jquery-ui-core', 'jquery-ui-button', 'jquery-ui-autocomplete', 'nf-admin-modal' ) );
  41. wp_enqueue_style( 'nf-combobox',
  42. NF_PLUGIN_URL . 'assets/css/combobox.css' );
  43. wp_enqueue_style( 'nf-admin-modal',
  44. NF_PLUGIN_URL . 'assets/css/admin-modal.css' );
  45. wp_enqueue_style( 'jquery-smoothness', NINJA_FORMS_URL .'css/smoothness/jquery-smoothness.css' );
  46. add_action( 'admin_footer', array( $this, 'output_tinymce_button_js' ) );
  47. return $context . ' ' . $html;
  48. }
  49. /**
  50. * Output our tinyMCE field buttons
  51. *
  52. * @access public
  53. * @since 2.8
  54. * @return void
  55. */
  56. public function output_tinymce_button_js( $context ) {
  57. ?>
  58. <div id="nf-insert-form-modal" style="height:350px;">
  59. <p>
  60. <?php
  61. $all_forms = Ninja_Forms()->forms()->get_all();
  62. $first_option = __( 'Select a form or type to search', 'ninja-forms' );
  63. echo '<select class="nf-forms-combobox" id="nf_form_select" data-first-option="' . $first_option . '">';
  64. echo '<option value="">' . $first_option .'</option>';
  65. foreach( $all_forms as $form_id ) {
  66. $label = esc_html( Ninja_Forms()->form( $form_id )->get_setting( 'form_title' ) );
  67. if ( strlen( $label ) > 30 )
  68. $label = substr( $label, 0, 30 ) . '...';
  69. echo '<option value="' . $form_id . '">' . $label . ' - ID: ' . $form_id . '</option>';
  70. }
  71. echo '</select>';
  72. ?>
  73. </p>
  74. </div>
  75. <div id="nf-insert-form-buttons">
  76. <div id="nf-admin-modal-cancel">
  77. <a class="submitdelete deletion modal-close" href="#"><?php _e( 'Cancel', 'ninja-forms' ); ?></a>
  78. </div>
  79. <div id="nf-admin-modal-update">
  80. <a class="button-primary" id="nf-insert-form" href="#"><?php _e( 'Insert', 'ninja-forms' ); ?></a>
  81. </div>
  82. </div>
  83. <script type="text/javascript">
  84. jQuery( document ).ready( function( $ ) {
  85. $( '#nf-insert-form-modal' ).nfAdminModal( { title: '<?php _e( "Add Form", "ninja-forms" ); ?>', buttons: '#nf-insert-form-buttons', backgroundClose: true } );
  86. $( document ).on( 'click', '.nf-insert-form', function( e ) {
  87. e.preventDefault();
  88. $( '#nf-insert-form-modal' ).nfAdminModal( 'open' );
  89. $( '.nf-forms-combobox' ).combobox();
  90. $( '#nf-admin-modal-content .ui-autocomplete-input' ).focus().select();
  91. } );
  92. $( document ).on( 'click', '#nf-insert-form', function( e ) {
  93. e.preventDefault();
  94. var form_id = $( this ).parent().parent().parent().find( '#nf_form_select' ).val();
  95. var shortcode = '[ninja_form id=' + form_id + ']';
  96. window.parent.send_to_editor( shortcode );
  97. $.fn.nfAdminModal.close();
  98. } );
  99. $( document ).on( 'nfAdminModalClose.destroyCombo', function( e ) {
  100. $( '.nf-forms-combobox' ).combobox( 'destroy' );
  101. } );
  102. } );
  103. </script>
  104. <?php
  105. }
  106. }