Advertisement
towfiqi

cta-widget.php

May 8th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.74 KB | None | 0 0
  1. <?php
  2. /*
  3. *FRONTPAGE - TEXT SECTION WIDGET
  4. */
  5. add_action( 'widgets_init', 'optimizer_register_front_cta' );
  6.  
  7. /*
  8. * Register widget.
  9. */
  10. function optimizer_register_front_cta() {
  11. register_widget( 'optimizer_front_cta' );
  12. }
  13.  
  14.  
  15. /*
  16. * Widget class.
  17. */
  18. class optimizer_front_Cta extends WP_Widget {
  19.  
  20. /* ---------------------------- */
  21. /* -------- Widget setup -------- */
  22. /* ---------------------------- */
  23.  
  24.  
  25. function __construct() {
  26. if(is_customize_preview()){$widgetname = __( 'CTA', 'optimizer' ); }else{ $widgetname = __( '&diams; CTA Widget', 'optimizer' ); }
  27.  
  28. parent::__construct( 'optimizer_front_cta', $widgetname, array(
  29. 'classname' => 'optimizer_front_cta actionblck',
  30. 'description' => __( 'Optimizer Frontpage Call to Action Section widget', 'optimizer' ),
  31. 'customize_selective_refresh' => true,
  32. ) );
  33. $this->alt_option_name = 'optimizer_front_cta';
  34. add_action('wp_enqueue_scripts', array(&$this, 'front_cta_enqueue_css'));
  35. }
  36.  
  37.  
  38. /* ---------------------------- */
  39. /* ------- Display Widget -------- */
  40. /* ---------------------------- */
  41.  
  42. function widget( $args, $instance ) {
  43.  
  44.  
  45. extract( $args );
  46. /* Our variables from the widget settings. */
  47. $content = isset( $instance['content'] ) ? apply_filters( 'wp_editor_widget_content', $instance['content'] ) : __('Collaboratively administrate empowered markets via plug-and-play networks.','optimizer');
  48. $buttontxt = isset( $instance['buttontxt'] ) ? apply_filters('wp_editor_widget_content', $instance['buttontxt']) : __('DOWNLOAD NOW', 'optimizer');
  49. $buttonlink = isset( $instance['buttonlink'] ) ? esc_url($instance['buttonlink']) : '#';
  50. $buttonalign = isset( $instance['buttonalign'] ) ? apply_filters('widget_title', $instance['buttonalign']) : 'button_right';
  51. $buttonstyle = isset( $instance['buttonstyle'] ) ? apply_filters('widget_title', $instance['buttonstyle']) : 'button_flat';
  52.  
  53. $buttontxtcolor = isset( $instance['buttontxtcolor'] ) ? $instance['buttontxtcolor'] : '';
  54. $buttonbgcolor = isset( $instance['buttonbgcolor'] ) ? $instance['buttonbgcolor'] : '';
  55. $ctatxtcolor = isset( $instance['ctatxtcolor'] ) ? $instance['ctatxtcolor'] : '';
  56. $ctabgcolor = isset( $instance['ctabgcolor'] ) ? $instance['ctabgcolor'] : '';
  57. $ctabgimg = isset( $instance['ctabgimg'] ) ? esc_url($instance['ctabgimg']) : '';
  58.  
  59. /* Before widget (defined by themes). */
  60. echo $before_widget;
  61.  
  62. //Sitegorigin Builder FIX
  63. echo '<span class="so_widget_id" data-panel-id="'.$this->id.'"></span>';
  64. if(is_customize_preview()) echo '<span class="widgetname">'.$this->name.'</span>';
  65.  
  66. echo '<div class="home_action cta_'.$buttonalign.'"><div class="center">';
  67. //Make inline editable
  68. if(is_customize_preview()){ $id= $this->id; $controlid = 'data-optionid="widget-'.$id.'-content"';}else{ $controlid = '';}
  69.  
  70. echo '<div class="home_action_left tiny_content_editable" '.$controlid.'>'.do_shortcode( $content ).'</div>';
  71.  
  72. if ( $buttontxt ){
  73. $buttonlink = 'href="'.do_shortcode($buttonlink).'"';
  74. echo '<div class="home_action_right"><div class="home_action_button_wrap"><div class="home_action_button '.$buttonstyle.'"><a '.$buttonlink.'>'.do_shortcode($buttontxt).'</a></div></div></div>';
  75. }
  76.  
  77. echo '</div></div>';
  78.  
  79.  
  80. //Stylesheet-loaded in Customizer Only.
  81. if(is_customize_preview()){
  82. $id= $this->id;
  83.  
  84. $buttontxtcolor = 'color:#ffffff;';
  85. $buttonbgcolor = 'background-color:#db5a49;';
  86. $ctatxtcolor = 'color:#444444;';
  87. $ctabgcolor = 'background-color:#f5f5f5;';
  88. $ctabgimg = '';
  89. $marginTop =''; $marginBottom =''; $marginLeft =''; $marginRight ='';$calcWidth ='';
  90. $paddingTop =''; $paddingBottom =''; $paddingLeft =''; $paddingRight =''; $boxSizing='';
  91.  
  92. //Margin
  93. if ( ! empty( $instance['margin'] ) ) {
  94. if(!empty($instance['margin'][0])){ $marginTop ='margin-top:'.$instance['margin'][0].';';}
  95. if(!empty($instance['margin'][1])){ $marginBottom ='margin-bottom:'.$instance['margin'][1].';';}
  96. if(!empty($instance['margin'][2])){ $marginLeft ='margin-left:'.$instance['margin'][2].';';}
  97. if(!empty($instance['margin'][3])){ $marginRight ='margin-right:'.$instance['margin'][3].';';}
  98.  
  99. //Width
  100. $thewidth ='100';
  101. $leftrightmargin ='0px';
  102.  
  103. if ( ! empty( $instance['width']) ) { if($instance['width'] == 2){ $thewidth = '50';} if($instance['width'] == 3){ $thewidth = '33.33';} if($instance['width'] == 4){ $thewidth = '66.67';} }
  104. if ( ! empty( $instance['width']) && !empty($instance['margin'][2] ) ) { $leftrightmargin = $instance['margin'][2]; }
  105. if ( ! empty( $instance['width']) && !empty($instance['margin'][3] ) ) { $leftrightmargin = $instance['margin'][3]; }
  106.  
  107. if ( ! empty( $instance['width']) ) {
  108. if(!empty($instance['margin'][2]) && !empty($instance['margin'][3]) ){
  109. $leftrightmargin = '('.$instance['margin'][2].' + '.$instance['margin'][3].')';
  110. }
  111. }
  112. $calcWidth ='width: calc(100% - '.$leftrightmargin.')!important;';
  113.  
  114. }
  115.  
  116. //Padding
  117. if ( ! empty( $instance['padding'] ) ) {
  118. if(!empty($instance['padding'][0])){ $paddingTop ='padding-top:'.$instance['padding'][0].';';}
  119. if(!empty($instance['padding'][1])){ $paddingBottom ='padding-bottom:'.$instance['padding'][1].';';}
  120. if(!empty($instance['padding'][2])){ $paddingLeft ='padding-left:'.$instance['padding'][2].';';}
  121. if(!empty($instance['padding'][3])){ $paddingRight ='padding-right:'.$instance['padding'][3].';';}
  122.  
  123. $boxSizing='box-sizing:border-box;';
  124.  
  125. }
  126.  
  127.  
  128. if ( ! empty( $instance['buttontxtcolor'] ) ) { $buttontxtcolor = 'color: ' . $instance['buttontxtcolor'] . '; ';}
  129. if ( ! empty( $instance['ctatxtcolor'] ) ) { $ctatxtcolor = 'color: ' . $instance['ctatxtcolor'] . '; ';}
  130. if ( ! empty( $instance['buttonbgcolor'] ) ) { $buttonbgcolor = 'background-color: ' . $instance['buttonbgcolor'] . '; ';}
  131. if ( ! empty( $instance['ctabgcolor'] ) ) { $ctabgcolor = 'background-color: ' . $instance['ctabgcolor'] . '; ';}
  132. if ( ! empty( $instance['ctabgimg'] ) ) { $ctabgimg = 'background-image: url(' . $instance['ctabgimg'] . '); ';}
  133. if ( ! empty( $instance['buttontxtcolor'] ) ) { $hollhowborder = 'border-color: ' . $instance['buttontxtcolor'] . '; ';}else{ $hollhowborder = ''; }
  134. if ( ! empty( $instance['buttonbgcolor'] ) ) { $roundedborder = 'border-color: ' . $instance['buttonbgcolor'] . '; ';}else{ $roundedborder = ''; }
  135.  
  136.  
  137. echo '<style>#'.$id.' .home_action{ ' . $ctabgcolor . '' . $ctabgimg . ''.$ctatxtcolor. '} #'.$id.' .home_action_button a{ ' . $buttontxtcolor . '}#'.$id.' .home_action_button.button_hollow{ ' . $hollhowborder . '}#'.$id.' .button_flat, #'.$id.' .button_rounded{' . $buttontxtcolor . '' . $buttonbgcolor . ' ' . $roundedborder . '} @media screen and (min-width: 480px){#'.$id.' .home_action{'.$marginTop.$marginBottom.$marginLeft.$marginRight.$calcWidth. $paddingTop.$paddingBottom.$paddingLeft.$paddingRight. $boxSizing.'} }</style>';
  138.  
  139. }
  140.  
  141. /* After widget (defined by themes). */
  142. echo $after_widget;
  143.  
  144. }
  145.  
  146.  
  147. /* ---------------------------- */
  148. /* ------- Update Widget -------- */
  149. /* ---------------------------- */
  150.  
  151. function update( $new_instance, $old_instance ) {
  152. $instance = $old_instance;
  153.  
  154. /* No need to strip tags */
  155. $instance['content'] = wp_kses_post($new_instance['content']);
  156. $instance['buttontxt'] = strip_tags($new_instance['buttontxt']);
  157. $instance['buttonlink'] = esc_url_raw($new_instance['buttonlink']);
  158. $instance['buttonalign'] = strip_tags($new_instance['buttonalign']);
  159. $instance['buttonstyle'] = strip_tags($new_instance['buttonstyle']);
  160.  
  161. $instance['buttontxtcolor'] = optimizer_sanitize_hex($new_instance['buttontxtcolor']);
  162. $instance['buttonbgcolor'] = optimizer_sanitize_hex($new_instance['buttonbgcolor']);
  163. $instance['ctatxtcolor'] = optimizer_sanitize_hex($new_instance['ctatxtcolor']);
  164. $instance['ctabgcolor'] = optimizer_sanitize_hex($new_instance['ctabgcolor']);
  165. $instance['ctabgimg'] = esc_url_raw($new_instance['ctabgimg']);
  166.  
  167. return $instance;
  168. }
  169.  
  170. /* ---------------------------- */
  171. /* ------- Widget Settings ------- */
  172. /* ---------------------------- */
  173.  
  174. /**
  175. * Displays the widget settings controls on the widget panel.
  176. * Make use of the get_field_id() and get_field_name() function
  177. * when creating your form elements. This handles the confusing stuff.
  178. */
  179.  
  180. function form( $instance ) {
  181.  
  182. /* Set up some default widget settings. */
  183. $defaults = array(
  184. 'content' => __('Collaboratively administrate empowered markets via plug-and-play networks.','optimizer'),
  185. 'buttontxt' => __('DOWNLOAD NOW','optimizer'),
  186. 'buttonlink' => '#',
  187. 'buttonalign' => 'button_right',
  188. 'buttonstyle' => 'button_flat',
  189. 'buttontxtcolor' => '#ffffff',
  190. 'buttonbgcolor' => '#db5a49',
  191. 'ctatxtcolor' => '#444444',
  192. 'ctabgcolor' => '#f5f5f5',
  193. 'ctabgimg' => '',
  194. );
  195. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  196.  
  197.  
  198. <!-- Text Content Field -->
  199. <p>
  200. <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e('CTA Content:', 'optimizer') ?></label>
  201. <input class="widefat" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" value="<?php echo esc_attr($instance['content']); ?>" type="hidden" />
  202. <a href="javascript:WPEditorWidget.showEditor('<?php echo $this->get_field_id( 'content' ); ?>');" class="button edit-content-button"><?php _e( 'Edit content', 'optimizer' ) ?></a>
  203. </p>
  204.  
  205. <!-- CTA Button TEXT Field -->
  206. <p>
  207. <label for="<?php echo $this->get_field_id( 'buttontxt' ); ?>"><?php _e('Button Text:', 'optimizer') ?></label>
  208. <input class="widefat" id="<?php echo $this->get_field_id( 'buttontxt' ); ?>" name="<?php echo $this->get_field_name( 'buttontxt' ); ?>" value="<?php echo htmlspecialchars($instance['buttontxt'], ENT_QUOTES, "UTF-8"); ?>" type="text" />
  209. </p>
  210.  
  211.  
  212. <!-- CTA Button Link Field -->
  213. <p>
  214. <label for="<?php echo $this->get_field_id( 'buttonlink' ); ?>"><?php _e('Button Link:', 'optimizer') ?></label>
  215. <input class="widefat" id="<?php echo $this->get_field_id( 'buttonlink' ); ?>" name="<?php echo $this->get_field_name( 'buttonlink' ); ?>" value="<?php echo esc_url($instance['buttonlink']); ?>" type="text" />
  216. </p>
  217.  
  218. <!-- CTA Button Align Field -->
  219. <p>
  220. <label for="<?php echo $this->get_field_id( 'buttonalign' ); ?>"><?php _e('Button Alignment:', 'optimizer') ?></label>
  221. <select id="<?php echo $this->get_field_id( 'buttonalign' ); ?>" name="<?php echo $this->get_field_name( 'buttonalign' ); ?>" class="widefat">
  222. <option value="button_left" <?php if ( 'button_left' == $instance['buttonalign'] ) echo 'selected="selected"'; ?>><?php _e('Left', 'optimizer') ?></option>
  223. <option value="button_right" <?php if ( 'button_right' == $instance['buttonalign'] ) echo 'selected="selected"'; ?>><?php _e('Right', 'optimizer') ?></option>
  224. <option value="button_center" <?php if ( 'button_center' == $instance['buttonalign'] ) echo 'selected="selected"'; ?>><?php _e('Center', 'optimizer') ?></option>
  225. </select>
  226. </p>
  227.  
  228. <!-- CTA Button Style Field -->
  229. <p>
  230. <label for="<?php echo $this->get_field_id( 'buttonstyle' ); ?>"><?php _e('Button Style:', 'optimizer') ?></label>
  231. <select id="<?php echo $this->get_field_id( 'buttonstyle' ); ?>" name="<?php echo $this->get_field_name( 'buttonstyle' ); ?>" class="widefat">
  232. <option value="button_flat" <?php if ( 'button_flat' == $instance['buttonstyle'] ) echo 'selected="selected"'; ?>><?php _e('Flat', 'optimizer') ?></option>
  233. <option value="button_hollow" <?php if ( 'button_hollow' == $instance['buttonstyle'] ) echo 'selected="selected"'; ?>><?php _e('Hollow', 'optimizer') ?></option>
  234. <option value="button_rounded" <?php if ( 'button_rounded' == $instance['buttonstyle'] ) echo 'selected="selected"'; ?>><?php _e('Rounded', 'optimizer') ?></option>
  235. </select>
  236. </p>
  237.  
  238.  
  239. <!-- CTA BUTTON Text Color Field -->
  240. <p>
  241. <label for="<?php echo $this->get_field_id( 'buttontxtcolor' ); ?>"><?php _e('Button Text Color', 'optimizer') ?></label>
  242. <input class="widefat color-picker" id="<?php echo $this->get_field_id( 'buttontxtcolor' ); ?>" name="<?php echo $this->get_field_name( 'buttontxtcolor' ); ?>" value="<?php echo $instance['buttontxtcolor']; ?>" type="text" />
  243. </p>
  244.  
  245. <!-- CTA BUTTON Background Color Field -->
  246. <p>
  247. <label for="<?php echo $this->get_field_id( 'buttonbgcolor' ); ?>"><?php _e('Button Background Color', 'optimizer') ?></label>
  248. <input class="widefat color-picker" id="<?php echo $this->get_field_id( 'buttonbgcolor' ); ?>" name="<?php echo $this->get_field_name( 'buttonbgcolor' ); ?>" value="<?php echo $instance['buttonbgcolor']; ?>" type="text" />
  249. </p>
  250.  
  251.  
  252. <!-- CTA Section Text Color Field -->
  253. <p>
  254. <label for="<?php echo $this->get_field_id( 'ctatxtcolor' ); ?>"><?php _e('CTA Section Text Color', 'optimizer') ?></label>
  255. <input class="widefat color-picker" id="<?php echo $this->get_field_id( 'ctatxtcolor' ); ?>" name="<?php echo $this->get_field_name( 'ctatxtcolor' ); ?>" value="<?php echo $instance['ctatxtcolor']; ?>" type="text" />
  256. </p>
  257.  
  258. <!-- CTA Section Background Color Field -->
  259. <p>
  260. <label for="<?php echo $this->get_field_id( 'ctabgcolor' ); ?>"><?php _e('CTA Section Background Color', 'optimizer') ?></label>
  261. <input class="widefat color-picker" id="<?php echo $this->get_field_id( 'ctabgcolor' ); ?>" name="<?php echo $this->get_field_name( 'ctabgcolor' ); ?>" value="<?php echo $instance['ctabgcolor']; ?>" type="text" />
  262. </p>
  263.  
  264. <!-- CTA Section Background Image Field -->
  265. <p>
  266. <label for="<?php echo $this->get_field_id( 'ctabgimg' ); ?>"><?php _e('CTA Section Background Image', 'optimizer') ?></label>
  267. <div class="media-picker-wrap">
  268. <?php if(!empty($instance['ctabgimg'])) { ?>
  269. <img style="max-width:100%; height:auto;" class="media-picker-preview" src="<?php echo esc_url($instance['ctabgimg']); ?>" />
  270. <i class="fa fa-times media-picker-remove"></i>
  271. <?php } ?>
  272. <input class="widefat media-picker" id="<?php echo $this->get_field_id( 'ctabgimg' ); ?>" name="<?php echo $this->get_field_name( 'ctabgimg' ); ?>" value="<?php echo esc_url($instance['ctabgimg']); ?>" type="hidden" />
  273. <a class="media-picker-button button" onclick="mediaPicker(this.id)" id="<?php echo $this->get_field_id( 'ctabgimg' ).'mpick'; ?>"><?php _e('Select Image', 'optimizer') ?></a>
  274. </div>
  275. </p>
  276. <?php
  277. }
  278. //ENQUEUE CSS
  279. function front_cta_enqueue_css() {
  280. $settings = $this->get_settings();
  281. if(!is_customize_preview()){
  282. if ( empty( $settings ) ) {
  283. return;
  284. }
  285.  
  286. foreach ( $settings as $instance_id => $instance ) {
  287. $id = $this->id_base . '-' . $instance_id;
  288.  
  289. if ( ! is_active_widget( false, $id, $this->id_base ) ) {
  290. continue;
  291. }
  292.  
  293. $buttontxtcolor = 'color:#ffffff;';
  294. $buttonbgcolor = 'background-color:#db5a49;';
  295. $ctatxtcolor = 'color:#444444;';
  296. $ctabgcolor = 'background-color:#f5f5f5;';
  297. $ctabgimg = '';
  298. $marginTop =''; $marginBottom =''; $marginLeft =''; $marginRight ='';$calcWidth ='';
  299. $paddingTop =''; $paddingBottom =''; $paddingLeft =''; $paddingRight =''; $boxSizing='';
  300.  
  301. //Margin
  302. if ( ! empty( $instance['margin'] ) ) {
  303. if(!empty($instance['margin'][0])){ $marginTop ='margin-top:'.$instance['margin'][0].';';}
  304. if(!empty($instance['margin'][1])){ $marginBottom ='margin-bottom:'.$instance['margin'][1].';';}
  305. if(!empty($instance['margin'][2])){ $marginLeft ='margin-left:'.$instance['margin'][2].';';}
  306. if(!empty($instance['margin'][3])){ $marginRight ='margin-right:'.$instance['margin'][3].';';}
  307.  
  308. //Width
  309. $thewidth ='100';
  310. $leftrightmargin ='0px';
  311.  
  312. if ( ! empty( $instance['width']) ) { if($instance['width'] == 2){ $thewidth = '50';} if($instance['width'] == 3){ $thewidth = '33.33';} if($instance['width'] == 4){ $thewidth = '66.67';} }
  313. if ( ! empty( $instance['width']) && !empty($instance['margin'][2] ) ) { $leftrightmargin = $instance['margin'][2]; }
  314. if ( ! empty( $instance['width']) && !empty($instance['margin'][3] ) ) { $leftrightmargin = $instance['margin'][3]; }
  315.  
  316. if ( ! empty( $instance['width']) ) {
  317. if(!empty($instance['margin'][2]) && !empty($instance['margin'][3]) ){
  318. $leftrightmargin = '('.$instance['margin'][2].' + '.$instance['margin'][3].')';
  319. }
  320. }
  321. $calcWidth ='width: calc(100% - '.$leftrightmargin.')!important;';
  322.  
  323. }
  324.  
  325. //Padding
  326. if ( ! empty( $instance['padding'] ) ) {
  327. if(!empty($instance['padding'][0])){ $paddingTop ='padding-top:'.$instance['padding'][0].';';}
  328. if(!empty($instance['padding'][1])){ $paddingBottom ='padding-bottom:'.$instance['padding'][1].';';}
  329. if(!empty($instance['padding'][2])){ $paddingLeft ='padding-left:'.$instance['padding'][2].';';}
  330. if(!empty($instance['padding'][3])){ $paddingRight ='padding-right:'.$instance['padding'][3].';';}
  331.  
  332. $boxSizing='box-sizing:border-box;';
  333.  
  334. }
  335.  
  336. if ( ! empty( $instance['buttontxtcolor'] ) ) {
  337. $buttontxtcolor = 'color: ' . $instance['buttontxtcolor'] . '; ';
  338. }
  339. if ( ! empty( $instance['ctatxtcolor'] ) ) {
  340. $ctatxtcolor = 'color: ' . $instance['ctatxtcolor'] . '; ';
  341. }
  342. if ( ! empty( $instance['buttonbgcolor'] ) ) {
  343. $buttonbgcolor = 'background-color: ' . $instance['buttonbgcolor'] . '; ';
  344. }
  345. if ( ! empty( $instance['ctabgcolor'] ) ) {
  346. $ctabgcolor = 'background-color: ' . $instance['ctabgcolor'] . '; ';
  347. }
  348. if ( ! empty( $instance['ctabgimg'] ) ) {
  349. $ctabgimg = 'background-image: url(' . $instance['ctabgimg'] . '); ';
  350. }
  351.  
  352. if ( ! empty( $instance['buttontxtcolor'] ) ) {
  353. $hollhowborder = 'border-color: ' . $instance['buttontxtcolor'] . '; ';
  354. }else{ $hollhowborder = ''; }
  355. if ( ! empty( $instance['buttonbgcolor'] ) ) {
  356. $roundedborder = 'border-color: ' . $instance['buttonbgcolor'] . '; ';
  357. }else{ $roundedborder = ''; }
  358.  
  359.  
  360. $widget_style = '#'.$id.' .home_action{ ' . $ctabgcolor . '' . $ctabgimg . ''.$ctatxtcolor. '} #'.$id.' .home_action_button a{ ' . $buttontxtcolor . '}#'.$id.' .home_action_button.button_hollow{ ' . $hollhowborder . '}#'.$id.' .button_flat, #'.$id.' .button_rounded{' . $buttontxtcolor . '' . $buttonbgcolor . ' ' . $roundedborder . '} @media screen and (min-width: 480px){#'.$id.' .home_action{'.$marginTop.$marginBottom.$marginLeft.$marginRight. $paddingTop.$paddingBottom.$paddingLeft.$paddingRight. $boxSizing.$calcWidth.'} }';
  361. wp_add_inline_style( 'optimizer-style', $widget_style );
  362.  
  363. }
  364. }
  365. }
  366. }
  367. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement