Advertisement
jmlapam

Untitled

Feb 6th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Rich Contact Widget
  4. Plugin URI: http://remyperona.fr/rich-contact-widget/
  5. Description: A simple contact widget enhanced with microdatas & microformats tags
  6. Version: 1.2
  7. Author: Rémy Perona
  8. Author URI: http://remyperona.fr
  9. License: GPL2
  10. Text Domain: rich-contact-widget
  11. Copyright 2012 Rémy Perona (email : remperona@gmail.com)
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License, version 2, as
  15. published by the Free Software Foundation.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * Class for vcard creation & saving
  28. */
  29. class VCF {
  30. var $data;
  31. var $name;
  32. function VCF($data) {
  33. $this->name = strtolower( remove_accents( trim( str_replace( ' ', '', $data['name'] ) ) ) );
  34. $this->data = "BEGIN:VCARD\nVERSION:3.0\nREV:".date("Ymd\THis\Z")."\nFN:".$data['name']."\nTITLE:".$data['activity']."\nADR;WORK:;;".$data['address'].";".$data['city'].";;".$data['postal_code'].";".$data['country']."\nTEL;WORK;VOICE:".$data['phone']."\nEMAIL;WORK;INTERNET:".$data['email']."\nURL;WORK:". home_url() ."\nEND:VCARD";
  35. }
  36.  
  37. function save() {
  38. $url = wp_nonce_url('widgets.php?editwidget=rc_widget-2','rich-contact-widget');
  39. if (false === ($creds = request_filesystem_credentials($url, '', false, false, null) ) ) {
  40. return; // stop processing here
  41. }
  42. if ( ! WP_Filesystem($creds) ) {
  43. request_filesystem_credentials($url, '', true, false, null);
  44. return;
  45. }
  46. $upload_dir = wp_upload_dir();
  47. global $wp_filesystem;
  48. $wp_filesystem->put_contents(
  49. $upload_dir['basedir'] . '/'. $this->name . '.vcf',
  50. $this->data
  51. );
  52. }
  53. }
  54.  
  55. /**
  56. * Adds RC_Widget widget.
  57. */
  58. class RC_Widget extends WP_Widget {
  59.  
  60. /**
  61. * Array containing the keys for each value of the contact fields
  62. */
  63. public function widget_keys() {
  64. $widget_keys = apply_filters( 'rc_widget_keys', array(
  65. 'title',
  66. 'type',
  67. 'name',
  68. 'activity',
  69. 'address',
  70. 'state',
  71. 'postal_code',
  72. 'city',
  73. 'country',
  74. 'phone',
  75. 'email',
  76. 'map',
  77. 'map_width',
  78. 'map_height',
  79. 'vcf'
  80. )
  81. );
  82. return $widget_keys;
  83. }
  84.  
  85. /**
  86. * Register widget with WordPress.
  87. */
  88. public function __construct() {
  89. parent::__construct(
  90. 'rc_widget', // Base ID
  91. 'Rich Contact Widget', // Name
  92. array( 'description' => __( 'A contact widget enhanced with microdatas & microformats tags', 'rich-contact-widget' ), ) // Args
  93. );
  94. }
  95.  
  96. /**
  97. * Front-end display of widget.
  98. *
  99. * @see WP_Widget::widget()
  100. *
  101. * @param array $args Widget arguments.
  102. * @param array $instance Saved values from database.
  103. */
  104. public function widget( $args, $instance ) {
  105. extract( $args );
  106. $title = apply_filters( 'widget_title', $instance['title'] );
  107.  
  108. echo $before_widget;
  109. if ( ! empty( $title ) )
  110. echo $before_title . $title . $after_title;
  111. if ( $instance['type'] == 'person' ) {
  112. $type = 'Person';
  113. $activity = 'jobTitle';
  114. $org = '';
  115. } else {
  116. $type = apply_filters( 'rc_widget_type', 'Corporation' );
  117. $activity = 'description';
  118. $org = ' org';
  119. }
  120.  
  121. if ( $instance['map'] == 1 ) {
  122. if (get_locale() == 'en_US')
  123. $map_adress = $instance['address'] . ' ' . $instance['city'] . ' ' . $instance['state'] . ' ' . $instance['postal_code'] . ' ' . $instance['country'];
  124. else
  125. $map_adress = $instance['address'] . ' ' . $instance['postal_code'] . ' ' . $instance['city'] . ' ' . $instance['country'];
  126.  
  127. $encoded_map_adress = str_replace( ' ', '+', $map_adress );
  128. }
  129.  
  130. $widget_output = '<ul class="vcard" itemscope itemtype="http://schema.org/'. $type. '">';
  131. if ( !empty( $instance['name'] ) )
  132. $widget_output .= '<li class="fn ' . $org . '" itemprop="name"><strong>' . $instance['name'] . '</strong></li>';
  133. if ( !empty( $instance['activity'] ) )
  134. $widget_output .= '<li itemprop="' . $activity . '">' . $instance['activity'] . '</li>';
  135. $widget_output .= '<li><ul class="adr" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">';
  136. if ( !empty( $instance['address'] ) ) {
  137. $widget_output .= '<li class="street-address" itemprop="streetAddress">' . $instance['address'] . '</li>';
  138. }
  139. if ( !empty( $instance['postal_code'] ) || ( get_locale() == 'en_US' && !empty( $instance['state'] ) ) ||!empty( $instance['city'] ) ) {
  140. $widget_output.= '<li>';
  141. if ( get_locale() == 'en_US' ) {
  142. if ( !empty( $instance['city'] ) ) {
  143. $widget_output .= '<span class="locality" itemprop="addressLocality">' . $instance['city'] . '</span>,&nbsp;';
  144. }
  145. if ( !empty( $instance['state'] ) ) {
  146. $widget_output .= '<span class="region" itemprop="addressRegion">' . $instance['state'] . '</span>&nbsp;';
  147. }
  148. if ( !empty( $instance['postal_code'] ) ) {
  149. $widget_output .= '<span class="postal-code" itemprop="postalCode">' . $instance['postal_code'] . '</span>';
  150. }
  151. }
  152. else {
  153. if ( !empty( $instance['postal_code'] ) ) {
  154. $widget_output .= '<span class="postal-code" itemprop="postalCode">' . $instance['postal_code'] . '</span>&nbsp;';
  155. }
  156. if ( !empty( $instance['city'] ) ) {
  157. $widget_output .= '<span class="locality" itemprop="addressLocality">' . $instance['city'] . '</span>';
  158. }
  159. }
  160. $widget_output .= '</li>';
  161. }
  162. if ( !empty( $instance['country'] ) ) {
  163. $widget_output .= '<li class="country-name" itemprop="addressCountry">' . $instance['country'] . '</li>';
  164. }
  165. $widget_output .= '</ul></li>';
  166. if ( !empty( $instance['phone'] ) ) {
  167. $widget_output .= '<li class="tel" itemprop="telephone">';
  168.  
  169. global $wp_version;
  170. if ( version_compare( $wp_version, '3.4', '>=' ) && wp_is_mobile() ) {
  171. $widget_output .= '<a href="tel:' . $instance['phone'] . '">' . $instance['phone'] . '</a>';
  172. }
  173. else
  174. $widget_output .= $instance['phone'];
  175. $widget_output .= '</li>';
  176. }
  177. if ( !empty( $instance['email'] ) ) {
  178. $widget_output .= '<li class="email" itemprop="email"><a href="mailto:' . antispambot($instance['email']) . '">' . $instance['email'] . '</a></li>';
  179. }
  180.  
  181. if ( $instance['vcf'] == 1 ) {
  182. $upload_dir = wp_upload_dir();
  183. $widget_output .= '<li class="vcf"><a href="' . $upload_dir['baseurl'] . '/' . strtolower( remove_accents( trim( str_replace( ' ', '', $instance['name'] ) ) ) ) . '.vcf">' . __( 'Download vCard', 'rich-contact-widget' ) . '</a></li>';
  184. }
  185.  
  186. $widget_output .= '</ul>';
  187. if ( $instance['map'] == 1 ) {
  188. $widget_output .= '<a href="http://mapof.it/'. $encoded_map_adress . '"><img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $encoded_map_adress . '&amp;zoom=15&amp;size=' . $instance['map_width'] . 'x' . $instance['map_height'] . '&amp;sensor=false&amp;markers=' . $encoded_map_adress . '" alt="' . __('Map for', 'rich_contact-widget') . ' ' . $map_adress . '" width="' . $instance['map_width'] . '" height="' . $instance['map_height'] . '"></a>';
  189. }
  190. $widget_output = apply_filters( 'rc_widget_output', $widget_output, $instance );
  191. echo $widget_output;
  192. echo $after_widget;
  193. }
  194.  
  195. /**
  196. * Sanitize widget form values as they are saved.
  197. *
  198. * @see WP_Widget::update()
  199. *
  200. * @param array $new_instance Values just sent to be saved.
  201. * @param array $old_instance Previously saved values from database.
  202. *
  203. * @return array Updated safe values to be saved.
  204. */
  205. public function update( $new_instance, $old_instance ) {
  206. foreach ( $this->widget_keys() as $key=>$value ) {
  207. if ( $old_instance[ $value ] != $new_instance[ $value ] || !array_key_exists($value, $old_instance) ) {
  208. $new_instance[ $value ] = strip_tags( $new_instance[$value] );
  209. }
  210. }
  211. if ( $new_instance['vcf'] == 1 ) {
  212. $vcf = new VCF( $new_instance );
  213. $vcf->save();
  214. }
  215. return $new_instance;
  216. }
  217.  
  218. /**
  219. * Back-end widget form.
  220. *
  221. * @see WP_Widget::form()
  222. *
  223. * @param array $instance Previously saved values from database.
  224. */
  225. public function form( $instance ) {
  226. foreach ( $this->widget_keys() as $key=>$value ) {
  227. if ( !array_key_exists( $value, $instance ) && $value == 'title' ) {
  228. ${$value} = __( 'Contact', 'rich-contact-widget' );
  229. } elseif ( !array_key_exists( $value, $instance ) ) {
  230. ${$value} = '';
  231. } else {
  232. ${$value} = $instance[ $value ];
  233. }
  234. }
  235.  
  236. $widget_form_output = '<p>
  237. <label for="' . $this->get_field_id( 'title' ) . '">' . __( 'Title :' , 'rich-contact-widget') . '</label>
  238. <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" />
  239. </p>';
  240. $widget_form_output .= '<p>
  241. ' . __('Type :', 'rich-contact-widget') .'<br />
  242. <input id="' . $this->get_field_id( 'person' ) . '" name="' . $this->get_field_name( 'type' ) . '" type="radio" value="person" ' . checked( $type, 'person', false ) . ' />
  243. <label for="' . $this->get_field_id( 'person' ) . '">' . __('Person', 'rich-contact-widget') . '<br />
  244. <input id="' . $this->get_field_id( 'company' ) . '" name="' . $this->get_field_name( 'type' ) . '" type="radio" value="company" ' . checked( $type, 'company', false ) . ' />
  245. <label for="' . $this->get_field_id( 'company' ) . '">' . __('Company', 'rich-contact-widget') . '
  246. </p>';
  247. $widget_form_output .= '<p>
  248. <label for="' . $this->get_field_id( 'name' ) . '">' . __( 'Company name/Your name :', 'rich-contact-widget' ) . '</label>
  249. <input class="widefat" id="' . $this->get_field_id( 'name' ). '" name="' . $this->get_field_name( 'name' ) . '" type="text" value="' . esc_attr( $name ) . '" />
  250. </p>';
  251. $widget_form_output .= '<p>
  252. <label for="' . $this->get_field_id('activity') . '">' . __('Activity/Job :', 'rich-contact-widget') . '</label>
  253. <input class="widefat" id="' . $this->get_field_id('activity') . '" name="' . $this->get_field_name('activity') . '" type="text" value="' . esc_attr( $activity ) . '" />
  254. </p>';
  255. $widget_form_output .= '<p>
  256. <label for="' . $this->get_field_id( 'address' ) . '">' . __( 'Company address :', 'rich-contact-widget' ) . '</label>
  257. <textarea class="widefat" id="' . $this->get_field_id( 'address' ) . '" name="' . $this->get_field_name( 'address' ) . '">' . esc_textarea( $address ) . '</textarea>
  258. </p>';
  259. $widget_form_output .= '<p>
  260. <label for="' . $this->get_field_id( 'postal_code' ) . '">' . __( 'Postal/ZIP code :', 'rich-contact-widget' ) . '</label>
  261. <input class="widefat" id="' . $this->get_field_id( 'postal_code' ) . '" name="' . $this->get_field_name( 'postal_code' ) . '" type="text" value="' . esc_attr( $postal_code ) . '" />
  262. </p>';
  263. $widget_form_output .= '<p>
  264. <label for="' . $this->get_field_id( 'city' ) . '">' . __( 'City :', 'rich-contact-widget' ) . '</label>
  265. <input class="widefat" id="' . $this->get_field_id( 'city' ) . '" name="' . $this->get_field_name( 'city' ) . '" type="text" value="' . esc_attr( $city ) . '" />
  266. </p>';
  267. if ( get_locale() == 'en_US' ) {
  268. $widget_form_output .= '<p>
  269. <label for="' . $this->get_field_id( 'state' ) . '">' . __( 'State :', 'rich-contact-widget' ) . '</label>
  270. <input class="widefat" id="' . $this->get_field_id( 'state' ) . '" name="' . $this->get_field_name( 'state' ) . '" type="text" value="' . esc_attr( $state ) . '" />
  271. </p>';
  272. }
  273. $widget_form_output .= '<p>
  274. <label for="' . $this->get_field_id( 'country' ) . '">' . __( 'Country :', 'rich-contact-widget' ) . '</label>
  275. <input class="widefat" id="' . $this->get_field_id( 'country' ) . '" name="' . $this->get_field_name( 'country' ) . '" type="text" value="' . esc_attr( $country ) . '" />
  276. </p>';
  277. $widget_form_output .= '<p>
  278. <label for="' . $this->get_field_id( 'phone' ) . '">' . __( 'Phone number :', 'rich-contact-widget' ) . '</label>
  279. <input class="widefat" id="' . $this->get_field_id( 'phone' ) . '" name="' . $this->get_field_name( 'phone' ) . '" type="text" value="' . esc_attr( $phone ) . '" />
  280. </p>';
  281. $widget_form_output .= '<p>
  282. <label for="' . $this->get_field_id( 'email' ) . '">' . __( 'Email address :', 'rich-contact-widget' ) . '</label>
  283. <input class="widefat" id="' . $this->get_field_id( 'email' ) . '" name="' . $this->get_field_name( 'email' ) . '" type="text" value="' . esc_attr( $email ) . '" />
  284. </p>';
  285. $widget_form_output .= '<p>
  286. <label for="' . $this->get_field_id( 'map' ) . '">' . __( 'Show image map :', 'rich-contact-widget' ) . '</label>
  287. <select name="' . $this->get_field_name( 'map' ) . '" id="' . $this->get_field_id( 'map' ) . '">
  288. <option value="1" ' . selected( $map, 1, false ) . '>' . __('Yes', 'rich-contact-widget') . '</option>
  289. <option value="0" ' . selected( $map, 0, false ) . '>' . __('No', 'rich-contact-widget') . '</option>
  290. </select>
  291. </p>';
  292. $widget_form_output .= '<p>
  293. <label for="' . $this->get_field_id( 'map_width' ) . '">' . __( 'Image map width (max 640px) :', 'rich-contact-widget' ) . '</label>
  294. <input class="widefat" id="' . $this->get_field_id( 'map_width' ) . '" name="' . $this->get_field_name( 'map_width' ) . '" type="text" value="' . esc_attr( $map_width ) . '" />
  295. </p>';
  296. $widget_form_output .= '<p>
  297. <label for="' . $this->get_field_id( 'map_height' ) . '">' . __( 'Image map height (max 640px) :', 'rich-contact-widget' ) . '</label>
  298. <input class="widefat" id="' . $this->get_field_id( 'map_height' ) . '" name="' . $this->get_field_name( 'map_height' ) . '" type="text" value="' . esc_attr( $map_height ) . '" />
  299. </p>';
  300. $widget_form_output .= '<p>
  301. <label for="' . $this->get_field_id( 'vcf' ) . '">' . __( 'Show vCard download link :', 'rich-contact-widget' ) . '</label>
  302. <select name="' . $this->get_field_name( 'vcf' ) . '" id="' . $this->get_field_id( 'vcf' ) . '">
  303. <option value="1" ' . selected( $vcf, 1, false ) . '>' . __('Yes', 'rich-contact-widget') . '</option>
  304. <option value="0" ' . selected( $vcf, 0, false ) . '>' . __('No', 'rich-contact-widget') . '</option>
  305. </select>
  306. </p>';
  307. $widget_form_output = apply_filters( 'rc_widget_form_output', $widget_form_output, $instance );
  308. echo $widget_form_output;
  309. }
  310.  
  311. } // class RC_Widget
  312.  
  313. // register RC_Widget widget
  314.  
  315. function rcw_register_widget() {
  316. register_widget('RC_Widget');
  317. }
  318.  
  319. // init RC_Widget widget
  320. add_action( 'widgets_init', 'rcw_register_widget' );
  321.  
  322. // Loading languages for i18n
  323. load_plugin_textdomain('rich-contact-widget', false, basename( dirname( __FILE__ ) ) . '/languages' );
  324. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement