cf7_style_meta_boxes

The Class for creating all of the meta boxes.

Defined (1)

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

/cf7-style-meta-box.php  
  1. class cf7_style_meta_boxes { 
  2.  
  3. /** 
  4. * Hook into the appropriate actions when the class is constructed. 
  5. */ 
  6. public function __construct() { 
  7. //selector init 
  8. add_action( 'add_meta_boxes', array( $this, 'add_meta_box_style_selector' ) ); 
  9. add_action( 'save_post', array( $this, 'save_style_selector' ) ); 
  10. //fonts 
  11. add_action( 'add_meta_boxes', array( $this, 'add_meta_box_font_selector' ) ); 
  12. add_action( 'save_post', array( $this, 'save_font_id' ) ); 
  13. //image meta box init 
  14. add_action( 'add_meta_boxes', array( $this, 'add_meta_box_style_image' ) ); 
  15. //add paypal button 
  16. add_action( 'add_meta_boxes', array( $this, 'add_meta_box_style_paypal' ) ); 
  17. //custom style meta box1 
  18. add_action( 'add_meta_boxes', array( $this, 'add_meta_box_style_customizer' ), 10, 2 ); 
  19. add_action( 'save_post', array( $this, 'save_style_customizer' ) ); 
  20.  
  21. /************************************************** 
  22. * Adds the meta box container for style selector. 
  23. * STYLE SELECTOR STARTS HERE 
  24. */ 
  25. public function add_meta_box_style_selector( $post_type ) { 
  26. $post_types = array('cf7_style'); //limit meta box to certain post types 
  27. if ( in_array( $post_type, $post_types )) { 
  28. add_meta_box( 
  29. 'cf7_style_meta_box_form_selector' 
  30. , __( 'Select forms for current style', 'contact-form-7-style' ) 
  31. , array( $this, 'render_meta_box_selector' ) 
  32. , $post_type 
  33. , 'advanced' 
  34. , 'high' 
  35. ); 
  36. public function add_meta_box_style_customizer( $post_type, $post ) { 
  37.  
  38. $post_types = array('cf7_style'); //limit meta box to certain post types 
  39. $custom_cat = get_the_terms( $post->ID, "style_category" ); 
  40. $custom_name = ( empty( $custom_cat ) ) ? "custom style" : $custom_cat[0]->name; 
  41. if ( in_array( $post_type, $post_types ) && ( $custom_name == "custom style" ) ) { 
  42. add_meta_box( 
  43. 'cf7_style_meta_box_style_customizer' 
  44. , __( 'Custom style settings', 'contact-form-7-style' ) 
  45. , array( $this, 'render_meta_box_style_customizer' ) 
  46. , $post_type 
  47. , 'advanced' 
  48. , 'high' 
  49. ); 
  50.  
  51. public function render_meta_box_style_customizer( $post ) { 
  52. require_once WPCF7S_PLUGIN_DIR . '/options.php'; 
  53.  
  54. public function save_style_customizer( $post_id ) { 
  55.  
  56. if ( ! isset( $_POST['cf_7_style_customizer_custom_box_nonce'] ) ) 
  57. return $post_id; 
  58.  
  59. $nonce = sanitize_text_field($_POST['cf_7_style_customizer_custom_box_nonce']); 
  60.  
  61.  
  62. // Verify that the nonce is valid. 
  63. if ( ! wp_verify_nonce( $nonce, 'cf_7_style_style_customizer_inner_custom_box' ) ) { 
  64. return $post_id; 
  65.  
  66. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  67. return $post_id; 
  68.  
  69. if ( 'page' == $_POST['post_type'] ) { 
  70. if ( ! current_user_can( 'edit_page', $post_id ) ) { 
  71. return $post_id; 
  72. } else { 
  73. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  74. return $post_id; 
  75.  
  76.  
  77. $posted_data = $_POST['cf7styleallvalues']; 
  78. $posted_data = str_replace("\'", '"', $posted_data); 
  79. $posted_data = json_decode($posted_data, true); 
  80. $active_pane = sanitize_text_field($_POST['cf7styleactivepane']); 
  81. if( isset( $active_pane )) { 
  82. update_post_meta( $post_id, 'cf7_style_active_panel', $active_pane, ""); 
  83. if ( is_array( $posted_data ) && isset( $posted_data ) ) { 
  84. update_post_meta( $post_id, 'cf7_style_custom_styler', $posted_data , ""); 
  85.  
  86.  
  87. /** 
  88. * Save the style selector when the post is saved. 
  89. */ 
  90. public function save_style_selector( $post_id ) { 
  91. if ( ! isset( $_POST['cf_7_style_selector_custom_box_nonce'] ) ) 
  92. return $post_id; 
  93.  
  94. $nonce = $_POST['cf_7_style_selector_custom_box_nonce']; 
  95.  
  96. // Verify that the nonce is valid. 
  97. if ( ! wp_verify_nonce( $nonce, 'cf_7_style_selector_inner_custom_box' ) ) { 
  98. return $post_id; 
  99.  
  100. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  101. return $post_id; 
  102.  
  103. if ( 'page' == $_POST['post_type'] ) { 
  104. if ( ! current_user_can( 'edit_page', $post_id ) ) { 
  105. return $post_id; 
  106. } else { 
  107. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  108. return $post_id; 
  109.  
  110.  
  111. //getting all the cf7 forms 
  112. $cf7formsargs = array( 
  113. 'post_type' => 'wpcf7_contact_form',  
  114. 'posts_per_page' => -1 
  115. ); 
  116. $cf7forms = get_posts( $cf7formsargs ); 
  117.  
  118. //for each checked form, saving the style id 
  119. foreach ( $cf7forms as $cf7form ) { 
  120. if ( isset( $_POST[$cf7form->post_name] ) ) { 
  121. //if ( !empty( $_POST[$cf7form->post_name] ) ) { 
  122. update_post_meta( $cf7form->ID, 'cf7_style_id', $post_id); 
  123. //}  
  124. } else { 
  125. $getthisstyle = get_post_meta( $cf7form->ID, 'cf7_style_id', $post_id ); 
  126.  
  127. if ( !empty( $getthisstyle ) && $post_id == $getthisstyle ) { 
  128. update_post_meta( $cf7form->ID, 'cf7_style_id', '' ); 
  129.  
  130. if ( !empty( $getthisstyle ) ) { 
  131. //update_post_meta( $cf7form->ID, 'cf7_style_id', $getthisstyle ); 
  132.  
  133. /** 
  134. * Render Meta Box content. 
  135. */ 
  136. public function render_meta_box_selector( $post ) { 
  137. wp_nonce_field( 'cf_7_style_selector_inner_custom_box', 'cf_7_style_selector_custom_box_nonce' ); 
  138.  
  139. // Display the form, using the current value. 
  140. $args = array( 
  141. 'post_type' => 'wpcf7_contact_form',  
  142. 'posts_per_page' => -1 
  143. ); 
  144. $currentpostid = get_the_ID(); 
  145.  
  146. $query = new WP_Query( $args ); 
  147. echo '<table class="wp-list-table fixed pages widefat">';  
  148. echo '<thead>'; 
  149. echo '<tr>'; 
  150. echo '<th class="manage-column">' . __('Contact form 7 forms', 'contact-form-7-style' ) . '</th>'; 
  151. echo '<th class="manage-column different-style"><input type="checkbox" id="select_all"/><label for="select_all">' . __('Select all', 'contact-form-7-style' ) . '</label></th>'; 
  152. echo '<th class="generate-preview-option">' . __( "Generate preview", 'contact-form-7-style' ) . '</th>'; 
  153. echo '</tr>'; 
  154. echo '</thead>'; 
  155. echo '<tbody class="cf7style_body_select_all">'; 
  156. if ( $query->have_posts() ) { 
  157. while ( $query->have_posts() ) : $query->the_post();  
  158. $cf7stylehas = get_post_meta( get_the_ID(), 'cf7_style_id', true );  
  159. ?> 
  160. <tr> 
  161. <td> 
  162. <label for="<?php echo cf7_style_the_slug(); ?>"><?php the_title(); ?></label> 
  163. </td> 
  164. <td> 
  165. <input type="checkbox" id="<?php echo cf7_style_the_slug(); ?>" name="<?php echo cf7_style_the_slug(); ?>" value="<?php echo get_the_ID(); ?>" <?php if ( $currentpostid == $cf7stylehas ) { echo 'checked'; } ?> /> 
  166. </td> 
  167. <td class="generate-preview-option"> 
  168. <button class="button-primary generate-preview-button" data-attr-title="<?php the_title(); ?>" data-attr-id="<?php the_ID(); ?>"><?php _e( "Generate preview", 'contact-form-7-style' ); ?></button> 
  169. </td> 
  170. </tr> 
  171. <tr> 
  172. <td colspan="2"> 
  173. <?php if ( $currentpostid != $cf7stylehas && !empty( $cf7stylehas ) ) { 
  174. echo '<p class="description">' . __('Notice: This form allready has a selected style. Checking this one will overwrite the ', 'contact-form-7-style' ) . '<a href="' . get_admin_url() . 'post.php?post_type=cf7_style&post=' . $cf7stylehas . '&action=edit">' . __('other one.', 'contact-form-7-style' ) . '</a></p>';  
  175. } ?>  
  176. </td> 
  177. <td class="generate-preview-option"> </td> 
  178. </tr> 
  179. <?php endwhile; wp_reset_postdata(); 
  180. echo '</tbody>'; 
  181. echo '</table>'; 
  182. } else { 
  183. echo '<tr><td><p class="description">' . __( 'Please create a form. You can do it by clicking', 'contact-form-7-style' ) . '<a href="' . admin_url() . 'admin.php?page=wpcf7-new" target="_blank">' . __(' here', 'contact-form-7-style' ) . '</a></p></td></tr></table>'; 
  184. /** 
  185. *STYLE SELECTOR ENDS HERE 
  186. ***************************** 
  187. */ 
  188.  
  189. /************************************************* 
  190. * Adds the meta box container for IMAGE PREVIEW 
  191. * IMAGE META BOX STARTS HERE 
  192. */ 
  193. public function add_meta_box_style_image( $post_type ) { 
  194. $post_types = array('cf7_style'); //limit meta box to certain post types 
  195. if ( in_array( $post_type, $post_types )) { 
  196. add_meta_box( 
  197. 'cf7_style_meta_box_image' 
  198. , __( 'Preview', 'contact-form-7-style' ) 
  199. , array( $this, 'render_meta_box_image' ) 
  200. , $post_type 
  201. , 'side' 
  202. , 'high' 
  203. ); 
  204. /** 
  205. * renders the image 
  206. */ 
  207. public function render_meta_box_image( $post ) { 
  208. $image = get_post_meta( $post->ID, 'cf7_style_image_preview', true ); 
  209. if ( !empty( $image ) ) { 
  210. echo '<img src="' . plugins_url() . '/contact-form-7-style' . $image . '" alt="' . $post->title . '" />'; 
  211. } else { 
  212. //here will be the placeholder in case the image is not available 
  213. $image = 'default_form.jpg'; 
  214. echo '<img src="' . plugins_url() . '/contact-form-7-style/images/' . $image . '" alt="' . $post->title . '" />'; 
  215.  
  216. /** 
  217. *IMAGE META BOX ENDS HERE 
  218. *************************** 
  219. */ 
  220. /** 
  221. * Meta box for font selector 
  222. */ 
  223. public function add_meta_box_font_selector( $post_type ) { 
  224. $post_types = array('cf7_style'); //limit meta box to certain post types 
  225. if ( in_array( $post_type, $post_types )) { 
  226. add_meta_box( 
  227. 'cf7_style_meta_box_font_selector' 
  228. , __( 'Select a Google Font', 'contact-form-7-style' ) 
  229. , array( $this, 'render_font_selector' ) 
  230. , $post_type 
  231. , 'advanced' 
  232. , 'high' 
  233. ); 
  234. public function render_font_selector( $post ) { 
  235. wp_nonce_field( 'cf_7_style_font_inner_custom_box', 'cf_7_style_font_custom_box_nonce' ); 
  236. //getting all google fonts 
  237. $google_list = wp_remote_get( 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBAympIKDNKmfxhI3udY-U_9vDWSdfHrEo' ); 
  238. $response = wp_remote_retrieve_body( $google_list ); 
  239.  
  240. $font_obj = json_decode( $response ); 
  241. $cf7_style_font = get_post_meta( $post->ID, 'cf7_style_font', true ); 
  242. $selected = ''; 
  243. echo '<select name="cf7_style_font_selector">'; 
  244. echo '<option value="none">'.__( 'None', 'contact-form-7-style' ).'</option>'; 
  245. foreach ( $font_obj->items as $font) { 
  246. echo '<option value="' . $font->family . '"' . ( $cf7_style_font == $font->family ? 'selected="selected"' : '' ) . '>' . $font->family . '</option>'; 
  247. echo '</select>'; ?> 
  248. <div class="cf7-style preview-zone"> 
  249. <h4><?php _e( "Preview Selected font:", "contact-form-7-style" ) ?></h4> 
  250. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rhoncus ultrices neque sit amet consequat. Aenean facilisis massa convallis nisl viverra eleifend. Nam fermentum mauris eu eleifend posuere.</p> 
  251. </div> 
  252. <div class="clear"></div> 
  253. <?php 
  254.  
  255. /** 
  256. * Save the font id 
  257. */ 
  258. public function save_font_id( $post_id ) { 
  259. if ( ! isset( $_POST['cf_7_style_font_custom_box_nonce'] ) ) 
  260. return $post_id; 
  261.  
  262. $nonce = $_POST['cf_7_style_font_custom_box_nonce']; 
  263.  
  264. if ( ! wp_verify_nonce( $nonce, 'cf_7_style_font_inner_custom_box' ) ) { 
  265. return $post_id; 
  266.  
  267. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  268. return $post_id; 
  269.  
  270. if ( 'page' == $_POST['post_type'] ) { 
  271. if ( ! current_user_can( 'edit_page', $post_id ) ) { 
  272. return $post_id; 
  273. } else { 
  274. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  275. return $post_id; 
  276.  
  277.  
  278. if ( isset ( $_POST['cf7_style_font_selector'] ) ) { 
  279. update_post_meta( $post_id, 'cf7_style_font', sanitize_text_field( $_POST['cf7_style_font_selector'] )); 
  280. /************************************************* 
  281. * Adds the meta box container for IMAGE PREVIEW 
  282. * IMAGE META BOX STARTS HERE 
  283. */ 
  284. public function add_meta_box_style_paypal( $post_type ) { 
  285. $post_types = array('cf7_style'); //limit meta box to certain post types 
  286. if ( in_array( $post_type, $post_types )) { 
  287. add_meta_box( 
  288. 'cf7_style_meta_box_paypal' 
  289. , __( 'Donate', 'contact-form-7-style' ) 
  290. , array( $this, 'render_meta_paypal' ) 
  291. , $post_type 
  292. , 'side' 
  293. , 'high' 
  294. ); 
  295. /** 
  296. * renders the image 
  297. */ 
  298. public function render_meta_paypal( $post ) { ?> 
  299. <p><?php _e( "Your donation will motivate us to work more and improve this plugin.", "contact-form-7-style" ); ?></p> 
  300. <a href="http://cf7style.com/back-this-project/" target="_blank"> 
  301. <img src="<?php echo WPCF7S_LOCATION;?>admin/images/paypal.svg"> 
  302. </a> 
  303. <?php }