Wpcf7_Signature_Public

The public-facing functionality of the plugin.

Defined (1)

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

/public/class-wpcf7-signature-public.php  
  1. class Wpcf7_Signature_Public { 
  2.  
  3. /** 
  4. * The ID of this plugin. 
  5. * @since 4.0.0 
  6. * @access private 
  7. * @var string $plugin_name The ID of this plugin. 
  8. */ 
  9. private $plugin_name; 
  10.  
  11. /** 
  12. * The version of this plugin. 
  13. * @since 4.0.0 
  14. * @access private 
  15. * @var string $version The current version of this plugin. 
  16. */ 
  17. private $version; 
  18.  
  19. /** 
  20. * Initialize the class and set its properties. 
  21. * @since 4.0.0 
  22. * @param string $plugin_name The name of the plugin. 
  23. * @param string $version The version of this plugin. 
  24. */ 
  25. public function __construct( $plugin_name, $version ) { 
  26.  
  27. $this->plugin_name = $plugin_name; 
  28. $this->version = $version; 
  29.  
  30.  
  31. /** 
  32. * Register the stylesheets for the public-facing side of the site. 
  33. * @since 4.0.0 
  34. */ 
  35. public function enqueue_styles() { 
  36.  
  37. wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/style.css', array(), $this->version, 'all' ); 
  38.  
  39.  
  40. /** 
  41. * Register the JavaScript for the public-facing side of the site. 
  42. * @since 4.0.0 
  43. */ 
  44. public function enqueue_scripts() { 
  45.  
  46. wp_enqueue_script( $this->plugin_name.'-signature', plugin_dir_url( __FILE__ ) . 'js/signature_pad.min.js', array(), $this->version, false ); 
  47. wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/scripts.js', array( 'jquery', $this->plugin_name.'-signature' ), $this->version, false ); 
  48.  
  49.  
  50. /** 
  51. * Add shortcode handler to CF7 
  52. * @since 4.0.0 
  53. */ 
  54. public function add_signature_shortcode_handler() { 
  55. if (function_exists('wpcf7_add_form_tag')) { 
  56. wpcf7_add_form_tag( 
  57. array( 'signature', 'signature*' ),  
  58. array($this, 'signature_shortcode_handler'), true  
  59. ); 
  60.  
  61. /** 
  62. * Signature Shortcode handler 
  63. * @since 4.0.0 
  64. */ 
  65. public function signature_shortcode_handler($tag) { 
  66.  
  67. $tag = new WPCF7_FormTag( $tag ); 
  68.  
  69. if ( empty( $tag->name ) ) 
  70. return ''; 
  71.  
  72. $validation_error = wpcf7_get_validation_error( $tag->name ); 
  73.  
  74. $class = wpcf7_form_controls_class( $tag->type, 'wpcf7-signature' ); 
  75.  
  76. if ( $validation_error ) 
  77. $class .= ' wpcf7-not-valid'; 
  78.  
  79. $atts = array(); 
  80.  
  81. $width = $tag->get_cols_option( '300' ); 
  82. $height = $tag->get_rows_option( '200' ); 
  83.  
  84. $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true ); 
  85.  
  86. if ( $tag->has_option( 'readonly' ) ) 
  87. $atts['readonly'] = 'readonly'; 
  88.  
  89. if ( $tag->is_required() ) 
  90. $atts['aria-required'] = 'true'; 
  91.  
  92. $atts['aria-invalid'] = $validation_error ? 'true' : 'false'; 
  93.  
  94. $value = (string) reset( $tag->values ); 
  95.  
  96. if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) { 
  97. $atts['placeholder'] = $value; 
  98. $value = ''; 
  99. } elseif ( '' === $value ) { 
  100. $value = $tag->get_default_option(); 
  101.  
  102. if ( wpcf7_is_posted() && isset( $_POST[$tag->name] ) ) 
  103. $value = wp_unslash( $_POST[$tag->name] ); 
  104.  
  105. /** Input attributes */ 
  106.  
  107. $atts['value'] = $value; 
  108. $atts['type'] = 'hidden'; 
  109. $atts['name'] = $tag->name; 
  110. $atts = wpcf7_format_atts( $atts ); 
  111.  
  112. /** Canvas attributes */ 
  113.  
  114. // Pen color 
  115. $atts_canvas['data-color'] = $tag->get_option( 'color', '#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})', true ); 
  116.  
  117. // Background color 
  118. $atts_canvas['data-background'] = $tag->get_option( 'background', '#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})', true ); 
  119.  
  120. $atts_canvas['id'] = ($tag->get_id_option() != '' ? $tag->get_id_option() : "wpcf7_".$tag->name."_signature"); 
  121.  
  122. $canvas_class = $tag->name; 
  123. $atts_canvas['class'] = $tag->get_class_option( $canvas_class ); 
  124.  
  125. $atts_canvas = wpcf7_format_atts( $atts_canvas ); 
  126.  
  127. /** Attachment attributes */ 
  128.  
  129. $atts_attach['value'] = $tag->has_option( 'attachment' ); 
  130. $atts_attach['type'] = 'hidden'; 
  131. $atts_attach['name'] = $tag->name . "-attachment"; 
  132. $atts_attach = wpcf7_format_atts( $atts_attach ); 
  133.  
  134. /** Inline attributes */ 
  135.  
  136. $atts_inline['value'] = $tag->has_option( 'inline' ); 
  137. $atts_inline['type'] = 'hidden'; 
  138. $atts_inline['name'] = $tag->name . "-inline"; 
  139. $atts_inline = wpcf7_format_atts( $atts_inline ); 
  140.  
  141. $html = sprintf( 
  142. '<div class="wpcf7-form-control-signature-global-wrap" data-field-id="%1$s"> 
  143. <div class="wpcf7-form-control-signature-wrap" style="width:%5$spx;height:%6$spx;"> 
  144. <div class="wpcf7-form-control-signature-body"> 
  145. <canvas %8$s></canvas> 
  146. </div> 
  147. </div> 
  148. <div class="wpcf7-form-control-clear-wrap"> 
  149. <input id="wpcf7_%4$s_clear" type="button" value="%7$s"/> 
  150. </div> 
  151. </div> 
  152. <span class="wpcf7-form-control-wrap wpcf7-form-control-signature-input-wrap %1$s"> 
  153. <input %2$s id="wpcf7_input_%1$s"/><input %9$s id="wpcf7_input_%1$s_attachment"/><input %10$s id="wpcf7_input_%1$s_inline"/>%3$s 
  154. </span> 
  155. ',  
  156. sanitize_html_class( $tag->name ), $atts, $validation_error, $tag->name, $width, $height, __( 'Clear', 'contact-form-7-signature-addon' ), $atts_canvas, $atts_attach, $atts_inline ); 
  157.  
  158. return $html; 
  159.  
  160. /** 
  161. * Signature validation 
  162. * @since 4.0.0 
  163. */ 
  164. public function signature_validation( $result, $tag ) { 
  165. $tag = new WPCF7_FormTag( $tag ); 
  166.  
  167. $name = $tag->name; 
  168.  
  169. $value = isset( $_POST[$name] ) 
  170. ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) 
  171. : ''; 
  172.  
  173. if ( 'signature*' == $tag->type ) { 
  174. if ( '' == $value ) { 
  175. if (method_exists($result, "invalidate")) { 
  176. $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); 
  177. return $result; 
  178. }else{ 
  179. $result['valid'] = false; 
  180. $result['reason'][$name] = wpcf7_get_message( 'invalid_required' ); 
  181.  
  182. if ( isset( $result['reason'][$name] ) && $id = $tag->get_id_option() ) { 
  183. $result['idref'][$name] = $id; 
  184.  
  185. return $result; 
  186.