Нет описания

class-pum-ninja-forms.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class NF_PUM
  7. */
  8. final class NF_PUM {
  9. const PREFIX = 'NF_PUM';
  10. /**
  11. * @var NF_PUM
  12. * @since 3.0
  13. */
  14. private static $instance;
  15. /**
  16. * Plugin Directory
  17. *
  18. * @since 3.0
  19. * @var string $dir
  20. */
  21. public static $dir = '';
  22. /**
  23. * Plugin URL
  24. *
  25. * @since 3.0
  26. * @var string $url
  27. */
  28. public static $url = '';
  29. /**
  30. * Main Plugin Instance
  31. *
  32. * Insures that only one instance of a plugin class exists in memory at any one
  33. * time. Also prevents needing to define globals all over the place.
  34. *
  35. * @since 3.0
  36. * @static
  37. * @static var array $instance
  38. * @return NF_PUM Highlander Instance
  39. */
  40. public static function instance() {
  41. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof NF_PUM ) ) {
  42. spl_autoload_register( array( __CLASS__, 'autoloader' ) );
  43. self::$instance = new NF_PUM();
  44. self::$dir = plugin_dir_path( __FILE__ );
  45. self::$url = plugin_dir_url( __FILE__ );
  46. }
  47. return self::$instance;
  48. }
  49. public function __construct() {
  50. $this->register_actions();
  51. add_filter( 'pum_registered_cookies', array( $this, 'register_cookies' ) );
  52. }
  53. /**
  54. * Register Actions
  55. *
  56. * Register custom form actions to integrate with popups.
  57. */
  58. public function register_actions() {
  59. Ninja_Forms()->actions['closepopup'] = new NF_PUM_Actions_ClosePopup();
  60. Ninja_Forms()->actions['openpopup'] = new NF_PUM_Actions_OpenPopup();
  61. }
  62. /**
  63. * Optional. If your extension creates a new field interaction or display template...
  64. */
  65. public function register_cookies( $cookies ) {
  66. $cookies['ninja_form_success'] = array(
  67. 'labels' => array(
  68. 'name' => __( 'Ninja Form Success (deprecated. Use Form Submission instead.)', 'popup-maker' ),
  69. ),
  70. 'fields' => pum_get_cookie_fields(),
  71. );
  72. return $cookies;
  73. }
  74. /*
  75. * Optional methods for convenience.
  76. */
  77. public static function autoloader( $class_name ) {
  78. if ( class_exists( $class_name ) ) {
  79. return;
  80. }
  81. if ( false === strpos( $class_name, self::PREFIX ) ) {
  82. return;
  83. }
  84. $class_name = str_replace( self::PREFIX, '', $class_name );
  85. $classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'ninja-forms' . DIRECTORY_SEPARATOR;
  86. $class_file = str_replace( '_', DIRECTORY_SEPARATOR, $class_name ) . '.php';
  87. if ( file_exists( $classes_dir . $class_file ) ) {
  88. require_once $classes_dir . $class_file;
  89. }
  90. }
  91. /**
  92. * Template
  93. *
  94. * @param string $file_name
  95. * @param array $data
  96. */
  97. public static function template( $file_name = '', array $data = array() ) {
  98. if ( ! $file_name ) {
  99. return;
  100. }
  101. extract( $data );
  102. include self::$dir . 'includes/Templates/' . $file_name;
  103. }
  104. /**
  105. * Config
  106. *
  107. * @param $file_name
  108. *
  109. * @return mixed
  110. */
  111. public static function config( $file_name ) {
  112. return include self::$dir . 'includes/Config/' . $file_name . '.php';
  113. }
  114. }
  115. /**
  116. * The main function responsible for returning The Highlander Plugin
  117. * Instance to functions everywhere.
  118. *
  119. * Use this function like you would a global variable, except without needing
  120. * to declare the global.
  121. *
  122. * @since 3.0
  123. * @return {class} Highlander Instance
  124. */
  125. function NF_PUM() {
  126. return NF_PUM::instance();
  127. }
  128. /**
  129. *
  130. */
  131. function pum_nf_should_init() {
  132. if ( ! class_exists( 'Ninja_Forms' ) ) {
  133. return;
  134. }
  135. if ( version_compare( get_option( 'ninja_forms_version', '0.0.0' ), '3.0', '<' ) || get_option( 'ninja_forms_load_deprecated', false ) ) {
  136. return;
  137. }
  138. NF_PUM();
  139. }
  140. add_action( 'init', 'pum_nf_should_init', 0 );