PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / integrations / jetpack / featured-content.php

featured-content.php in Polylang 3.1.4, at integrations/jetpack/featured-content.php

122 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Manages the compatibility with the Jetpack Twenty Fourteenn Featured content
8 *
9 * @since 2.4
10 */
11 class PLL_Featured_Content {
12 /**
13 * Constructor
14 *
15 * @since 2.6
16 */
17 public function init() {
18 add_filter( 'transient_featured_content_ids', array( $this, 'featured_content_ids' ) );
19 add_filter( 'option_featured-content', array( $this, 'option_featured_content' ) );
20 }
21
22 /**
23 * Tell whether the theme supports the featured content
24 *
25 * @since 2.4
26 *
27 * @return bool
28 */
29 protected function is_active() {
30 return 'twentyfourteen' === get_template() || ( defined( 'JETPACK__VERSION' ) && get_theme_support( 'featured-content' ) );
31 }
32
33 /**
34 * Get the theme featured posts filter name
35 *
36 * @since 2.4
37 *
38 * @return string
39 */
40 protected function get_featured_posts_filter() {
41 $theme_support = get_theme_support( 'featured-content' );
42
43 if ( isset( $theme_support[0]['featured_content_filter'] ) ) {
44 $theme_support[0]['filter'] = $theme_support[0]['featured_content_filter'];
45 unset( $theme_support[0]['featured_content_filter'] );
46 }
47
48 return $theme_support[0]['filter'];
49 }
50
51 /**
52 * Rewrites the function Featured_Content::get_featured_post_ids()
53 *
54 * @since 1.4
55 *
56 * @param array $featured_ids Featured posts ids
57 * @return array modified featured posts ids ( include all languages )
58 */
59 public function featured_content_ids( $featured_ids ) {
60 if ( ! $this->is_active() || false !== $featured_ids ) {
61 return $featured_ids;
62 }
63
64 $settings = Featured_Content::get_setting();
65
66 if ( ! $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ) ) {
67 return $featured_ids;
68 }
69
70 // Get featured tag translations
71 $tags = PLL()->model->term->get_translations( $term->term_id );
72 $ids = array();
73
74 // Query for featured posts in all languages
75 // One query per language to get the correct number of posts per language
76 foreach ( $tags as $tag ) {
77 $args = array(
78 'lang' => 0, // Avoid language filters.
79 'fields' => 'ids',
80 'numberposts' => Featured_Content::$max_posts,
81 'tax_query' => array(
82 array(
83 'taxonomy' => 'post_tag',
84 'terms' => (int) $tag,
85 ),
86 ),
87 );
88
89 // Available in Jetpack, but not in Twenty Fourteen.
90 if ( isset( Featured_Content::$post_types ) ) {
91 $args['post_type'] = Featured_Content::$post_types;
92 }
93
94 $_ids = get_posts( $args );
95 $ids = array_merge( $ids, $_ids );
96 }
97
98 $ids = array_map( 'absint', $ids );
99 set_transient( 'featured_content_ids', $ids );
100
101 return $ids;
102 }
103
104 /**
105 * Translates the featured tag id in featured content settings
106 * Mainly to allow hiding it when requested in featured content options
107 * Acts only on frontend
108 *
109 * @since 1.4
110 *
111 * @param array $settings featured content settings
112 * @return array modified $settings
113 */
114 public function option_featured_content( $settings ) {
115 if ( $this->is_active() && PLL() instanceof PLL_Frontend && $settings['tag-id'] && $tr = pll_get_term( $settings['tag-id'] ) ) {
116 $settings['tag-id'] = $tr;
117 }
118
119 return $settings;
120 }
121 }
122