PluginProbe
Polylang / 3.0.1
Polylang v3.0.1
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 / settings / settings-cpt.php

settings-cpt.php in Polylang 3.0.1, at settings/settings-cpt.php

163 lines 4.6 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 * Settings class for custom post types and taxonomies language and translation management
8 *
9 * @since 1.8
10 */
11 class PLL_Settings_CPT extends PLL_Settings_Module {
12 /**
13 * Stores the display order priority.
14 *
15 * @var int
16 */
17 public $priority = 40;
18
19 /**
20 * The list of post types to show in the form.
21 *
22 * @var string[]
23 */
24 private $post_types;
25
26 /**
27 * The list of post types to disable in the form.
28 *
29 * @var string[]
30 */
31 private $disabled_post_types;
32
33 /**
34 * The list of taxonomies to show in the form.
35 *
36 * @var string[]
37 */
38 private $taxonomies;
39
40 /**
41 * The list of taxonomies to disable in the form.
42 *
43 * @var string[]
44 */
45 private $disabled_taxonomies;
46
47 /**
48 * Constructor
49 *
50 * @since 1.8
51 *
52 * @param object $polylang polylang object
53 */
54 public function __construct( &$polylang ) {
55 parent::__construct(
56 $polylang,
57 array(
58 'module' => 'cpt',
59 'title' => __( 'Custom post types and Taxonomies', 'polylang' ),
60 'description' => __( 'Activate languages and translations management for the custom post types and the taxonomies.', 'polylang' ),
61 )
62 );
63
64 $public_post_types = get_post_types( array( 'public' => true, '_builtin' => false ) );
65 /** This filter is documented in include/model.php */
66 $this->post_types = array_unique( apply_filters( 'pll_get_post_types', $public_post_types, true ) );
67
68 /** This filter is documented in include/model.php */
69 $programmatically_active_post_types = array_unique( apply_filters( 'pll_get_post_types', array(), false ) );
70 $this->disabled_post_types = array_intersect( $programmatically_active_post_types, $this->post_types );
71
72 $public_taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ) );
73 $public_taxonomies = array_diff( $public_taxonomies, get_taxonomies( array( '_pll' => true ) ) );
74 /** This filter is documented in include/model.php */
75 $this->taxonomies = array_unique( apply_filters( 'pll_get_taxonomies', $public_taxonomies, true ) );
76
77 /** This filter is documented in include/model.php */
78 $programmatically_active_taxonomies = array_unique( apply_filters( 'pll_get_taxonomies', array(), false ) );
79 $this->disabled_taxonomies = array_intersect( $programmatically_active_taxonomies, $this->taxonomies );
80 }
81
82 /**
83 * Tells if the module is active
84 *
85 * @since 1.8
86 *
87 * @return bool
88 */
89 public function is_active() {
90 return ! empty( $this->post_types ) || ! empty( $this->taxonomies );
91 }
92
93 /**
94 * Displays the settings form
95 *
96 * @since 1.8
97 */
98 protected function form() {
99 if ( ! empty( $this->post_types ) ) {?>
100 <h4><?php esc_html_e( 'Custom post types', 'polylang' ); ?></h4>
101 <ul class="pll-inline-block-list">
102 <?php
103 foreach ( $this->post_types as $post_type ) {
104 $pt = get_post_type_object( $post_type );
105 if ( ! empty( $pt ) ) {
106 $disabled = in_array( $post_type, $this->disabled_post_types );
107 printf(
108 '<li><label><input name="post_types[%s]" type="checkbox" value="1" %s %s/> %s</label></li>',
109 esc_attr( $post_type ),
110 checked( in_array( $post_type, $this->options['post_types'] ) || $disabled, true, false ),
111 disabled( $disabled, true, false ),
112 esc_html( $pt->labels->name )
113 );
114 }
115 }
116 ?>
117 </ul>
118 <p class="description"><?php esc_html_e( 'Activate languages and translations for custom post types.', 'polylang' ); ?></p>
119 <?php
120 }
121
122 if ( ! empty( $this->taxonomies ) ) {
123 ?>
124 <h4><?php esc_html_e( 'Custom taxonomies', 'polylang' ); ?></h4>
125 <ul class="pll-inline-block-list">
126 <?php
127 foreach ( $this->taxonomies as $taxonomy ) {
128 $tax = get_taxonomy( $taxonomy );
129 if ( ! empty( $tax ) ) {
130 $disabled = in_array( $taxonomy, $this->disabled_taxonomies );
131 printf(
132 '<li><label><input name="taxonomies[%s]" type="checkbox" value="1" %s %s/> %s</label></li>',
133 esc_attr( $taxonomy ),
134 checked( in_array( $taxonomy, $this->options['taxonomies'] ) || $disabled, true, false ),
135 disabled( $disabled, true, false ),
136 esc_html( $tax->labels->name )
137 );
138 }
139 }
140 ?>
141 </ul>
142 <p class="description"><?php esc_html_e( 'Activate languages and translations for custom taxonomies.', 'polylang' ); ?></p>
143 <?php
144 }
145 }
146
147 /**
148 * Sanitizes the settings before saving
149 *
150 * @since 1.8
151 *
152 * @param array $options
153 */
154 protected function update( $options ) {
155 $newoptions = array();
156
157 foreach ( array( 'post_types', 'taxonomies' ) as $key ) {
158 $newoptions[ $key ] = empty( $options[ $key ] ) ? array() : array_keys( $options[ $key ], 1 );
159 }
160 return $newoptions; // Take care to return only validated options
161 }
162 }
163