NF_MergeTags_WP

Class NF_MergeTags_WordPress.

Defined (1)

The class is defined in the following location(s).

/includes/MergeTags/WP.php  
  1. final class NF_MergeTags_WP extends NF_Abstracts_MergeTags 
  2. protected $id = 'wp'; 
  3.  
  4. /** 
  5. * @var array 
  6. * $post_meta[ $meta_key ] = $meta_value; 
  7. */ 
  8. protected $post_meta = array(); 
  9.  
  10. public function __construct() 
  11. parent::__construct(); 
  12. $this->title = __( 'WordPress', 'ninja-forms' ); 
  13. $this->merge_tags = Ninja_Forms()->config( 'MergeTagsWP' ); 
  14.  
  15. // Setup merge tag data for each post in The Loop. 
  16. add_action( 'the_post', array( $this, 'init' ) ); 
  17.  
  18. // Setup merge tag data when Doing AJAX. 
  19. add_action( 'admin_init', array( $this, 'init' ) ); 
  20.  
  21. public function init() 
  22. global $post; 
  23.  
  24. $this->setup_post_meta( $this->post_id() ); 
  25.  
  26. public function replace( $subject ) 
  27.  
  28. /** 
  29. * If we are dealing with a post meta merge tag, we need to overwrite the parent replace() method. 
  30. * Otherwise, we use the parent's method. 
  31. */ 
  32.  
  33. /** 
  34. * {post_meta:foo} --> meta key is 'foo' 
  35. */ 
  36. preg_match_all("/{post_meta:(.*?)}/", $subject, $matches ); 
  37.  
  38. // If not matching merge tags are found, then return early. 
  39. if( empty( $matches[0] ) ) return parent::replace( $subject ); 
  40.  
  41.  
  42. // Recursively replace merge tags. 
  43. if( is_array( $subject ) ) { 
  44. foreach( $subject as $i => $s ) { 
  45. $subject[ $i ] = $this->replace( $s ); 
  46. return $subject; 
  47.  
  48. /** 
  49. * $matches[0][$i] merge tag match {post_meta:foo} 
  50. * $matches[1][$i] captured meta key foo 
  51. */ 
  52. foreach( $matches[0] as $i => $search ) { 
  53. $meta_key = $matches[ 1 ][ $i ]; 
  54. if( ! isset( $this->post_meta[ $meta_key ] ) ) continue; 
  55. $subject = str_replace( $search, $this->post_meta[ $meta_key ], $subject ); 
  56.  
  57. return $subject; 
  58.  
  59. protected function post_id() 
  60. global $post; 
  61.  
  62. if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
  63. // If we are doing AJAX, use the referer to get the Post ID. 
  64. $post_id = url_to_postid( wp_get_referer() ); 
  65. } elseif( $post ) { 
  66. $post_id = $post->ID; 
  67. } else { 
  68. return false; // No Post ID found. 
  69.  
  70. return $post_id; 
  71.  
  72. protected function post_title() 
  73. $post_id = $this->post_id(); 
  74. if( ! $post_id ) return; 
  75. $post = get_post( $post_id ); 
  76. return ( $post ) ? $post->post_title : ''; 
  77.  
  78. protected function post_url() 
  79. $post_id = $this->post_id(); 
  80. if( ! $post_id ) return; 
  81. $post = get_post( $post_id ); 
  82. return ( $post ) ? get_permalink( $post->ID ) : ''; 
  83.  
  84. protected function post_author() 
  85. $post_id = $this->post_id(); 
  86. if( ! $post_id ) return; 
  87. $post = get_post( $post_id ); 
  88. if( ! $post ) return ''; 
  89. $author = get_user_by( 'id', $post->post_author); 
  90. return $author->display_name; 
  91.  
  92. protected function post_author_email() 
  93. $post_id = $this->post_id(); 
  94. if( ! $post_id ) return; 
  95. $post = get_post( $post_id ); 
  96. if( ! $post ) return ''; 
  97. $author = get_user_by( 'id', $post->post_author ); 
  98. return $author->user_email; 
  99.  
  100. public function setup_post_meta( $post_id ) 
  101. global $wpdb; 
  102.  
  103. // Get ALL post meta for a given Post ID. 
  104. $results = $wpdb->get_results( $wpdb->prepare( " 
  105. SELECT `meta_key`, `meta_value` 
  106. FROM {$wpdb->postmeta} 
  107. WHERE `post_id` = %d 
  108. ", $post_id ) ); 
  109.  
  110. foreach( $results as $result ) { 
  111. $this->post_meta[ $result->meta_key ] = $result->meta_value; 
  112.  
  113. protected function user_id() 
  114. $current_user = wp_get_current_user(); 
  115.  
  116. return ( $current_user ) ? $current_user->ID : ''; 
  117.  
  118. protected function user_first_name() 
  119. $current_user = wp_get_current_user(); 
  120.  
  121. return ( $current_user ) ? $current_user->user_firstname : ''; 
  122.  
  123. protected function user_last_name() 
  124. $current_user = wp_get_current_user(); 
  125.  
  126. return ( $current_user ) ? $current_user->user_lastname : ''; 
  127.  
  128. protected function user_display_name() 
  129. $current_user = wp_get_current_user(); 
  130.  
  131. return ( $current_user ) ? $current_user->display_name : ''; 
  132.  
  133. protected function user_email() 
  134. $current_user = wp_get_current_user(); 
  135.  
  136. return ( $current_user ) ? $current_user->user_email : ''; 
  137.  
  138. protected function user_url() 
  139. $current_user = wp_get_current_user(); 
  140.  
  141. return ( $current_user ) ? $current_user->user_url : ''; 
  142.  
  143. protected function admin_email() 
  144. return get_option( 'admin_email' ); 
  145.  
  146. protected function site_title() 
  147. return get_bloginfo( 'name' ); 
  148.  
  149. protected function site_url() 
  150. return get_bloginfo( 'url' ); 
  151.  
  152. } // END CLASS NF_MergeTags_System