| 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 The 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( $disabled || in_array( $post_type, $this->options['post_types'], true ), true, false ), |
| 111 |
disabled( $disabled, true, false ), |
| 112 |
esc_html( |
| 113 |
sprintf( |
| 114 |
/* translators: 1 is a post type or taxonomy label, 2 is a post type or taxonomy key. */ |
| 115 |
_x( '%1$s (%2$s)', 'content type setting choice', 'polylang' ), |
| 116 |
$pt->labels->name, |
| 117 |
$pt->name |
| 118 |
) |
| 119 |
) |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
?> |
| 124 |
</ul> |
| 125 |
<p class="description"><?php esc_html_e( 'Activate languages and translations for custom post types.', 'polylang' ); ?></p> |
| 126 |
<?php |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $this->taxonomies ) ) { |
| 130 |
?> |
| 131 |
<h4><?php esc_html_e( 'Custom taxonomies', 'polylang' ); ?></h4> |
| 132 |
<ul class="pll-inline-block-list"> |
| 133 |
<?php |
| 134 |
foreach ( $this->taxonomies as $taxonomy ) { |
| 135 |
$tax = get_taxonomy( $taxonomy ); |
| 136 |
if ( ! empty( $tax ) ) { |
| 137 |
$disabled = in_array( $taxonomy, $this->disabled_taxonomies ); |
| 138 |
printf( |
| 139 |
'<li><label><input name="taxonomies[%s]" type="checkbox" value="1" %s %s/> %s</label></li>', |
| 140 |
esc_attr( $taxonomy ), |
| 141 |
checked( $disabled || in_array( $taxonomy, $this->options['taxonomies'], true ), true, false ), |
| 142 |
disabled( $disabled, true, false ), |
| 143 |
esc_html( |
| 144 |
sprintf( |
| 145 |
/* translators: 1 is a post type or taxonomy label, 2 is a post type or taxonomy key. */ |
| 146 |
_x( '%1$s (%2$s)', 'content type setting choice', 'polylang' ), |
| 147 |
$tax->labels->name, |
| 148 |
$tax->name |
| 149 |
) |
| 150 |
) |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
?> |
| 155 |
</ul> |
| 156 |
<p class="description"><?php esc_html_e( 'Activate languages and translations for custom taxonomies.', 'polylang' ); ?></p> |
| 157 |
<?php |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Prepares the received data before saving. |
| 163 |
* |
| 164 |
* @since 3.7 |
| 165 |
* |
| 166 |
* @param array $options Raw values to save. |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
protected function prepare_raw_data( array $options ): array { |
| 170 |
$newoptions = array(); |
| 171 |
|
| 172 |
foreach ( array( 'post_types', 'taxonomies' ) as $key ) { |
| 173 |
$newoptions[ $key ] = empty( $options[ $key ] ) ? array() : array_keys( $options[ $key ], 1 ); |
| 174 |
} |
| 175 |
|
| 176 |
return $newoptions; // Take care to return only validated options. |
| 177 |
} |
| 178 |
} |
| 179 |
|