Nessuna descrizione

sharing-sources.php 60KB

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