Advertisement
adekherry

class.jnews-amp.php

Sep 7th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.88 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Jegtheme
  4. */
  5.  
  6. if ( ! defined( 'ABSPATH' ) )
  7. {
  8. exit;
  9. }
  10.  
  11. use Jeg\Util\Font;
  12.  
  13. class JNews_AMP
  14. {
  15. /**
  16. * @var JNews_AMP
  17. */
  18. private static $instance;
  19.  
  20. /**
  21. * @var boolean
  22. */
  23. protected $amp_ads = array();
  24.  
  25. /**
  26. * @return JNews_AMP
  27. */
  28. public static function getInstance()
  29. {
  30. if ( null === static::$instance )
  31. {
  32. static::$instance = new static();
  33. }
  34. return static::$instance;
  35. }
  36.  
  37. /**
  38. * JNews_AMP constructor
  39. */
  40. private function __construct()
  41. {
  42. $this->setup_init();
  43. $this->setup_hook();
  44. }
  45.  
  46. /**
  47. * Setup hook
  48. */
  49. protected function setup_hook()
  50. {
  51. // customizer
  52. add_action( 'jnews_register_customizer_option', array( $this, 'customizer_option' ) );
  53. add_filter( 'jeg_register_lazy_section', array( $this, 'autoload_section'));
  54.  
  55. // amp
  56. add_filter( 'amp_post_template_data', array( $this, 'add_googlefont' ) );
  57. add_filter( 'amp_post_template_data', array( $this, 'add_body_class' ) );
  58. add_filter( 'amp_post_template_dir', array( $this, 'add_template_folder' ) );
  59.  
  60. // favicon
  61. add_action( 'amp_post_template_head', array( $this, 'add_script' ) );
  62. add_action( 'amp_post_template_head', array( $this, 'add_favicon' ) );
  63.  
  64. // ads
  65. add_action( 'jnews_amp_before_header', array( $this, 'above_header_ads' ) );
  66. add_action( 'jnews_amp_before_header', array( $this, 'google_auto_ads' ) );
  67. add_action( 'jnews_amp_before_article', array( $this, 'above_article_ads' ) );
  68. add_action( 'jnews_amp_after_article', array( $this, 'below_article_ads' ) );
  69. add_action( 'jnews_amp_before_content', array( $this, 'above_content_ads' ) );
  70. add_action( 'jnews_amp_after_content', array( $this, 'below_content_ads' ) );
  71. add_filter( 'the_content', array( $this, 'inline_content_ads' ) );
  72.  
  73. // related item
  74. add_action( 'jnews_amp_after_content', array( $this, 'related_item' ) );
  75.  
  76. // main share button
  77. add_filter( 'jnews_single_share_main_button_list', array( $this, 'share_main_button' ) );
  78.  
  79. // search form
  80. add_filter( 'jnews_get_permalink', array( $this, 'get_permalink' ) );
  81.  
  82. // AMP
  83. add_filter('amp_post_template_metadata', array($this, 'meta_data'), null, 2);
  84.  
  85. add_action('pre_amp_render_post', function(){
  86. remove_filter('the_content', array(\JNews\Ads::getInstance(), 'inject_ads'), 10);
  87. });
  88.  
  89. // mobile truncate
  90. add_filter( 'theme_mod_jnews_mobile_truncate', array( $this, 'mobile_truncate' ), 99 );
  91. }
  92.  
  93. public function mobile_truncate($value)
  94. {
  95. if ( is_amp_endpoint() )
  96. {
  97. return false;
  98. }
  99.  
  100. return $value;
  101. }
  102.  
  103. public function meta_data($metadata, $post)
  104. {
  105. unset($metadata['image']);
  106.  
  107. // Type
  108. $metadata['@type'] = jnews_get_option('article_schema_type', 'article');
  109.  
  110. // URL
  111. $metadata['url'] = get_the_permalink($post);
  112.  
  113. // Thumbnail URL
  114. if(has_post_thumbnail($post))
  115. {
  116. $post_thumbnail_id = get_post_thumbnail_id( $post );
  117. $thumbnail = wp_get_attachment_image_src($post_thumbnail_id, 'jnews-120x86');
  118. $fullImage = wp_get_attachment_image_src($post_thumbnail_id, 'full');
  119.  
  120. $metadata['thumbnailUrl'] = $thumbnail[0];
  121. $metadata['image'] = $fullImage[0];
  122. }
  123.  
  124. // Category
  125. $categories = get_the_category($post->ID);
  126. if(!empty($categories))
  127. {
  128. $metadata['articleSection'] = array();
  129. foreach($categories as $category){
  130. $metadata['articleSection'][] = $category->name;
  131. }
  132. }
  133.  
  134. // Publisher
  135. $logo = jnews_get_option('main_schema_logo', '');
  136. if(!empty($logo)) {
  137. $metadata['publisher']['logo'] = array(
  138. '@type' => 'ImageObject',
  139. 'url' => $logo
  140. );
  141. }
  142.  
  143. return $metadata;
  144. }
  145.  
  146. public function add_favicon()
  147. {
  148. if ( has_site_icon() ) wp_site_icon();
  149. }
  150.  
  151. public function get_permalink( $url )
  152. {
  153. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() )
  154. {
  155. if ( ! is_ssl() )
  156. {
  157. $url = preg_replace( "/^http:/i", "", $url );
  158. }
  159. }
  160.  
  161. return $url;
  162. }
  163.  
  164. /**
  165. * Setup init
  166. */
  167. protected function setup_init()
  168. {
  169. $locations = array( 'above_header', 'above_article', 'below_article', 'above_content', 'inline_content', 'below_content' );
  170.  
  171. foreach ( $locations as $location )
  172. {
  173. $enable_ad = jnews_get_option( 'amp_ads_' . $location . '_enable', false );
  174.  
  175. if ( $enable_ad )
  176. {
  177. $this->amp_ads[$location] = true;
  178. }
  179. }
  180. }
  181.  
  182. /**
  183. * Load customizer option
  184. */
  185. public function customizer_option()
  186. {
  187. $customizer = Jeg\Customizer::getInstance();
  188.  
  189. // set section
  190. $customizer->add_section(array(
  191. 'id' => 'jnews_ads_amp_section',
  192. 'title' => esc_html__( 'AMP Ads', 'jnews-amp' ),
  193. 'panel' => 'jnews_ads',
  194. 'priority' => 252,
  195. 'type' => 'jnews-lazy-section',
  196. ));
  197. }
  198.  
  199. public function autoload_section($result)
  200. {
  201. $result['jnews_ads_amp_section'][] = JNEWS_AMP_DIR . "amp-option.php";
  202. return $result;
  203. }
  204.  
  205. /**
  206. * Load amp template folder
  207. */
  208. public function add_template_folder()
  209. {
  210. return JNEWS_AMP_DIR . "template";
  211. }
  212.  
  213. /**
  214. * Add google font
  215. */
  216. public function add_googlefont( $amp_data )
  217. {
  218. $style_instance = Jeg\Util\StyleGenerator::getInstance();
  219. $font_url = $style_instance->get_font_url();
  220.  
  221. $amp_data = $this->gdpr_google_font( $amp_data );
  222.  
  223. if ( empty( $font_url ) ) return $amp_data;
  224.  
  225. $font_url = 'https:' . $font_url;
  226.  
  227. $amp_data['font_urls'] = array(
  228. 'customizer-fonts' => $font_url
  229. );
  230.  
  231. return $amp_data;
  232. }
  233.  
  234. public function gdpr_google_font( $amp_data )
  235. {
  236. if ( class_exists( 'Jeg\Util\Font' ) && get_theme_mod('jnews_gdpr_google_font_disable', false) ) {
  237.  
  238. if ( isset( $amp_data['font_urls'] ) && is_array( $amp_data['font_urls'] ) ) {
  239.  
  240. foreach ( $amp_data['font_urls'] as $key => $value ) {
  241.  
  242. if ( Font::is_google_font( ucfirst($key) ) ) {
  243. unset( $amp_data['font_urls'][$key] );
  244. }
  245.  
  246. }
  247.  
  248. }
  249. }
  250.  
  251. return $amp_data;
  252. }
  253.  
  254. /**
  255. * Add Additional Body Class
  256. */
  257. public function add_body_class( $amp_data )
  258. {
  259. if ( is_rtl() )
  260. {
  261. $amp_data['body_class'] .= ' rtl';
  262. }
  263.  
  264. return $amp_data;
  265. }
  266.  
  267. /**
  268. * Add script
  269. */
  270. public function add_script( $amp_template )
  271. {
  272. $scripts = array();
  273. $format = get_post_format( get_the_ID() );
  274.  
  275. if ( $format === 'gallery' )
  276. {
  277. $scripts[] = array(
  278. 'name' => 'amp-carousel',
  279. 'source' => 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js'
  280. );
  281. }
  282.  
  283. if ( $format === 'video' )
  284. {
  285. $video_url = get_post_meta( get_the_ID(), '_format_video_embed', true );
  286.  
  287. if ( jnews_check_video_type( $video_url ) === 'youtube' )
  288. {
  289. $scripts[] = array(
  290. 'name' => 'amp-youtube',
  291. 'source' => 'https://cdn.ampproject.org/v0/amp-youtube-0.1.js'
  292. );
  293. }
  294.  
  295. }
  296.  
  297. if ( !empty( $this->amp_ads ) )
  298. {
  299. $scripts[] = array(
  300. 'name' => 'amp-ad',
  301. 'source' => 'https://cdn.ampproject.org/v0/amp-ad-0.1.js'
  302. );
  303. }
  304.  
  305. // sidebar
  306. $scripts[] = array(
  307. 'name' => 'amp-sidebar',
  308. 'source' => 'https://cdn.ampproject.org/v0/amp-sidebar-0.1.js'
  309. );
  310.  
  311. // form
  312. if ( $this->header_search_form() )
  313. {
  314. $scripts[] = array(
  315. 'name' => 'amp-form',
  316. 'source' => 'https://cdn.ampproject.org/v0/amp-form-0.1.js'
  317. );
  318. }
  319.  
  320. // Google Auto Ads
  321. if ( jnews_get_option('amp_ads_google_auto_enable', false) )
  322. {
  323. $scripts[] = array(
  324. 'name' => 'amp-auto-ads',
  325. 'source' => 'https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js'
  326. );
  327. }
  328.  
  329. foreach ( $scripts as $script )
  330. {
  331. $loaded_script = $amp_template->get( 'amp_component_scripts', array() );
  332.  
  333. if ( !empty( $script['name'] ) && !array_key_exists( $script['name'], $loaded_script ) )
  334. {
  335. ?>
  336. <script custom-element="<?php echo esc_attr( $script['name'] ); ?>" src="<?php echo esc_url( $script['source'] ); ?>" async></script>
  337. <?php
  338. }
  339. }
  340. }
  341.  
  342. /**
  343. * Google Auto Ads
  344. */
  345. public function google_auto_ads()
  346. {
  347. if ( jnews_get_option('amp_ads_google_auto_enable', false) )
  348. {
  349. $publisher_id = jnews_get_option('amp_ads_google_auto_publisher', false);
  350.  
  351. if ( $publisher_id )
  352. {
  353. $html = "<amp-auto-ads type=\"adsense\" data-ad-client=\"{$publisher_id}\"></amp-auto-ads>";
  354. echo $html;
  355. }
  356. }
  357. }
  358.  
  359. /**
  360. * Above header ads
  361. */
  362. public function above_header_ads()
  363. {
  364. $location = 'above_header';
  365.  
  366. if ( array_key_exists( $location, $this->amp_ads ) )
  367. {
  368. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  369.  
  370. echo $html;
  371. }
  372. }
  373.  
  374. /**
  375. * Above article ads
  376. */
  377. public function above_article_ads()
  378. {
  379. $location = 'above_article';
  380.  
  381. if ( array_key_exists( $location, $this->amp_ads ) )
  382. {
  383. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  384.  
  385. echo $html;
  386. }
  387. }
  388.  
  389. /**
  390. * Below article ads
  391. */
  392. public function below_article_ads()
  393. {
  394. $location = 'below_article';
  395.  
  396. if ( array_key_exists( $location, $this->amp_ads ) )
  397. {
  398. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  399.  
  400. echo $html;
  401. }
  402. }
  403.  
  404. /**
  405. * Above content ads
  406. */
  407. public function above_content_ads()
  408. {
  409. $location = 'above_content';
  410.  
  411. if ( array_key_exists( $location, $this->amp_ads ) )
  412. {
  413. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  414.  
  415. echo $html;
  416. }
  417. }
  418.  
  419. /**
  420. * Below content ads
  421. */
  422. public function below_content_ads()
  423. {
  424. $location = 'below_content';
  425.  
  426. if ( array_key_exists( $location, $this->amp_ads ) )
  427. {
  428. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  429.  
  430. echo $html;
  431. }
  432. }
  433.  
  434. /**
  435. * Inline content ads
  436. */
  437. public function inline_content_ads( $content )
  438. {
  439. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() )
  440. {
  441. $location = 'inline_content';
  442.  
  443. if ( array_key_exists( $location, $this->amp_ads ) )
  444. {
  445. $tag = new \JNews\ContentTag($content);
  446. $pnumber = $tag->total('p');
  447.  
  448. $adsposition = jnews_get_option( 'amp_ads_' . $location . '_paragraph', 3 );
  449. $adsrandom = jnews_get_option( 'amp_ads_' . $location . '_paragraph_random', false );
  450.  
  451. if ( $adsrandom )
  452. {
  453. $maxparagraph = count( $pnumber ) - 2;
  454. $adsposition = rand( $adsposition, $maxparagraph );
  455. }
  456.  
  457. $html = "<div class=\"amp_ad_wrapper jnews_amp_{$location}_ads\">" . $this->render_ads( $location ) . "</div>";
  458. $content = $this->prefix_insert_after_paragraph( $html, $adsposition, $tag );
  459. }
  460.  
  461. }
  462.  
  463. return $content;
  464. }
  465.  
  466. /**
  467. * Render ads
  468. *
  469. * @param string $location
  470. *
  471. * @return string
  472. *
  473. */
  474. protected function render_ads( $location )
  475. {
  476. $ads_html = '';
  477.  
  478. if ( jnews_get_option( 'amp_ads_' . $location . '_type', 'googleads' ) == 'googleads' )
  479. {
  480. $publisherid = jnews_get_option( 'amp_ads_' . $location . '_google_publisher', '' );
  481. $slotid = jnews_get_option( 'amp_ads_' . $location . '_google_id', '' );
  482.  
  483. if ( !empty( $publisherid ) && !empty( $slotid ) )
  484. {
  485. $ad_size = jnews_get_option( 'amp_ads_' . $location . '_size', 'auto' );
  486.  
  487. if ( $ad_size !== 'auto' )
  488. {
  489. $ad_size = explode( 'x', $ad_size );
  490. } else {
  491. $ad_size = array('320', '50');
  492. }
  493.  
  494. $publisherid = str_replace('ca-', '', $publisherid);
  495.  
  496. $ads_html .=
  497. "<amp-ad
  498. type=\"adsense\"
  499. width={$ad_size[0]}
  500. height={$ad_size[1]}
  501. data-ad-client=\"ca-{$publisherid}\"
  502. data-ad-slot=\"{$slotid}\">
  503. </amp-ad>";
  504. }
  505. } else {
  506. $ads_html = jnews_get_option( 'amp_ads_' . $location . '_custom', '' ) ;
  507. }
  508.  
  509. return $ads_html;
  510. }
  511.  
  512. /**
  513. * Insert ads into certain paragraph
  514. *
  515. * @param string $insertion
  516. * @param int $paragraph_id
  517. * @param \JNews\ContentTag $tag
  518. *
  519. * @return string
  520. *
  521. */
  522. protected function prefix_insert_after_paragraph( $insertion, $paragraph_id, $tag )
  523. {
  524. $line = $tag->find('p', $paragraph_id);
  525. return jeg_string_insert($tag->get_content(), $insertion, $line);
  526. }
  527.  
  528. /**
  529. * Generate related post item
  530. */
  531. public function related_item()
  532. {
  533. if ( class_exists( 'JNews\\Module\\ModuleQuery' ) )
  534. {
  535. $match = get_theme_mod( 'jnews_single_post_related_match', 'category' );
  536. $post_per_page = get_theme_mod( 'jnews_single_number_post_related', 5 );
  537. $related_amp = '';
  538.  
  539. $category = $tag = $result = array();
  540.  
  541. if ( $match === 'category' )
  542. {
  543. $this->recursive_category( get_the_category(), $result );
  544.  
  545. if ( $result )
  546. {
  547. foreach ( $result as $cat )
  548. {
  549. $category[] = $cat->term_id;
  550. }
  551. }
  552.  
  553. } elseif ( $match === 'tag' ) {
  554.  
  555. $tags = get_the_tags();
  556.  
  557. if ( $tags )
  558. {
  559. foreach ( $tags as $cat )
  560. {
  561. $tag[] = $cat->term_id;
  562. }
  563. }
  564.  
  565. }
  566.  
  567. $attr = array(
  568. 'post_type' => array( 'post' ),
  569. 'pagination_number_post' => $post_per_page,
  570. 'number_post' => $post_per_page,
  571. 'include_category' => implode( ',', $category ),
  572. 'include_tag' => implode( ',', $tag ),
  573. 'exclude_post' => get_the_ID(),
  574. 'sort_by' => 'latest',
  575. 'post_offset' => 0,
  576. );
  577.  
  578. $result = JNews\Module\ModuleQuery::do_query( $attr );
  579. $contents = $result['result'];
  580.  
  581. if ( !empty( $contents ) )
  582. {
  583. $related_content = '';
  584.  
  585. foreach( $contents as $content )
  586. {
  587. $author = $content->post_author;
  588. $author_name = get_the_author_meta( 'display_name', $author );
  589. $date = jeg_get_post_date( null, $content );
  590. $image = '';
  591.  
  592. if ( has_post_thumbnail( $content->ID ) )
  593. {
  594. $image = get_the_post_thumbnail_url( $content->ID, 'jnews-120x86' );
  595. $image = "<amp-img src='{$image}' width='120' height='86' layout='responsive' class='amp-related-image'></amp-img>";
  596. }
  597.  
  598. $related_content .=
  599. "<div class='amp-related-content'>
  600. {$image}
  601. <div class='amp-related-text'>
  602. <h3><a href='" . get_permalink( $content->ID ) . "'>{$content->post_title}</a></h3>
  603. <div class='amp-related-meta'>
  604. " . jnews_return_translation( 'By', 'jnews-amp', 'by' ) . "
  605. <span class='amp-related-author'>{$author_name}</span>
  606. <span class='amp-related-date'>{$date}</span>
  607. </div>
  608. </div>
  609. </div>";
  610. }
  611.  
  612. $related_amp =
  613. "<div class='amp-related-wrapper'>
  614. <h2>" . jnews_return_translation( 'Related Content','jnews-amp', 'related_content' ) . "</h2>
  615. {$related_content}
  616. </div>";
  617. }
  618.  
  619. echo $related_amp;
  620. }
  621. }
  622.  
  623. /**
  624. * Get category list of post
  625. *
  626. * @param array $categories
  627. * @param array &$result
  628. *
  629. */
  630. protected function recursive_category( $categories, &$result )
  631. {
  632. foreach ( $categories as $category )
  633. {
  634. $result[] = $category;
  635. $children = get_categories( array( 'parent' => $category->term_id ) );
  636.  
  637. if ( !empty( $children ) )
  638. {
  639. $this->recursive_category( $children, $result );
  640. }
  641. }
  642. }
  643.  
  644. protected function header_search_form()
  645. {
  646. $top_element = get_theme_mod('jnews_hb_element_mobile_drawer_top_center', jnews_header_default("drawer_element_top"));
  647. $bottom_element = get_theme_mod('jnews_hb_element_mobile_drawer_bottom_center', jnews_header_default("drawer_element_bottom"));
  648. $elements = array_merge( $top_element, $bottom_element );
  649.  
  650. if ( in_array( 'search_form', $elements ) )
  651. {
  652. return true;
  653. }
  654.  
  655. return false;
  656. }
  657.  
  658. public function share_main_button( $main_button )
  659. {
  660. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() )
  661. {
  662. foreach ( $main_button as $key => $value )
  663. {
  664. if ( $value['social_share'] == 'wechat' )
  665. {
  666. unset( $main_button[$key] );
  667. }
  668. }
  669. }
  670.  
  671. return $main_button;
  672. }
  673. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement