sharing-sources.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  1. <?php
  2. abstract class Sharing_Source {
  3. public $button_style;
  4. public $smart;
  5. protected $open_link_in_new;
  6. protected $id;
  7. public function __construct( $id, array $settings ) {
  8. $this->id = $id;
  9. /**
  10. * Filter the way sharing links open.
  11. *
  12. * By default, sharing links open in a new window.
  13. *
  14. * @module sharedaddy
  15. *
  16. * @since 3.4.0
  17. *
  18. * @param bool true Should Sharing links open in a new window. Default to true.
  19. */
  20. $this->open_link_in_new = apply_filters( 'jetpack_open_sharing_in_new_window', true );
  21. if ( isset( $settings['button_style'] ) ) {
  22. $this->button_style = $settings['button_style'];
  23. }
  24. if ( isset( $settings['smart'] ) ) {
  25. $this->smart = $settings['smart'];
  26. }
  27. }
  28. public function http() {
  29. return is_ssl() ? 'https' : 'http';
  30. }
  31. public function get_id() {
  32. return $this->id;
  33. }
  34. public function get_class() {
  35. return $this->id;
  36. }
  37. public function get_share_url( $post_id ) {
  38. /**
  39. * Filter the sharing permalink.
  40. *
  41. * @module sharedaddy
  42. *
  43. * @since 1.2.0
  44. *
  45. * @param string get_permalink( $post_id ) Post Permalink.
  46. * @param int $post_id Post ID.
  47. * @param int $this->id Sharing ID.
  48. */
  49. return apply_filters( 'sharing_permalink', get_permalink( $post_id ), $post_id, $this->id );
  50. }
  51. public function get_share_title( $post_id ) {
  52. $post = get_post( $post_id );
  53. /**
  54. * Filter the sharing title.
  55. *
  56. * @module sharedaddy
  57. *
  58. * @since 2.8.0
  59. *
  60. * @param string $post->post_title Post Title.
  61. * @param int $post_id Post ID.
  62. * @param int $this->id Sharing ID.
  63. */
  64. $title = apply_filters( 'sharing_title', $post->post_title, $post_id, $this->id );
  65. return html_entity_decode( wp_kses( $title, null ) );
  66. }
  67. public function has_custom_button_style() {
  68. return false;
  69. }
  70. public function get_link( $url, $text, $title, $query = '', $id = false ) {
  71. $args = func_get_args();
  72. $klasses = array( 'share-' . $this->get_class(), 'sd-button' );
  73. if ( 'icon' == $this->button_style || 'icon-text' == $this->button_style ) {
  74. $klasses[] = 'share-icon';
  75. }
  76. if ( 'icon' == $this->button_style ) {
  77. $text = $title;
  78. $klasses[] = 'no-text';
  79. if ( true == $this->open_link_in_new ) {
  80. $text .= __( ' (Opens in new window)', 'jetpack' );
  81. }
  82. }
  83. /**
  84. * Filter the sharing display ID.
  85. *
  86. * @module sharedaddy
  87. *
  88. * @since 3.4.0
  89. *
  90. * @param int|false $id Sharing ID.
  91. * @param object $this Sharing service properties.
  92. * @param array $args Array of sharing service options.
  93. */
  94. $id = apply_filters( 'jetpack_sharing_display_id', $id, $this, $args );
  95. /**
  96. * Filter the sharing display link.
  97. *
  98. * @module sharedaddy
  99. *
  100. * @since 2.8.0
  101. *
  102. * @param string $url Post URL.
  103. * @param object $this Sharing service properties.
  104. * @param int|false $id Sharing ID.
  105. * @param array $args Array of sharing service options.
  106. */
  107. $url = apply_filters( 'sharing_display_link', $url, $this, $id, $args ); // backwards compatibility
  108. /**
  109. * Filter the sharing display link.
  110. *
  111. * @module sharedaddy
  112. *
  113. * @since 2.8.0
  114. *
  115. * @param string $url Post URL.
  116. * @param object $this Sharing service properties.
  117. * @param int|false $id Sharing ID.
  118. * @param array $args Array of sharing service options.
  119. */
  120. $url = apply_filters( 'jetpack_sharing_display_link', $url, $this, $id, $args );
  121. /**
  122. * Filter the sharing display query.
  123. *
  124. * @module sharedaddy
  125. *
  126. * @since 2.8.0
  127. *
  128. * @param string $query Sharing service URL parameter.
  129. * @param object $this Sharing service properties.
  130. * @param int|false $id Sharing ID.
  131. * @param array $args Array of sharing service options.
  132. */
  133. $query = apply_filters( 'jetpack_sharing_display_query', $query, $this, $id, $args );
  134. if ( ! empty( $query ) ) {
  135. if ( false === stripos( $url, '?' ) ) {
  136. $url .= '?' . $query;
  137. } else {
  138. $url .= '&amp;' . $query;
  139. }
  140. }
  141. if ( 'text' == $this->button_style ) {
  142. $klasses[] = 'no-icon';
  143. }
  144. /**
  145. * Filter the sharing display classes.
  146. *
  147. * @module sharedaddy
  148. *
  149. * @since 3.4.0
  150. *
  151. * @param array $klasses Sharing service classes.
  152. * @param object $this Sharing service properties.
  153. * @param int|false $id Sharing ID.
  154. * @param array $args Array of sharing service options.
  155. */
  156. $klasses = apply_filters( 'jetpack_sharing_display_classes', $klasses, $this, $id, $args );
  157. /**
  158. * Filter the sharing display title.
  159. *
  160. * @module sharedaddy
  161. *
  162. * @since 3.4.0
  163. *
  164. * @param string $title Sharing service title.
  165. * @param object $this Sharing service properties.
  166. * @param int|false $id Sharing ID.
  167. * @param array $args Array of sharing service options.
  168. */
  169. $title = apply_filters( 'jetpack_sharing_display_title', $title, $this, $id, $args );
  170. /**
  171. * Filter the sharing display text.
  172. *
  173. * @module sharedaddy
  174. *
  175. * @since 3.4.0
  176. *
  177. * @param string $text Sharing service text.
  178. * @param object $this Sharing service properties.
  179. * @param int|false $id Sharing ID.
  180. * @param array $args Array of sharing service options.
  181. */
  182. $text = apply_filters( 'jetpack_sharing_display_text', $text, $this, $id, $args );
  183. return sprintf(
  184. '<a rel="nofollow%s" data-shared="%s" class="%s" href="%s"%s title="%s"><span%s>%s</span></a>',
  185. ( true == $this->open_link_in_new ) ? ' noopener noreferrer' : '',
  186. ( $id ? esc_attr( $id ) : '' ),
  187. implode( ' ', $klasses ),
  188. $url,
  189. ( true == $this->open_link_in_new ) ? ' target="_blank"' : '',
  190. $title,
  191. ( 'icon' == $this->button_style ) ? '></span><span class="sharing-screen-reader-text"' : '',
  192. $text
  193. );
  194. }
  195. /**
  196. * Get an unfiltered post permalink to use when generating a sharing URL with get_link.
  197. * Use instead of get_share_url for non-official styles as get_permalink ensures that process_request
  198. * will be executed more reliably, in the case that the filtered URL uses a service that strips query parameters.
  199. *
  200. * @since 3.7.0
  201. * @param int $post_id Post ID.
  202. * @uses get_permalink
  203. * @return string get_permalink( $post_id ) Post permalink.
  204. */
  205. public function get_process_request_url( $post_id ) {
  206. return get_permalink( $post_id );
  207. }
  208. abstract public function get_name();
  209. abstract public function get_display( $post );
  210. public function display_header() {
  211. }
  212. public function display_footer() {
  213. }
  214. public function has_advanced_options() {
  215. return false;
  216. }
  217. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
  218. $text = '&nbsp;';
  219. $button_style = ( ! empty( $button_style ) ) ? $button_style : $this->button_style;
  220. if ( ! $this->smart && ! $force_smart ) {
  221. if ( $button_style != 'icon' ) {
  222. $text = $this->get_name();
  223. }
  224. }
  225. $klasses = array( 'share-' . $this->get_class(), 'sd-button' );
  226. if ( $button_style == 'icon' || $button_style == 'icon-text' ) {
  227. $klasses[] = 'share-icon';
  228. }
  229. if ( $button_style == 'icon' ) {
  230. $klasses[] = 'no-text';
  231. }
  232. if ( $button_style == 'text' ) {
  233. $klasses[] = 'no-icon';
  234. }
  235. $link = sprintf(
  236. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span>%s</span></a>',
  237. implode( ' ', $klasses ),
  238. $this->get_name(),
  239. $text
  240. );
  241. $smart = ( $this->smart || $force_smart ) ? 'on' : 'off';
  242. $return = "<div class='option option-smart-$smart'>$link</div>";
  243. if ( $echo ) {
  244. echo $return;
  245. }
  246. return $return;
  247. }
  248. public function get_total( $post = false ) {
  249. global $wpdb, $blog_id;
  250. $name = strtolower( $this->get_id() );
  251. if ( $post == false ) {
  252. // get total number of shares for service
  253. return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $blog_id, $name ) );
  254. }
  255. // get total shares for a post
  256. return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s', $blog_id, $post->ID, $name ) );
  257. }
  258. public function get_posts_total() {
  259. global $wpdb, $blog_id;
  260. $totals = array();
  261. $name = strtolower( $this->get_id() );
  262. $my_data = $wpdb->get_results( $wpdb->prepare( 'SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d AND share_service = %s GROUP BY post_id ORDER BY count DESC ', $blog_id, $name ) );
  263. if ( ! empty( $my_data ) ) {
  264. foreach ( $my_data as $row ) {
  265. $totals[] = new Sharing_Post_Total( $row->id, $row->total );
  266. }
  267. }
  268. usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) );
  269. return $totals;
  270. }
  271. public function process_request( $post, array $post_data ) {
  272. /**
  273. * Fires when a post is shared via one of the sharing buttons.
  274. *
  275. * @module sharedaddy
  276. *
  277. * @since 1.1.0
  278. *
  279. * @param array $args Aray of information about the sharing service.
  280. */
  281. do_action( 'sharing_bump_stats', array( 'service' => $this, 'post' => $post ) );
  282. }
  283. public function js_dialog( $name, $params = array() ) {
  284. if ( true !== $this->open_link_in_new ) {
  285. return;
  286. }
  287. $defaults = array(
  288. 'menubar' => 1,
  289. 'resizable' => 1,
  290. 'width' => 600,
  291. 'height' => 400,
  292. );
  293. $params = array_merge( $defaults, $params );
  294. $opts = array();
  295. foreach ( $params as $key => $val ) {
  296. $opts[] = "$key=$val";
  297. }
  298. $opts = implode( ',', $opts );
  299. // Add JS after sharing-js has been enqueued.
  300. wp_add_inline_script( 'sharing-js',
  301. "var windowOpen;
  302. jQuery( document.body ).on( 'click', 'a.share-$name', function() {
  303. // If there's another sharing window open, close it.
  304. if ( 'undefined' !== typeof windowOpen ) {
  305. windowOpen.close();
  306. }
  307. windowOpen = window.open( jQuery( this ).attr( 'href' ), 'wpcom$name', '$opts' );
  308. return false;
  309. });"
  310. );
  311. }
  312. }
  313. abstract class Sharing_Advanced_Source extends Sharing_Source {
  314. public function has_advanced_options() {
  315. return true;
  316. }
  317. abstract public function display_options();
  318. abstract public function update_options( array $data );
  319. abstract public function get_options();
  320. }
  321. class Share_Email extends Sharing_Source {
  322. public $shortname = 'email';
  323. public $icon = '\f410';
  324. public function __construct( $id, array $settings ) {
  325. parent::__construct( $id, $settings );
  326. if ( 'official' == $this->button_style ) {
  327. $this->smart = true;
  328. } else {
  329. $this->smart = false;
  330. }
  331. }
  332. public function get_name() {
  333. return _x( 'Email', 'as sharing source', 'jetpack' );
  334. }
  335. // Default does nothing
  336. public function process_request( $post, array $post_data ) {
  337. $ajax = false;
  338. if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
  339. $ajax = true;
  340. }
  341. $source_email = $target_email = $source_name = false;
  342. if ( isset( $post_data['source_email'] ) && is_email( $post_data['source_email'] ) ) {
  343. $source_email = $post_data['source_email'];
  344. }
  345. if ( isset( $post_data['target_email'] ) && is_email( $post_data['target_email'] ) ) {
  346. $target_email = $post_data['target_email'];
  347. }
  348. if ( isset( $post_data['source_name'] ) && strlen( $post_data['source_name'] ) < 200 ) {
  349. $source_name = $post_data['source_name'];
  350. } elseif ( isset( $post_data['source_name'] ) ) {
  351. $source_name = substr( $post_data['source_name'], 0, 200 );
  352. } else {
  353. $source_name = '';
  354. }
  355. // Test email
  356. $error = 1; // Failure in data
  357. if ( empty( $post_data['source_f_name'] ) && $source_email && $target_email && $source_name ) {
  358. /**
  359. * Allow plugins to stop the email sharing button from running the shared message through Akismet.
  360. *
  361. * @module sharedaddy
  362. *
  363. * @since 1.1.0
  364. *
  365. * @param bool true Should we check if the message isn't spam?
  366. * @param object $post Post information.
  367. * @param array $post_data Information about the shared message.
  368. */
  369. if ( apply_filters( 'sharing_email_check', true, $post, $post_data ) ) {
  370. $data = array(
  371. 'post' => $post,
  372. 'source' => $source_email,
  373. 'target' => $target_email,
  374. 'name' => $source_name,
  375. 'sharing_source' => $this,
  376. );
  377. // todo: implement an error message when email doesn't get sent.
  378. /**
  379. * Filter whether an email can be sent from the Email sharing button.
  380. *
  381. * @module sharedaddy
  382. *
  383. * @since 1.1.0
  384. *
  385. * @param array $data Array of information about the shared message.
  386. */
  387. if ( ( $data = apply_filters( 'sharing_email_can_send', $data ) ) !== false ) {
  388. // Record stats
  389. parent::process_request( $data['post'], $post_data );
  390. /**
  391. * Fires when an email is sent via the Email sharing button.
  392. *
  393. * @module sharedaddy
  394. *
  395. * @since 1.1.0
  396. *
  397. * @param array $data Array of information about the shared message.
  398. */
  399. do_action( 'sharing_email_send_post', $data );
  400. }
  401. // Return a positive regardless of whether the user is subscribed or not
  402. if ( $ajax ) {
  403. ?>
  404. <div class="response">
  405. <div class="response-title"><?php _e( 'This post has been shared!', 'jetpack' ); ?></div>
  406. <div class="response-sub"><?php printf( __( 'You have shared this post with %s', 'jetpack' ), esc_html( $target_email ) ); ?></div>
  407. <div class="response-close"><a href="#" class="sharing_cancel"><?php _e( 'Close', 'jetpack' ); ?></a></div>
  408. </div>
  409. <?php
  410. } else {
  411. wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email' );
  412. }
  413. die();
  414. } else {
  415. $error = 2; // Email check failed
  416. }
  417. }
  418. if ( $ajax ) {
  419. echo $error;
  420. } else {
  421. wp_safe_redirect( get_permalink( $post->ID ) . '?shared=email&msg=fail' );
  422. }
  423. die();
  424. }
  425. public function get_display( $post ) {
  426. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Email', 'share to', 'jetpack' ), __( 'Click to email this to a friend', 'jetpack' ), 'share=email' );
  427. }
  428. /**
  429. * Outputs the hidden email dialog
  430. */
  431. public function display_footer() {
  432. global $current_user;
  433. $visible = $status = false;
  434. ?>
  435. <div id="sharing_email" style="display: none;">
  436. <form action="<?php echo esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post">
  437. <label for="target_email"><?php _e( 'Send to Email Address', 'jetpack' ) ?></label>
  438. <input type="email" name="target_email" id="target_email" value="" />
  439. <?php if ( is_user_logged_in() ) : ?>
  440. <input type="hidden" name="source_name" value="<?php echo esc_attr( $current_user->display_name ); ?>" />
  441. <input type="hidden" name="source_email" value="<?php echo esc_attr( $current_user->user_email ); ?>" />
  442. <?php else : ?>
  443. <label for="source_name"><?php _e( 'Your Name', 'jetpack' ) ?></label>
  444. <input type="text" name="source_name" id="source_name" value="" />
  445. <label for="source_email"><?php _e( 'Your Email Address', 'jetpack' ) ?></label>
  446. <input type="email" name="source_email" id="source_email" value="" />
  447. <?php endif; ?>
  448. <input type="text" id="jetpack-source_f_name" name="source_f_name" class="input" value="" size="25" autocomplete="off" title="<?php esc_attr_e( 'This field is for validation and should not be changed', 'jetpack' ); ?>" />
  449. <?php
  450. /**
  451. * Fires when the Email sharing dialog is loaded.
  452. *
  453. * @module sharedaddy
  454. *
  455. * @since 1.1.0
  456. *
  457. * @param string jetpack Eail sharing source.
  458. */
  459. do_action( 'sharing_email_dialog', 'jetpack' );
  460. ?>
  461. <img style="float: right; display: none" class="loading" src="<?php
  462. /** This filter is documented in modules/stats.php */
  463. echo apply_filters( 'jetpack_static_url', plugin_dir_url( __FILE__ ) . 'images/loading.gif' ); ?>" alt="loading" width="16" height="16" />
  464. <input type="submit" value="<?php esc_attr_e( 'Send Email', 'jetpack' ); ?>" class="sharing_send" />
  465. <a rel="nofollow" href="#cancel" class="sharing_cancel"><?php _e( 'Cancel', 'jetpack' ); ?></a>
  466. <div class="errors errors-1" style="display: none;">
  467. <?php _e( 'Post was not sent - check your email addresses!', 'jetpack' ); ?>
  468. </div>
  469. <div class="errors errors-2" style="display: none;">
  470. <?php _e( 'Email check failed, please try again', 'jetpack' ); ?>
  471. </div>
  472. <div class="errors errors-3" style="display: none;">
  473. <?php _e( 'Sorry, your blog cannot share posts by email.', 'jetpack' ); ?>
  474. </div>
  475. </form>
  476. </div>
  477. <?php
  478. }
  479. }
  480. class Share_Twitter extends Sharing_Source {
  481. public $shortname = 'twitter';
  482. public $icon = '\f202';
  483. // 'https://dev.twitter.com/rest/reference/get/help/configuration' ( 2015/02/06 ) short_url_length is 22, short_url_length_https is 23
  484. public $short_url_length = 24;
  485. public function __construct( $id, array $settings ) {
  486. parent::__construct( $id, $settings );
  487. if ( 'official' == $this->button_style ) {
  488. $this->smart = true;
  489. } else {
  490. $this->smart = false;
  491. }
  492. }
  493. public function get_name() {
  494. return __( 'Twitter', 'jetpack' );
  495. }
  496. /**
  497. * Determine the Twitter 'via' value for a post.
  498. *
  499. * @param WP_Post|int $post Post object or post ID.
  500. * @return string Twitter handle without the preceding @.
  501. **/
  502. public static function sharing_twitter_via( $post ) {
  503. $post = get_post( $post );
  504. /**
  505. * Allow third-party plugins to customize the Twitter username used as "twitter:site" Twitter Card Meta Tag.
  506. *
  507. * @module sharedaddy
  508. *
  509. * @since 3.0.0
  510. *
  511. * @param string $string Twitter Username.
  512. * @param array $args Array of Open Graph Meta Tags and Twitter Cards tags.
  513. */
  514. $twitter_site_tag_value = apply_filters(
  515. 'jetpack_twitter_cards_site_tag',
  516. '',
  517. /** This action is documented in modules/sharedaddy/sharing-sources.php */
  518. array( 'twitter:creator' => apply_filters( 'jetpack_sharing_twitter_via', '', $post->ID ) )
  519. );
  520. /*
  521. * Hack to remove the unwanted behavior of adding 'via @jetpack' which
  522. * was introduced with the adding of the Twitter cards.
  523. * This should be a temporary solution until a better method is setup.
  524. */
  525. if ( 'jetpack' == $twitter_site_tag_value ) {
  526. $twitter_site_tag_value = '';
  527. }
  528. /**
  529. * Filters the Twitter username used as "via" in the Twitter sharing button.
  530. *
  531. * @module sharedaddy
  532. *
  533. * @since 1.7.0
  534. *
  535. * @param string $twitter_site_tag_value Twitter Username.
  536. * @param int $post->ID Post ID.
  537. */
  538. $twitter_site_tag_value = apply_filters( 'jetpack_sharing_twitter_via', $twitter_site_tag_value, $post->ID );
  539. // Strip out anything other than a letter, number, or underscore.
  540. // This will prevent the inadvertent inclusion of an extra @, as well as normalizing the handle.
  541. return preg_replace( '/[^\da-z_]+/i', '', $twitter_site_tag_value );
  542. }
  543. /**
  544. * Determine the 'related' Twitter accounts for a post.
  545. *
  546. * @param WP_Post|int $post Post object or post ID.
  547. * @return string Comma-separated list of Twitter handles.
  548. **/
  549. public static function get_related_accounts( $post ) {
  550. $post = get_post( $post );
  551. /**
  552. * Filter the list of related Twitter accounts added to the Twitter sharing button.
  553. *
  554. * @module sharedaddy
  555. *
  556. * @since 1.7.0
  557. *
  558. * @param array $args Array of Twitter usernames. Format is 'username' => 'Optional description'
  559. * @param int $post->ID Post ID.
  560. */
  561. $related_accounts = apply_filters( 'jetpack_sharing_twitter_related', array(), $post->ID );
  562. // Example related string: account1,account2:Account 2 description,account3
  563. $related = array();
  564. foreach ( $related_accounts as $related_account_username => $related_account_description ) {
  565. // Join the description onto the end of the username
  566. if ( $related_account_description ) {
  567. $related_account_username .= ':' . $related_account_description;
  568. }
  569. $related[] = $related_account_username;
  570. }
  571. return implode( ',', $related );
  572. }
  573. public function get_display( $post ) {
  574. $via = $this->sharing_twitter_via( $post );
  575. if ( $via ) {
  576. $via = 'data-via="' . esc_attr( $via ) . '"';
  577. } else {
  578. $via = '';
  579. }
  580. $related = $this->get_related_accounts( $post );
  581. if ( ! empty( $related ) && $related !== $via ) {
  582. $related = 'data-related="' . esc_attr( $related ) . '"';
  583. } else {
  584. $related = '';
  585. }
  586. if ( $this->smart ) {
  587. $share_url = $this->get_share_url( $post->ID );
  588. $post_title = $this->get_share_title( $post->ID );
  589. return sprintf(
  590. '<a href="https://twitter.com/share" class="twitter-share-button" data-url="%1$s" data-text="%2$s" %3$s %4$s>Tweet</a>',
  591. esc_url( $share_url ),
  592. esc_attr( $post_title ),
  593. $via,
  594. $related
  595. );
  596. } else {
  597. if (
  598. /**
  599. * Allow plugins to disable sharing counts for specific sharing services.
  600. *
  601. * @module sharedaddy
  602. *
  603. * @since 3.0.0
  604. *
  605. * @param bool true Should sharing counts be enabled for this specific service. Default to true.
  606. * @param int $post->ID Post ID.
  607. * @param string $str Sharing service name.
  608. */
  609. apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'twitter' )
  610. ) {
  611. sharing_register_post_for_share_counts( $post->ID );
  612. }
  613. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Twitter', 'share to', 'jetpack' ), __( 'Click to share on Twitter', 'jetpack' ), 'share=twitter', 'sharing-twitter-' . $post->ID );
  614. }
  615. }
  616. public function process_request( $post, array $post_data ) {
  617. $post_title = $this->get_share_title( $post->ID );
  618. $post_link = $this->get_share_url( $post->ID );
  619. if ( function_exists( 'mb_stripos' ) ) {
  620. $strlen = 'mb_strlen';
  621. $substr = 'mb_substr';
  622. } else {
  623. $strlen = 'strlen';
  624. $substr = 'substr';
  625. }
  626. $via = $this->sharing_twitter_via( $post );
  627. $related = $this->get_related_accounts( $post );
  628. if ( $via ) {
  629. $sig = " via @$via";
  630. if ( $related === $via ) {
  631. $related = false;
  632. }
  633. } else {
  634. $via = false;
  635. $sig = '';
  636. }
  637. $suffix_length = $this->short_url_length + $strlen( $sig );
  638. // $sig is handled by twitter in their 'via' argument.
  639. // $post_link is handled by twitter in their 'url' argument.
  640. if ( 280 < $strlen( $post_title ) + $suffix_length ) {
  641. // The -1 is for "\xE2\x80\xA6", a UTF-8 ellipsis.
  642. $text = $substr( $post_title, 0, 280 - $suffix_length - 1 ) . "\xE2\x80\xA6";
  643. } else {
  644. $text = $post_title;
  645. }
  646. // Record stats
  647. parent::process_request( $post, $post_data );
  648. $url = $post_link;
  649. $twitter_url = add_query_arg(
  650. rawurlencode_deep( array_filter( compact( 'via', 'related', 'text', 'url' ) ) ),
  651. 'https://twitter.com/intent/tweet'
  652. );
  653. // Redirect to Twitter
  654. wp_redirect( $twitter_url );
  655. die();
  656. }
  657. public function has_custom_button_style() {
  658. return $this->smart;
  659. }
  660. public function display_footer() {
  661. if ( $this->smart ) {
  662. ?>
  663. <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
  664. <?php
  665. } else {
  666. $this->js_dialog( $this->shortname, array( 'height' => 350 ) );
  667. }
  668. }
  669. }
  670. class Share_Reddit extends Sharing_Source {
  671. public $shortname = 'reddit';
  672. public $icon = '\f222';
  673. public function __construct( $id, array $settings ) {
  674. parent::__construct( $id, $settings );
  675. if ( 'official' == $this->button_style ) {
  676. $this->smart = true;
  677. } else {
  678. $this->smart = false;
  679. }
  680. }
  681. public function get_name() {
  682. return __( 'Reddit', 'jetpack' );
  683. }
  684. public function get_display( $post ) {
  685. if ( $this->smart ) {
  686. return '<div class="reddit_button"><iframe src="' . $this->http() . '://www.reddit.com/static/button/button1.html?newwindow=true&width=120&amp;url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&amp;title=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '" height="22" width="120" scrolling="no" frameborder="0"></iframe></div>';
  687. } else {
  688. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Reddit', 'share to', 'jetpack' ), __( 'Click to share on Reddit', 'jetpack' ), 'share=reddit' );
  689. }
  690. }
  691. public function process_request( $post, array $post_data ) {
  692. $reddit_url = $this->http() . '://reddit.com/submit?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) );
  693. // Record stats
  694. parent::process_request( $post, $post_data );
  695. // Redirect to Reddit
  696. wp_redirect( $reddit_url );
  697. die();
  698. }
  699. }
  700. class Share_LinkedIn extends Sharing_Source {
  701. public $shortname = 'linkedin';
  702. public $icon = '\f207';
  703. public function __construct( $id, array $settings ) {
  704. parent::__construct( $id, $settings );
  705. if ( 'official' == $this->button_style ) {
  706. $this->smart = true;
  707. } else {
  708. $this->smart = false;
  709. }
  710. }
  711. public function get_name() {
  712. return __( 'LinkedIn', 'jetpack' );
  713. }
  714. public function has_custom_button_style() {
  715. return $this->smart;
  716. }
  717. public function get_display( $post ) {
  718. $display = '';
  719. if ( $this->smart ) {
  720. $share_url = $this->get_share_url( $post->ID );
  721. $display .= sprintf( '<div class="linkedin_button"><script type="in/share" data-url="%s" data-counter="right"></script></div>', esc_url( $share_url ) );
  722. } else {
  723. $display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'LinkedIn', 'share to', 'jetpack' ), __( 'Click to share on LinkedIn', 'jetpack' ), 'share=linkedin', 'sharing-linkedin-' . $post->ID );
  724. }
  725. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  726. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
  727. sharing_register_post_for_share_counts( $post->ID );
  728. }
  729. return $display;
  730. }
  731. public function process_request( $post, array $post_data ) {
  732. $post_link = $this->get_share_url( $post->ID );
  733. // Using the same URL as the official button, which is *not* LinkedIn's documented sharing link
  734. // https://www.linkedin.com/cws/share?url={url}&token=&isFramed=false
  735. $linkedin_url = add_query_arg( array(
  736. 'url' => rawurlencode( $post_link ),
  737. ), 'https://www.linkedin.com/cws/share?token=&isFramed=false' );
  738. // Record stats
  739. parent::process_request( $post, $post_data );
  740. // Redirect to LinkedIn
  741. wp_redirect( $linkedin_url );
  742. die();
  743. }
  744. public function display_footer() {
  745. if ( ! $this->smart ) {
  746. $this->js_dialog( $this->shortname, array( 'width' => 580, 'height' => 450 ) );
  747. } else {
  748. ?><script type="text/javascript">
  749. jQuery( document ).ready( function() {
  750. jQuery.getScript( 'https://platform.linkedin.com/in.js?async=true', function success() {
  751. IN.init();
  752. });
  753. });
  754. jQuery( document.body ).on( 'post-load', function() {
  755. if ( typeof IN != 'undefined' )
  756. IN.parse();
  757. });
  758. </script><?php
  759. }
  760. }
  761. }
  762. class Share_Facebook extends Sharing_Source {
  763. public $shortname = 'facebook';
  764. public $icon = '\f204';
  765. private $share_type = 'default';
  766. public function __construct( $id, array $settings ) {
  767. parent::__construct( $id, $settings );
  768. if ( isset( $settings['share_type'] ) ) {
  769. $this->share_type = $settings['share_type'];
  770. }
  771. if ( 'official' == $this->button_style ) {
  772. $this->smart = true;
  773. } else {
  774. $this->smart = false;
  775. }
  776. }
  777. public function get_name() {
  778. return __( 'Facebook', 'jetpack' );
  779. }
  780. public function display_header() {
  781. }
  782. function guess_locale_from_lang( $lang ) {
  783. if ( 'en' == $lang || 'en_US' == $lang || ! $lang ) {
  784. return 'en_US';
  785. }
  786. if ( ! class_exists( 'GP_Locales' ) ) {
  787. if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  788. return false;
  789. }
  790. require JETPACK__GLOTPRESS_LOCALES_PATH;
  791. }
  792. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  793. // WP.com: get_locale() returns 'it'
  794. $locale = GP_Locales::by_slug( $lang );
  795. } else {
  796. // Jetpack: get_locale() returns 'it_IT';
  797. $locale = GP_Locales::by_field( 'wp_locale', $lang );
  798. }
  799. if ( ! $locale ) {
  800. return false;
  801. }
  802. if ( empty( $locale->facebook_locale ) ) {
  803. if ( empty( $locale->wp_locale ) ) {
  804. return false;
  805. } else {
  806. // Facebook SDK is smart enough to fall back to en_US if a
  807. // locale isn't supported. Since supported Facebook locales
  808. // can fall out of sync, we'll attempt to use the known
  809. // wp_locale value and rely on said fallback.
  810. return $locale->wp_locale;
  811. }
  812. }
  813. return $locale->facebook_locale;
  814. }
  815. public function get_display( $post ) {
  816. if ( $this->smart ) {
  817. $share_url = $this->get_share_url( $post->ID );
  818. $fb_share_html = '<div class="fb-share-button" data-href="' . esc_attr( $share_url ) . '" data-layout="button_count"></div>';
  819. /**
  820. * Filter the output of the Facebook Sharing button.
  821. *
  822. * @module sharedaddy
  823. *
  824. * @since 3.6.0
  825. *
  826. * @param string $fb_share_html Facebook Sharing button HTML.
  827. * @param string $share_url URL of the post to share.
  828. */
  829. return apply_filters( 'jetpack_sharing_facebook_official_button_output', $fb_share_html, $share_url );
  830. }
  831. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  832. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'facebook' ) ) {
  833. sharing_register_post_for_share_counts( $post->ID );
  834. }
  835. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Facebook', 'share to', 'jetpack' ), __( 'Click to share on Facebook', 'jetpack' ), 'share=facebook', 'sharing-facebook-' . $post->ID );
  836. }
  837. public function process_request( $post, array $post_data ) {
  838. $fb_url = $this->http() . '://www.facebook.com/sharer.php?u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) );
  839. // Record stats
  840. parent::process_request( $post, $post_data );
  841. // Redirect to Facebook
  842. wp_redirect( $fb_url );
  843. die();
  844. }
  845. public function display_footer() {
  846. $this->js_dialog( $this->shortname );
  847. if ( $this->smart ) {
  848. $locale = $this->guess_locale_from_lang( get_locale() );
  849. if ( ! $locale ) {
  850. $locale = 'en_US';
  851. }
  852. /**
  853. * Filter the App ID used in the official Facebook Share button.
  854. *
  855. * @since 3.8.0
  856. *
  857. * @param int $fb_app_id Facebook App ID. Default to 249643311490 (WordPress.com's App ID).
  858. */
  859. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
  860. if ( is_numeric( $fb_app_id ) ) {
  861. $fb_app_id = '&appId=' . $fb_app_id;
  862. } else {
  863. $fb_app_id = '';
  864. }
  865. ?><div id="fb-root"></div>
  866. <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = 'https://connect.facebook.net/<?php echo $locale; ?>/sdk.js#xfbml=1<?php echo $fb_app_id; ?>&version=v2.3'; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>
  867. <script>
  868. jQuery( document.body ).on( 'post-load', function() {
  869. if ( 'undefined' !== typeof FB ) {
  870. FB.XFBML.parse();
  871. }
  872. } );
  873. </script>
  874. <?php
  875. }
  876. }
  877. }
  878. class Share_Print extends Sharing_Source {
  879. public $shortname = 'print';
  880. public $icon = '\f469';
  881. public function __construct( $id, array $settings ) {
  882. parent::__construct( $id, $settings );
  883. if ( 'official' == $this->button_style ) {
  884. $this->smart = true;
  885. } else {
  886. $this->smart = false;
  887. }
  888. }
  889. public function get_name() {
  890. return __( 'Print', 'jetpack' );
  891. }
  892. public function get_display( $post ) {
  893. return $this->get_link( $this->get_process_request_url( $post->ID ) . ( ( is_single() || is_page() ) ? '#print': '' ), _x( 'Print', 'share to', 'jetpack' ), __( 'Click to print', 'jetpack' ) );
  894. }
  895. }
  896. class Share_PressThis extends Sharing_Source {
  897. public $shortname = 'pressthis';
  898. public $icon = '\f205';
  899. public function __construct( $id, array $settings ) {
  900. parent::__construct( $id, $settings );
  901. if ( 'official' == $this->button_style ) {
  902. $this->smart = true;
  903. } else {
  904. $this->smart = false;
  905. }
  906. }
  907. public function get_name() {
  908. return __( 'Press This', 'jetpack' );
  909. }
  910. public function process_request( $post, array $post_data ) {
  911. global $current_user, $wp_version;
  912. $primary_blog = (int) get_user_meta( $current_user->ID, 'primary_blog', true );
  913. if ( $primary_blog ) {
  914. $primary_blog_details = get_blog_details( $primary_blog );
  915. } else {
  916. $primary_blog_details = false;
  917. }
  918. if ( $primary_blog_details ) {
  919. $blogs = array( $primary_blog_details );
  920. } elseif ( function_exists( 'get_active_blogs_for_user' ) ) {
  921. $blogs = get_active_blogs_for_user();
  922. if ( empty( $blogs ) ) {
  923. $blogs = get_blogs_of_user( $current_user->ID );
  924. }
  925. } else {
  926. $blogs = get_blogs_of_user( $current_user->ID );
  927. }
  928. if ( empty( $blogs ) ) {
  929. wp_safe_redirect( get_permalink( $post->ID ) );
  930. die();
  931. }
  932. $blog = current( $blogs );
  933. $args = array(
  934. 'u' => rawurlencode( $this->get_share_url( $post->ID ) ),
  935. );
  936. if ( version_compare( $wp_version, '4.9-RC1-42107', '>=' ) ) {
  937. $args[ 'url-scan-submit' ] = 'Scan';
  938. $args[ '_wpnonce' ] = wp_create_nonce( 'scan-site' );
  939. } else { // Remove once 4.9 is the minimum.
  940. $args['t'] = rawurlencode( $this->get_share_title( $post->ID ) );
  941. if ( isset( $_GET['sel'] ) ) {
  942. $args['s'] = rawurlencode( $_GET['sel'] );
  943. }
  944. }
  945. $url = $blog->siteurl . '/wp-admin/press-this.php';
  946. $url = add_query_arg( $args, $url );
  947. // Record stats
  948. parent::process_request( $post, $post_data );
  949. // Redirect to Press This
  950. wp_redirect( $url );
  951. die();
  952. }
  953. public function get_display( $post ) {
  954. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Press This', 'share to', 'jetpack' ), __( 'Click to Press This!', 'jetpack' ), 'share=press-this' );
  955. }
  956. }
  957. class Share_GooglePlus1 extends Sharing_Source {
  958. public $shortname = 'googleplus1';
  959. public $icon = '\f218';
  960. private $state = false;
  961. public function __construct( $id, array $settings ) {
  962. parent::__construct( $id, $settings );
  963. if ( 'official' == $this->button_style ) {
  964. $this->smart = true;
  965. } else {
  966. $this->smart = false;
  967. }
  968. }
  969. public function get_name() {
  970. return __( 'Google', 'jetpack' );
  971. }
  972. public function has_custom_button_style() {
  973. return $this->smart;
  974. }
  975. public function get_display( $post ) {
  976. if ( $this->smart ) {
  977. $share_url = $this->get_share_url( $post->ID );
  978. return '<div class="googleplus1_button"><div class="g-plus" data-action="share" data-annotation="bubble" data-href="' . esc_url( $share_url ) . '"></div></div>';
  979. } else {
  980. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Google', 'share to', 'jetpack' ), __( 'Click to share on Google+', 'jetpack' ), 'share=google-plus-1', 'sharing-google-' . $post->ID );
  981. }
  982. }
  983. public function get_state() {
  984. return $this->state;
  985. }
  986. public function process_request( $post, array $post_data ) {
  987. if ( isset( $post_data['state'] ) ) {
  988. $this->state = $post_data['state'];
  989. }
  990. // Record stats
  991. parent::process_request( $post, $post_data );
  992. // Redirect to Google +'s sharing endpoint
  993. $url = 'https://plus.google.com/share?url=' . rawurlencode( $this->get_share_url( $post->ID ) );
  994. wp_redirect( $url );
  995. die();
  996. }
  997. public function display_footer() {
  998. global $post;
  999. if ( $this->smart ) { ?>
  1000. <script>
  1001. function renderGooglePlus1() {
  1002. if ( 'undefined' === typeof gapi ) {
  1003. return;
  1004. }
  1005. jQuery( '.g-plus' ).each(function() {
  1006. var $button = jQuery( this );
  1007. if ( ! $button.data( 'gplus-rendered' ) ) {
  1008. gapi.plusone.render( this, {
  1009. href: $button.attr( 'data-href' ),
  1010. size: $button.attr( 'data-size' ),
  1011. annotation: $button.attr( 'data-annotation' )
  1012. });
  1013. $button.data( 'gplus-rendered', true );
  1014. }
  1015. });
  1016. }
  1017. (function() {
  1018. var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
  1019. po.src = 'https://apis.google.com/js/platform.js';
  1020. po.innerHTML = '{"parsetags": "explicit"}';
  1021. po.onload = renderGooglePlus1;
  1022. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  1023. })();
  1024. jQuery( document.body ).on( 'post-load', renderGooglePlus1 );
  1025. </script>
  1026. <?php
  1027. } else {
  1028. $this->js_dialog( 'google-plus-1', array( 'width' => 480, 'height' => 550 ) );
  1029. }
  1030. }
  1031. public function get_total( $post = false ) {
  1032. global $wpdb, $blog_id;
  1033. $name = strtolower( $this->get_id() );
  1034. if ( $post == false ) {
  1035. // get total number of shares for service
  1036. return $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $blog_id, $name ) );
  1037. }
  1038. // get total shares for a post
  1039. return $wpdb->get_var( $wpdb->prepare( 'SELECT count FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s', $blog_id, $post->ID, $name ) );
  1040. }
  1041. }
  1042. class Share_Custom extends Sharing_Advanced_Source {
  1043. private $name;
  1044. private $icon;
  1045. private $url;
  1046. public $smart = true;
  1047. public $shortname;
  1048. public function get_class() {
  1049. return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) );
  1050. }
  1051. public function __construct( $id, array $settings ) {
  1052. parent::__construct( $id, $settings );
  1053. $opts = $this->get_options();
  1054. if ( isset( $settings['name'] ) ) {
  1055. $this->name = $settings['name'];
  1056. $this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] );
  1057. }
  1058. if ( isset( $settings['icon'] ) ) {
  1059. $this->icon = $settings['icon'];
  1060. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
  1061. $i = 0;
  1062. while ( $new_icon != $this->icon ) {
  1063. if ( $i > 5 ) {
  1064. $this->icon = false;
  1065. break;
  1066. } else {
  1067. $this->icon = $new_icon;
  1068. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) );
  1069. }
  1070. $i++;
  1071. }
  1072. }
  1073. if ( isset( $settings['url'] ) ) {
  1074. $this->url = $settings['url'];
  1075. }
  1076. }
  1077. public function get_name() {
  1078. return $this->name;
  1079. }
  1080. public function get_display( $post ) {
  1081. $str = $this->get_link( $this->get_process_request_url( $post->ID ), esc_html( $this->name ), sprintf( __( 'Click to share on %s', 'jetpack' ), esc_attr( $this->name ) ), 'share=' . $this->id );
  1082. return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str );
  1083. }
  1084. public function process_request( $post, array $post_data ) {
  1085. $url = str_replace( '&amp;', '&', $this->url );
  1086. $url = str_replace( '%post_id%', rawurlencode( $post->ID ), $url );
  1087. $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url );
  1088. $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url );
  1089. $url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url );
  1090. $url = str_replace( '%home_url%', rawurlencode( home_url() ), $url );
  1091. $url = str_replace( '%post_slug%', rawurlencode( $post->post_name ), $url );
  1092. if ( strpos( $url, '%post_tags%' ) !== false ) {
  1093. $tags = get_the_tags( $post->ID );
  1094. $tagged = '';
  1095. if ( $tags ) {
  1096. $tagged_raw = array();
  1097. foreach ( $tags as $tag ) {
  1098. $tagged_raw[] = rawurlencode( $tag->name );
  1099. }
  1100. $tagged = implode( ',', $tagged_raw );
  1101. }
  1102. $url = str_replace( '%post_tags%', $tagged, $url );
  1103. }
  1104. if ( strpos( $url, '%post_excerpt%' ) !== false ) {
  1105. $url_excerpt = $post->post_excerpt;
  1106. if ( empty( $url_excerpt ) ) {
  1107. $url_excerpt = $post->post_content;
  1108. }
  1109. $url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) );
  1110. $url_excerpt = wp_html_excerpt( $url_excerpt, 100 );
  1111. $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) );
  1112. $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url );
  1113. }
  1114. // Record stats
  1115. parent::process_request( $post, $post_data );
  1116. // Redirect
  1117. wp_redirect( $url );
  1118. die();
  1119. }
  1120. public function display_options() {
  1121. ?>
  1122. <div class="input">
  1123. <table class="form-table">
  1124. <tbody>
  1125. <tr>
  1126. <th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th>
  1127. <td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td>
  1128. </tr>
  1129. <tr>
  1130. <th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th>
  1131. <td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td>
  1132. </tr>
  1133. <tr>
  1134. <th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th>
  1135. <td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td>
  1136. </tr>
  1137. <tr>
  1138. <th scope="row"></th>
  1139. <td>
  1140. <input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" />
  1141. <a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a>
  1142. </td>
  1143. </tr>
  1144. </tbody>
  1145. </table>
  1146. </div>
  1147. <?php
  1148. }
  1149. public function update_options( array $data ) {
  1150. $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) );
  1151. $url = trim( esc_url_raw( $data['url'] ) );
  1152. $icon = trim( esc_url_raw( $data['icon'] ) );
  1153. if ( $name ) {
  1154. $this->name = $name;
  1155. }
  1156. if ( $url ) {
  1157. $this->url = $url;
  1158. }
  1159. if ( $icon ) {
  1160. $this->icon = $icon;
  1161. }
  1162. }
  1163. public function get_options() {
  1164. return array(
  1165. 'name' => $this->name,
  1166. 'icon' => $this->icon,
  1167. 'url' => $this->url,
  1168. );
  1169. }
  1170. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) {
  1171. $opts = $this->get_options();
  1172. $text = '&nbsp;';
  1173. if ( ! $this->smart ) {
  1174. if ( $this->button_style != 'icon' ) {
  1175. $text = $this->get_name();
  1176. }
  1177. }
  1178. $klasses = array( 'share-' . $this->shortname );
  1179. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) {
  1180. $klasses[] = 'share-icon';
  1181. }
  1182. if ( $this->button_style == 'icon' ) {
  1183. $text = '';
  1184. $klasses[] = 'no-text';
  1185. }
  1186. if ( $this->button_style == 'text' ) {
  1187. $klasses[] = 'no-icon';
  1188. }
  1189. $link = sprintf(
  1190. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span style="background-image:url(&quot;%s&quot;) !important;background-position:left center;background-repeat:no-repeat;">%s</span></a>',
  1191. implode( ' ', $klasses ),
  1192. $this->get_name(),
  1193. addcslashes( esc_url_raw( $opts['icon'] ), '"' ),
  1194. $text
  1195. );
  1196. ?>
  1197. <div class="option option-smart-off">
  1198. <?php echo $link ; ?>
  1199. </div><?php
  1200. }
  1201. }
  1202. class Share_Tumblr extends Sharing_Source {
  1203. public $shortname = 'tumblr';
  1204. public $icon = '\f214';
  1205. public function __construct( $id, array $settings ) {
  1206. parent::__construct( $id, $settings );
  1207. if ( 'official' == $this->button_style ) {
  1208. $this->smart = true;
  1209. } else {
  1210. $this->smart = false;
  1211. }
  1212. }
  1213. public function get_name() {
  1214. return __( 'Tumblr', 'jetpack' );
  1215. }
  1216. public function get_display( $post ) {
  1217. if ( $this->smart ) {
  1218. $target = '';
  1219. if ( true == $this->open_link_in_new ) {
  1220. $target = '_blank';
  1221. }
  1222. return '<a target="' . $target . '" href="https://www.tumblr.com/share/link/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&name=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '" title="' . __( 'Share on Tumblr', 'jetpack' ) . '" style="display:inline-block; text-indent:-9999px; overflow:hidden; width:62px; height:20px; background:url(\'https://platform.tumblr.com/v1/share_2.png\') top left no-repeat transparent;">' . __( 'Share on Tumblr', 'jetpack' ) . '</a>';
  1223. } else {
  1224. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Tumblr', 'share to', 'jetpack' ), __( 'Click to share on Tumblr', 'jetpack' ), 'share=tumblr' );
  1225. }
  1226. }
  1227. public function process_request( $post, array $post_data ) {
  1228. // Record stats
  1229. parent::process_request( $post, $post_data );
  1230. // Redirect to Tumblr's sharing endpoint (a la their bookmarklet)
  1231. $url = 'https://www.tumblr.com/share?v=3&u=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&t=' . rawurlencode( $this->get_share_title( $post->ID ) ) . '&s=';
  1232. wp_redirect( $url );
  1233. die();
  1234. }
  1235. // http://www.tumblr.com/share?v=3&u=URL&t=TITLE&s=
  1236. public function display_footer() {
  1237. if ( $this->smart ) {
  1238. ?><script type="text/javascript" src="https://platform.tumblr.com/v1/share.js"></script><?php
  1239. } else {
  1240. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  1241. }
  1242. }
  1243. }
  1244. class Share_Pinterest extends Sharing_Source {
  1245. public $shortname = 'pinterest';
  1246. public $icon = '\f209';
  1247. public function __construct( $id, array $settings ) {
  1248. parent::__construct( $id, $settings );
  1249. if ( 'official' == $this->button_style ) {
  1250. $this->smart = true;
  1251. } else {
  1252. $this->smart = false;
  1253. }
  1254. }
  1255. public function get_name() {
  1256. return __( 'Pinterest', 'jetpack' );
  1257. }
  1258. public function get_image( $post ) {
  1259. if ( class_exists( 'Jetpack_PostImages' ) ) {
  1260. $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) );
  1261. if ( ! empty( $image ) ) {
  1262. return $image['src'];
  1263. }
  1264. }
  1265. /**
  1266. * Filters the default image used by the Pinterest Pin It share button.
  1267. *
  1268. * @module sharedaddy
  1269. *
  1270. * @since 3.6.0
  1271. *
  1272. * @param string $url Default image URL.
  1273. */
  1274. return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' );
  1275. }
  1276. public function get_external_url( $post ) {
  1277. $url = 'https://www.pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&media=' . rawurlencode( $this->get_image( $post ) ) . '&description=' . rawurlencode( $post->post_title );
  1278. /**
  1279. * Filters the Pinterest share URL used in sharing button output.
  1280. *
  1281. * @module sharedaddy
  1282. *
  1283. * @since 3.6.0
  1284. *
  1285. * @param string $url Pinterest share URL.
  1286. */
  1287. return apply_filters( 'jetpack_sharing_pinterest_share_url', $url );
  1288. }
  1289. public function get_widget_type() {
  1290. /**
  1291. * Filters the Pinterest widget type.
  1292. *
  1293. * @see https://business.pinterest.com/en/widget-builder
  1294. *
  1295. * @module sharedaddy
  1296. *
  1297. * @since 3.6.0
  1298. *
  1299. * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal.
  1300. */
  1301. return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' );
  1302. }
  1303. public function get_display( $post ) {
  1304. $display = '';
  1305. if ( $this->smart ) {
  1306. $display = sprintf(
  1307. '<div class="pinterest_button"><a href="%s" data-pin-do="%s" data-pin-config="beside"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a></div>',
  1308. esc_url( $this->get_external_url( $post ) ),
  1309. esc_attr( $this->get_widget_type() )
  1310. );
  1311. } else {
  1312. $display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest', 'sharing-pinterest-' . $post->ID );
  1313. }
  1314. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  1315. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) {
  1316. sharing_register_post_for_share_counts( $post->ID );
  1317. }
  1318. return $display;
  1319. }
  1320. public function process_request( $post, array $post_data ) {
  1321. // Record stats
  1322. parent::process_request( $post, $post_data );
  1323. // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest
  1324. if ( ! isset( $_GET['js_only'] ) ) {
  1325. $pinterest_url = esc_url_raw( $this->get_external_url( $post ) );
  1326. wp_redirect( $pinterest_url );
  1327. } else {
  1328. echo '// share count bumped';
  1329. }
  1330. die();
  1331. }
  1332. public function display_footer() {
  1333. /**
  1334. * Filter the Pin it button appearing when hovering over images when using the official button style.
  1335. *
  1336. * @module sharedaddy
  1337. *
  1338. * @since 3.6.0
  1339. *
  1340. * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images.
  1341. */
  1342. $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true );
  1343. ?>
  1344. <?php if ( $this->smart ) : ?>
  1345. <script type="text/javascript">
  1346. // Pinterest shared resources
  1347. var s = document.createElement("script");
  1348. s.type = "text/javascript";
  1349. s.async = true;
  1350. <?php if ( $jetpack_pinit_over ) {
  1351. echo "s.setAttribute('data-pin-hover', true);";
  1352. } ?>
  1353. s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js";
  1354. var x = document.getElementsByTagName("script")[0];
  1355. x.parentNode.insertBefore(s, x);
  1356. // if 'Pin it' button has 'counts' make container wider
  1357. jQuery(window).load( function(){ jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } );
  1358. </script>
  1359. <?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?>
  1360. <script type="text/javascript">
  1361. jQuery(document).ready( function(){
  1362. jQuery('body').on('click', 'a.share-pinterest', function(e){
  1363. e.preventDefault();
  1364. // Load Pinterest Bookmarklet code
  1365. var s = document.createElement("script");
  1366. s.type = "text/javascript";
  1367. s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 );
  1368. var x = document.getElementsByTagName("script")[0];
  1369. x.parentNode.insertBefore(s, x);
  1370. // Trigger Stats
  1371. var s = document.createElement("script");
  1372. s.type = "text/javascript";
  1373. s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1';
  1374. var x = document.getElementsByTagName("script")[0];
  1375. x.parentNode.insertBefore(s, x);
  1376. });
  1377. });
  1378. </script>
  1379. <?php endif;
  1380. }
  1381. }
  1382. class Share_Pocket extends Sharing_Source {
  1383. public $shortname = 'pocket';
  1384. public $icon = '\f224';
  1385. public function __construct( $id, array $settings ) {
  1386. parent::__construct( $id, $settings );
  1387. if ( 'official' == $this->button_style ) {
  1388. $this->smart = true;
  1389. } else {
  1390. $this->smart = false;
  1391. }
  1392. }
  1393. public function get_name() {
  1394. return __( 'Pocket', 'jetpack' );
  1395. }
  1396. public function process_request( $post, array $post_data ) {
  1397. // Record stats
  1398. parent::process_request( $post, $post_data );
  1399. $pocket_url = esc_url_raw( 'https://getpocket.com/save/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&title=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
  1400. wp_redirect( $pocket_url );
  1401. exit;
  1402. }
  1403. public function get_display( $post ) {
  1404. if ( $this->smart ) {
  1405. $post_count = 'horizontal';
  1406. $button = '';
  1407. $button .= '<div class="pocket_button">';
  1408. $button .= sprintf( '<a href="https://getpocket.com/save" class="pocket-btn" data-lang="%s" data-save-url="%s" data-pocket-count="%s" >%s</a>', 'en', esc_attr( $this->get_share_url( $post->ID ) ), $post_count, esc_attr__( 'Pocket', 'jetpack' ) );
  1409. $button .= '</div>';
  1410. return $button;
  1411. } else {
  1412. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pocket', 'share to', 'jetpack' ), __( 'Click to share on Pocket', 'jetpack' ), 'share=pocket' );
  1413. }
  1414. }
  1415. function display_footer() {
  1416. if ( $this->smart ) :
  1417. ?>
  1418. <script>
  1419. // Don't use Pocket's default JS as it we need to force init new Pocket share buttons loaded via JS.
  1420. function jetpack_sharing_pocket_init() {
  1421. jQuery.getScript( 'https://widgets.getpocket.com/v1/j/btn.js?v=1' );
  1422. }
  1423. jQuery( document ).ready( jetpack_sharing_pocket_init );
  1424. jQuery( document.body ).on( 'post-load', jetpack_sharing_pocket_init );
  1425. </script>
  1426. <?php
  1427. else :
  1428. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  1429. endif;
  1430. }
  1431. }
  1432. class Share_Telegram extends Sharing_Source {
  1433. public $shortname = 'telegram';
  1434. public function __construct( $id, array $settings ) {
  1435. parent::__construct( $id, $settings );
  1436. }
  1437. public function get_name() {
  1438. return __( 'Telegram', 'jetpack' );
  1439. }
  1440. public function process_request( $post, array $post_data ) {
  1441. // Record stats
  1442. parent::process_request( $post, $post_data );
  1443. $telegram_url = esc_url_raw( 'https://telegram.me/share/url?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&text=' . rawurlencode( $this->get_share_title( $post->ID ) ) );
  1444. wp_redirect( $telegram_url );
  1445. exit;
  1446. }
  1447. public function get_display( $post ) {
  1448. return $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Telegram', 'share to', 'jetpack' ), __( 'Click to share on Telegram', 'jetpack' ), 'share=telegram' );
  1449. }
  1450. function display_footer() {
  1451. $this->js_dialog( $this->shortname, array( 'width' => 450, 'height' => 450 ) );
  1452. }
  1453. }
  1454. class Jetpack_Share_WhatsApp extends Sharing_Source {
  1455. public $shortname = 'jetpack-whatsapp';
  1456. public function __construct( $id, array $settings ) {
  1457. parent::__construct( $id, $settings );
  1458. }
  1459. public function get_name() {
  1460. return __( 'WhatsApp', 'jetpack' );
  1461. }
  1462. public function get_display( $post ) {
  1463. return $this->get_link( 'https://api.whatsapp.com/send?text=' . rawurlencode( $this->get_share_title( $post->ID ) . ' ' . $this->get_share_url( $post->ID ) ), _x( 'WhatsApp', 'share to', 'jetpack' ), __( 'Click to share on WhatsApp', 'jetpack' ) );
  1464. }
  1465. }
  1466. class Share_Skype extends Sharing_Source {
  1467. public $shortname = 'skype';
  1468. public $icon = '\f220';
  1469. private $share_type = 'default';
  1470. public function __construct( $id, array $settings ) {
  1471. parent::__construct( $id, $settings );
  1472. if ( isset( $settings['share_type'] ) ) {
  1473. $this->share_type = $settings['share_type'];
  1474. }
  1475. if ( 'official' == $this->button_style ) {
  1476. $this->smart = true;
  1477. } else {
  1478. $this->smart = false;
  1479. }
  1480. }
  1481. public function get_name() {
  1482. return __( 'Skype', 'jetpack' );
  1483. }
  1484. public function get_display( $post ) {
  1485. if ( $this->smart ) {
  1486. $skype_share_html = sprintf(
  1487. '<div class="skype-share" data-href="%1$s" data-lang="%2$s" data-style="small" data-source="jetpack" ></div>',
  1488. esc_attr( $this->get_share_url( $post->ID ) ),
  1489. 'en-US'
  1490. );
  1491. return $skype_share_html;
  1492. }
  1493. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */
  1494. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'skype' ) ) {
  1495. sharing_register_post_for_share_counts( $post->ID );
  1496. }
  1497. return $this->get_link(
  1498. $this->get_process_request_url( $post->ID ), _x( 'Skype', 'share to', 'jetpack' ), __( 'Click to share on Skype', 'jetpack' ), 'share=skype', 'sharing-skype-' . $post->ID );
  1499. }
  1500. public function process_request( $post, array $post_data ) {
  1501. $skype_url = sprintf(
  1502. 'https://web.skype.com/share?url=%1$s&lang=%2$s=&source=jetpack',
  1503. rawurlencode( $this->get_share_url( $post->ID ) ),
  1504. 'en-US'
  1505. );
  1506. // Record stats
  1507. parent::process_request( $post, $post_data );
  1508. // Redirect to Skype
  1509. wp_redirect( $skype_url );
  1510. die();
  1511. }
  1512. public function display_footer() {
  1513. if ( $this->smart ) :
  1514. ?>
  1515. <script>
  1516. (function(r, d, s) {
  1517. r.loadSkypeWebSdkAsync = r.loadSkypeWebSdkAsync || function(p) {
  1518. var js, sjs = d.getElementsByTagName(s)[0];
  1519. if (d.getElementById(p.id)) { return; }
  1520. js = d.createElement(s);
  1521. js.id = p.id;
  1522. js.src = p.scriptToLoad;
  1523. js.onload = p.callback
  1524. sjs.parentNode.insertBefore(js, sjs);
  1525. };
  1526. var p = {
  1527. scriptToLoad: 'https://swx.cdn.skype.com/shared/v/latest/skypewebsdk.js',
  1528. id: 'skype_web_sdk'
  1529. };
  1530. r.loadSkypeWebSdkAsync(p);
  1531. })(window, document, 'script');
  1532. </script>
  1533. <?php
  1534. else :
  1535. $this->js_dialog( $this->shortname, array( 'width' => 305, 'height' => 665 ) );
  1536. endif;
  1537. }
  1538. }