Crunion_Contact_Form_Shortcode

Generic shortcode class.

Defined (1)

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

/modules/contact-form/grunion-contact-form.php  
  1. class Crunion_Contact_Form_Shortcode { 
  2. /** 
  3. * @var string the name of the shortcode: [$shortcode_name /] 
  4. */ 
  5. public $shortcode_name; 
  6.  
  7. /** 
  8. * @var array key => value pairs for the shortcode's attributes: [$shortcode_name key="value" ... /] 
  9. */ 
  10. public $attributes; 
  11.  
  12. /** 
  13. * @var array key => value pair for attribute defaults 
  14. */ 
  15. public $defaults = array(); 
  16.  
  17. /** 
  18. * @var null|string Null for selfclosing shortcodes. Hhe inner content of otherwise: [$shortcode_name]$content[/$shortcode_name] 
  19. */ 
  20. public $content; 
  21.  
  22. /** 
  23. * @var array Associative array of inner "child" shortcodes equivalent to the $content: [$shortcode_name][child 1/][child 2/][/$shortcode_name] 
  24. */ 
  25. public $fields; 
  26.  
  27. /** 
  28. * @var null|string The HTML of the parsed inner "child" shortcodes". Null for selfclosing shortcodes. 
  29. */ 
  30. public $body; 
  31.  
  32. /** 
  33. * @param array $attributes An associative array of shortcode attributes. @see shortcode_atts() 
  34. * @param null|string $content Null for selfclosing shortcodes. The inner content otherwise. 
  35. */ 
  36. function __construct( $attributes, $content = null ) { 
  37. $this->attributes = $this->unesc_attr( $attributes ); 
  38. if ( is_array( $content ) ) { 
  39. $string_content = ''; 
  40. foreach ( $content as $field ) { 
  41. $string_content .= (string) $field; 
  42.  
  43. $this->content = $string_content; 
  44. } else { 
  45. $this->content = $content; 
  46.  
  47. $this->parse_content( $this->content ); 
  48.  
  49. /** 
  50. * Processes the shortcode's inner content for "child" shortcodes 
  51. * @param string $content The shortcode's inner content: [shortcode]$content[/shortcode] 
  52. */ 
  53. function parse_content( $content ) { 
  54. if ( is_null( $content ) ) { 
  55. $this->body = null; 
  56.  
  57. $this->body = do_shortcode( $content ); 
  58.  
  59. /** 
  60. * Returns the value of the requested attribute. 
  61. * @param string $key The attribute to retrieve 
  62. * @return mixed 
  63. */ 
  64. function get_attribute( $key ) { 
  65. return isset( $this->attributes[$key] ) ? $this->attributes[$key] : null; 
  66.  
  67. function esc_attr( $value ) { 
  68. if ( is_array( $value ) ) { 
  69. return array_map( array( $this, 'esc_attr' ), $value ); 
  70.  
  71. $value = Grunion_Contact_Form_Plugin::strip_tags( $value ); 
  72. $value = _wp_specialchars( $value, ENT_QUOTES, false, true ); 
  73.  
  74. // Shortcode attributes can't contain "]" 
  75. $value = str_replace( ']', '', $value ); 
  76. $value = str_replace( ', ', ',', $value ); // store commas encoded 
  77. $value = strtr( $value, array( '%' => '%25', '&' => '%26' ) ); 
  78.  
  79. // shortcode_parse_atts() does stripcslashes() 
  80. $value = addslashes( $value ); 
  81. return $value; 
  82.  
  83. function unesc_attr( $value ) { 
  84. if ( is_array( $value ) ) { 
  85. return array_map( array( $this, 'unesc_attr' ), $value ); 
  86.  
  87. // For back-compat with old Grunion encoding 
  88. // Also, unencode commas 
  89. $value = strtr( $value, array( '%26' => '&', '%25' => '%' ) ); 
  90. $value = preg_replace( array( '/�*22;/i', '/�*27;/i', '/�*26;/i', '/�*2c;/i' ), array( '"', "'", '&', ', ' ), $value ); 
  91. $value = htmlspecialchars_decode( $value, ENT_QUOTES ); 
  92. $value = Grunion_Contact_Form_Plugin::strip_tags( $value ); 
  93.  
  94. return $value; 
  95.  
  96. /** 
  97. * Generates the shortcode 
  98. */ 
  99. function __toString() { 
  100. $r = "[{$this->shortcode_name} "; 
  101.  
  102. foreach ( $this->attributes as $key => $value ) { 
  103. if ( !$value ) { 
  104. continue; 
  105.  
  106. if ( isset( $this->defaults[$key] ) && $this->defaults[$key] == $value ) { 
  107. continue; 
  108.  
  109. if ( 'id' == $key ) { 
  110. continue; 
  111.  
  112. $value = $this->esc_attr( $value ); 
  113.  
  114. if ( is_array( $value ) ) { 
  115. $value = join( ', ', $value ); 
  116.  
  117. if ( false === strpos( $value, "'" ) ) { 
  118. $value = "'$value'"; 
  119. } elseif ( false === strpos( $value, '"' ) ) { 
  120. $value = '"' . $value . '"'; 
  121. } else { 
  122. // Shortcodes can't contain both '"' and "'". Strip one. 
  123. $value = str_replace( "'", '', $value ); 
  124. $value = "'$value'"; 
  125.  
  126. $r .= "{$key}={$value} "; 
  127.  
  128. $r = rtrim( $r ); 
  129.  
  130. if ( $this->fields ) { 
  131. $r .= ']'; 
  132.  
  133. foreach ( $this->fields as $field ) { 
  134. $r .= (string) $field; 
  135.  
  136. $r .= "[/{$this->shortcode_name}]"; 
  137. } else { 
  138. $r .= '/]'; 
  139.  
  140. return $r;