calderawpfiltermagictag

Class magictag.

Defined (2)

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

/vendor/calderawp/magic-tags/src/magictag.php  
  1. class magictag { 
  2.  
  3. /** 
  4. * Constructor for class. Sets up the default filters. 
  5. */ 
  6. function __construct() { 
  7.  
  8. // post 
  9. add_filter( 'caldera_magic_tag-post', array( $this, 'filter_post') ); 
  10. // GET 
  11. add_filter( 'caldera_magic_tag-_GET', array( $this, 'filter_get_var') ); 
  12. // POST 
  13. add_filter( 'caldera_magic_tag-_POST', array( $this, 'filter_post_var') ); 
  14. // REQUEST 
  15. add_filter( 'caldera_magic_tag-_REQUEST', array( $this, 'filter_request_var') ); 
  16. // user 
  17. add_filter( 'caldera_magic_tag-user', array( $this, 'filter_user') ); 
  18. // date 
  19. add_filter( 'caldera_magic_tag-date', array( $this, 'filter_date') ); 
  20. // ip 
  21. add_filter( 'caldera_magic_tag-ip', array( $this, 'filter_ip') ); 
  22.  
  23.  
  24. /** 
  25. * Renders a magic tag 
  26. * @return string converted string with matched tags replaced 
  27. */ 
  28. public function do_magic_tag($content) { 
  29.  
  30. // check for magics 
  31. preg_match_all("/\{(.+?)\}/", (string) $content, $magics); 
  32.  
  33. // on found tags 
  34. if(!empty($magics[1])) { 
  35. foreach($magics[1] as $magic_key=>$magic_tag) { 
  36. $params = explode(':', $magic_tag, 2 ); 
  37. if( empty( $params[1] ) ) { continue; } 
  38. // filter a general tag using the second argument as the original tag 
  39. $filter_value = apply_filters( 'caldera_magic_tag', apply_filters( "caldera_magic_tag-{$params[0]}", $params[1] ) , $magics[0][$magic_key]); 
  40. // chech the tag changed 
  41. if( $filter_value !== $params[1] ) { 
  42. // on a difference in the tag, replace it. 
  43. $content = str_replace( $magics[0][$magic_key], $filter_value, $content ); 
  44.  
  45. // return content converted or not. 
  46. return $content; 
  47.  
  48.  
  49. /** 
  50. * Gets a posts meta value 
  51. * @since 0.0.1 
  52. * @return string post field value 
  53. */ 
  54. private function get_post_value( $field, $in_params, $post ) { 
  55.  
  56. if( !is_object( $post ) ) { 
  57. return $in_params; 
  58.  
  59. if( isset( $post->{$field} ) ) { 
  60. return implode( ', ', (array) $post->{$field} ); 
  61.  
  62. return $in_params; 
  63.  
  64.  
  65. /** 
  66. * Filters a post magic tag 
  67. * @since 0.0.1 
  68. * @return string converted tag value 
  69. */ 
  70. public function filter_post( $in_params ) { 
  71.  
  72. // check if there is a third 
  73. $params = explode( ':', $in_params ); 
  74.  
  75. if( isset( $params[1] ) ) { 
  76. // a third e.g {post:24:post_title} indicates post ID 24 value post_title 
  77. $post = get_post( $params[0] ); 
  78. $field = $params[1]; 
  79.  
  80. }else{ 
  81. // stic to current post 
  82. global $post; 
  83. $field = $params[0]; 
  84.  
  85.  
  86. // try object  
  87. return $this->get_post_value( $field, $in_params, $post ); 
  88.  
  89. /** 
  90. * Filters a GET magic tag 
  91. * @since 0.0.1 
  92. * @return string converted tag value 
  93. */ 
  94. public function filter_get_var( $params ) { 
  95.  
  96. if( isset($_GET[$params])) { 
  97. return wp_slash( $_GET[$params] ); 
  98. }elseif( !empty( $_SERVER['HTTP_REFERER'] ) ) { 
  99. return $this->filter_get_referr_var( $params ); 
  100.  
  101. return $params; 
  102.  
  103. /** 
  104. * Filters a GET from a referrer 
  105. * @since 0.0.1 
  106. * @return string converted tag value 
  107. */ 
  108. private function filter_get_referr_var( $params ) { 
  109.  
  110. $referer = parse_url( $_SERVER['HTTP_REFERER'] ); 
  111. if( !empty( $referer['query'] ) ) { 
  112. parse_str( $referer['query'], $get_vars ); 
  113. if( isset( $get_vars[$params] ) ) { 
  114. return wp_slash( $get_vars[$params] ); 
  115. return $params; 
  116. }  
  117.  
  118. /** 
  119. * Filters a POST magic tag 
  120. * @since 0.0.1 
  121. * @return string converted tag value 
  122. */ 
  123. public function filter_post_var( $params ) { 
  124.  
  125. if( isset($_POST[$params])) { 
  126. return wp_slash( $_POST[$params] ); 
  127.  
  128. return $params;  
  129.  
  130. /** 
  131. * Filters a REQUEST magic tag 
  132. * @since 0.0.1 
  133. * @return string converted tag value 
  134. */ 
  135. public function filter_request_var( $params ) { 
  136.  
  137. if( isset($_REQUEST[$params])) { 
  138. return wp_slash( $_REQUEST[$params] ); 
  139.  
  140. return $params; 
  141.  
  142. /** 
  143. * Filters a user magic tag 
  144. * @since 0.0.1 
  145. * @return string converted tag value 
  146. */ 
  147. public function filter_user( $params ) { 
  148.  
  149. if(!is_user_logged_in() ) { 
  150. return $params; 
  151. $user = get_userdata( get_current_user_id() ); 
  152. if(isset( $user->data->{$params} )) { 
  153. $params = $user->data->{$params}; 
  154.  
  155. $is_meta = get_user_meta( $user->ID, $params, true ); 
  156. if( !empty( $is_meta ) ) { 
  157. $params = implode( ', ', (array) $is_meta ); 
  158.  
  159. return $params; 
  160.  
  161. /** 
  162. * Filters a date magic tag 
  163. * @since 0.0.1 
  164. * @return string converted tag value 
  165. */ 
  166. public function filter_date( $params ) { 
  167.  
  168. return date( $params ); 
  169.  
  170.  
  171. /** 
  172. * Filters a ip magic tag 
  173. * @since 0.0.1 
  174. * @return string converted tag value 
  175. */ 
  176. public function filter_ip( ) { 
  177.  
  178. // get IP 
  179. $ip = $_SERVER['REMOTE_ADDR']; 
  180. if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
  181. $ip = $_SERVER['HTTP_CLIENT_IP']; 
  182. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { 
  183. $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  184.  
  185. return $ip; 
  186.  
  187.  
/vendor/calderawp/metaplate-core/vendor/calderawp/magic-tags/src/magictag.php  
  1. class magictag { 
  2.  
  3. /** 
  4. * Constructor for class. Sets up the default filters. 
  5. */ 
  6. function __construct() { 
  7.  
  8. // GET 
  9. add_filter( 'caldera_magic_tag-_GET', array( $this, 'filter_get_var') ); 
  10. // POST 
  11. add_filter( 'caldera_magic_tag-_POST', array( $this, 'filter_post_var') ); 
  12. // REQUEST 
  13. add_filter( 'caldera_magic_tag-_REQUEST', array( $this, 'filter_request_var') ); 
  14. // post 
  15. add_filter( 'caldera_magic_tag-post', array( $this, 'filter_post') ); 
  16. // user 
  17. add_filter( 'caldera_magic_tag-user', array( $this, 'filter_user') ); 
  18. // date 
  19. add_filter( 'caldera_magic_tag-date', array( $this, 'filter_date') ); 
  20. // date 
  21. add_filter( 'caldera_magic_tag-ip', array( $this, 'filter_ip') ); 
  22.  
  23.  
  24. /** 
  25. * Renders a magic tag 
  26. * @return string converted string with matched tags replaced 
  27. */ 
  28. static public function do_magic_tag($content) { 
  29.  
  30. // check for magics 
  31. preg_match_all("/\{(.+?)\}/", (string) $content, $magics); 
  32.  
  33. // on found tags 
  34. if(!empty($magics[1])) { 
  35. foreach($magics[1] as $magic_key=>$magic_tag) { 
  36.  
  37. $params = explode(':', $magic_tag ); 
  38.  
  39. // create the base for arguments to be added as second filter arg 
  40. $magic_tag_base = array_shift( $params ); 
  41. // filter the matched tag part 
  42. $magic_tag = apply_filters( "caldera_magic_tag-{$magic_tag_base}", $params); 
  43.  
  44. // filter a general tag using the second argument as the original tag 
  45. $filter_value = apply_filters( 'caldera_magic_tag', $magic_tag, $magics[0][$magic_key]);  
  46.  
  47. // chech the tag changed 
  48. if( $filter_value != $magics[1][$magic_key] ) { 
  49. // on a difference in the tag, replace it. 
  50. $content = str_replace($magics[0][$magic_key], $filter_value, $content); 
  51. // return content converted or not. 
  52. return $content; 
  53.  
  54. /** 
  55. * filters a GET magic tag 
  56. * @return string converted tag value 
  57. */ 
  58. static public function filter_get_var( $params ) { 
  59. $magic_tag = null; 
  60. if( isset($_GET[$params[0]])) { 
  61. $magic_tag = $_GET[$params[0]]; 
  62. return $magic_tag;  
  63.  
  64. /** 
  65. * filters a POST magic tag 
  66. * @return string converted tag value 
  67. */ 
  68. static public function filter_post_var( $params ) { 
  69. $magic_tag = null; 
  70. if( isset($_POST[$params[0]])) { 
  71. $magic_tag = $_POST[$params[0]]; 
  72. return $magic_tag;  
  73.  
  74. /** 
  75. * filters a REQUEST magic tag 
  76. * @return string converted tag value 
  77. */ 
  78. static public function filter_request_var( $params ) { 
  79. $magic_tag = null; 
  80. if( isset($_REQUEST[$params[0]])) { 
  81. $magic_tag = $_REQUEST[$params[0]]; 
  82. return $magic_tag;  
  83.  
  84. /** 
  85. * filters a post magic tag 
  86. * @return string converted tag value 
  87. */ 
  88. static public function filter_post( $params ) { 
  89. // set default value. 
  90. $magic_tag = null; 
  91.  
  92. if( isset( $params[1] ) ) { 
  93. // a third e.g {post:24:post_title} indicates post ID 24 value post_title 
  94. $post = get_post( $params[0] ); 
  95. $field = $params[1]; 
  96. }else{ 
  97. // stic to current post 
  98. global $post; 
  99. $field = $params[0]; 
  100.  
  101. if(is_object($post)) { 
  102.  
  103. if(isset( $post->{$field} )) { 
  104.  
  105. return $post->{$field}; 
  106.  
  107.  
  108. // try meta data 
  109. $post_metavalue = get_post_meta( $post->ID, $field ); 
  110. if( false !== strpos($field, ', ') ) { 
  111. $field = explode(', ', $field); 
  112. if(!empty($post_metavalue)) { 
  113.  
  114. $outmagic = array(); 
  115. foreach ( (array) $field as $requested_field) { 
  116. foreach( (array) $post_metavalue as $meta_field) { 
  117. if(isset($meta_field[$subvalue])) { 
  118. $outmagic[] = $post_metavalue; 
  119. }  
  120. if( !empty( $outmagic ) ) { 
  121. $magic_tag = implode(', ', $outmagic); 
  122.  
  123. return $magic_tag;  
  124.  
  125. /** 
  126. * filters a user magic tag 
  127. * @return string converted tag value 
  128. */ 
  129. static public function filter_user( $params ) { 
  130.  
  131. if(!is_user_logged_in() || empty( $params[0] ) ) { 
  132. return null; 
  133. $user = get_userdata( get_current_user_id() ); 
  134. if(isset( $user->data->{$params[0]} )) { 
  135. return $user->data->{$params[0]}; 
  136.  
  137. $is_meta = get_user_meta( $user->ID, $params[0], true ); 
  138. if( !empty( $is_meta ) ) { 
  139. return $is_meta; 
  140.  
  141. return null; 
  142.  
  143. /** 
  144. * filters a date magic tag 
  145. * @return string converted tag value 
  146. */ 
  147. static public function filter_date( $params ) { 
  148. return date( $params[0] ); 
  149.  
  150. /** 
  151. * filters a ip magic tag 
  152. * @return string converted tag value 
  153. */ 
  154. static public function filter_ip( ) { 
  155. return $_SERVER['REMOTE_ADDR']; 
  156.