| @@ -1,48 +1,73 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | - * Admin side controller | |
| 5 | - * accessible in $polylang global object | |
| 7 | + * Main Polylang class for admin (except Polylang pages), accessible from @see PLL(). | |
| 6 | 8 | * |
| 7 | - * Properties: | |
| 8 | - * options => inherited, reference to Polylang options array | |
| 9 | - * model => inherited, reference to PLL_Model object | |
| 10 | - * links_model => inherited, reference to PLL_Links_Model object | |
| 11 | - * links => inherited, reference to PLL_Admin_Links object | |
| 12 | - * static_pages => inherited, reference to PLL_Admin_Static_Pages object | |
| 13 | - * filters_links => inherited, reference to PLL_Filters_Links object | |
| 14 | - * curlang => inherited, optional, current language used to filter the content (language of the post or term being edited, equal to filter_lang otherwise) | |
| 15 | - * filter_lang => inherited, optional, current status of the admin languages filter (in the admin bar) | |
| 16 | - * pref_lang => inherited, preferred language used as default when saving posts or terms | |
| 17 | - * posts => reference to PLL_CRUD_Posts object | |
| 18 | - * terms => reference to PLL_CRUD_Terms object | |
| 19 | - * filters => reference to PLL_Admin_Filters object | |
| 20 | - * filters_columns => reference to PLL_Admin_Filters_Columns object | |
| 21 | - * filters_post => reference to PLL_Admin_Filters_Post object | |
| 22 | - * filters_term => reference to PLL_Admin_filters_Term object | |
| 23 | - * nav_menu => reference to PLL_Admin_Nav_Menu object | |
| 24 | - * sync => reference to PLL_Admin_Sync object | |
| 25 | - * block_editor => reference to PLL_Admin_Block_Editor object | |
| 26 | - * classic_editor => reference to PLL_Admin_Classic_Editor object | |
| 27 | - * filters_media => optional, reference to PLL_Admin_Filters_Media object | |
| 28 | - * bulk_translate => reference, a PLL_Bulk_Translate subclass instance | |
| 29 | - * wizard => reference, a PLL_Wizard object | |
| 30 | - * | |
| 31 | 9 | * @since 1.2 |
| 32 | - * @since 2.7 Added a reference to a PLL_Bulk_Translate instance. | |
| 33 | - * @since 2.7 Added a reference to a PLL_Wizard object. | |
| 34 | 10 | */ |
| 35 | 11 | class PLL_Admin extends PLL_Admin_Base { |
| 36 | - public $filters, $filters_columns, $filters_post, $filters_term, $nav_menu, $sync, $filters_media; | |
| 12 | + /** | |
| 13 | + * @var PLL_Admin_Filters|null | |
| 14 | + */ | |
| 15 | + public $filters; | |
| 37 | 16 | |
| 38 | 17 | /** |
| 39 | - * Loads the polylang text domain | |
| 40 | - * Setups filters and action needed on all admin pages and on plugins page | |
| 18 | + * @var PLL_Admin_Filters_Columns|null | |
| 19 | + */ | |
| 20 | + public $filters_columns; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * @var PLL_Admin_Filters_Post|null | |
| 24 | + */ | |
| 25 | + public $filters_post; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * @var PLL_Admin_Filters_Term|null | |
| 29 | + */ | |
| 30 | + public $filters_term; | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * @var PLL_Admin_Filters_Media|null | |
| 34 | + */ | |
| 35 | + public $filters_media; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @since 2.9 | |
| 41 | 39 | * |
| 40 | + * @var PLL_Filters_Sanitization|null | |
| 41 | + */ | |
| 42 | + public $filters_sanitization; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @var PLL_Admin_Block_Editor|null | |
| 46 | + */ | |
| 47 | + public $block_editor; | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * @var PLL_Admin_Classic_Editor|null | |
| 51 | + */ | |
| 52 | + public $classic_editor; | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * @var PLL_Admin_Nav_Menu|null | |
| 56 | + */ | |
| 57 | + public $nav_menu; | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * @var PLL_Admin_Filters_Widgets_Options|null | |
| 61 | + */ | |
| 62 | + public $filters_widgets_options; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Setups filters and action needed on all admin pages and on plugins page. | |
| 66 | + * | |
| 42 | 67 | * @since 1.2 |
| 43 | 68 | * |
| 44 | - * @param object $links_model | |
| 69 | + * @param PLL_Links_Model $links_model Reference to the links model. | |
| 45 | 70 | */ |
| 46 | 71 | public function __construct( &$links_model ) { |
| 47 | 72 | parent::__construct( $links_model ); |
| 48 | 73 | |
| @@ -63,19 +88,18 @@ | ||
| 63 | 88 | // Setup filters for admin pages |
| 64 | 89 | // Priority 5 to make sure filters are there before customize_register is fired |
| 65 | 90 | if ( $this->model->get_languages_list() ) { |
| 66 | 91 | add_action( 'wp_loaded', array( $this, 'add_filters' ), 5 ); |
| 67 | - add_action( 'admin_init', array( $this, 'maybe_load_sync_post' ), 20 ); // After fusion Builder. | |
| 68 | 92 | } |
| 69 | 93 | } |
| 70 | 94 | |
| 71 | 95 | /** |
| 72 | - * Adds a 'settings' link in the plugins table | |
| 96 | + * Adds a 'settings' link for our plugin in the plugins list table. | |
| 73 | 97 | * |
| 74 | 98 | * @since 0.1 |
| 75 | 99 | * |
| 76 | - * @param array $links list of links associated to the plugin | |
| 77 | - * @return array modified list of links | |
| 100 | + * @param string[] $links List of links associated to the plugin. | |
| 101 | + * @return string[] Modified list of links. | |
| 78 | 102 | */ |
| 79 | 103 | public function plugin_action_links( $links ) { |
| 80 | 104 | array_unshift( $links, '<a href="admin.php?page=mlang">' . __( 'Settings', 'polylang' ) . '</a>' ); |
| 81 | 105 | return $links; |
| @@ -87,8 +111,9 @@ | ||
| 87 | 111 | * @since 1.1.6 |
| 88 | 112 | * |
| 89 | 113 | * @param array $plugin_data Not used |
| 90 | 114 | * @param object $r Plugin update data |
| 115 | + * @return void | |
| 91 | 116 | */ |
| 92 | 117 | public function plugin_update_message( $plugin_data, $r ) { |
| 93 | 118 | if ( isset( $r->upgrade_notice ) ) { |
| 94 | 119 | printf( '<p style="margin: 3px 0 0 0; border-top: 1px solid #ddd; padding-top: 3px">%s</p>', esc_html( $r->upgrade_notice ) ); |
| @@ -99,12 +124,16 @@ | ||
| 99 | 124 | * Setup filters for admin pages |
| 100 | 125 | * |
| 101 | 126 | * @since 1.2 |
| 102 | 127 | * @since 2.7 instantiate a PLL_Bulk_Translate instance. |
| 128 | + * @return void | |
| 103 | 129 | */ |
| 104 | 130 | public function add_filters() { |
| 131 | + $this->filters_sanitization = new PLL_Filters_Sanitization( $this->get_locale_for_sanitization() ); | |
| 132 | + $this->filters_widgets_options = new PLL_Admin_Filters_Widgets_Options( $this ); | |
| 133 | + | |
| 105 | 134 | // All these are separated just for convenience and maintainability |
| 106 | - $classes = array( 'Filters', 'Filters_Columns', 'Filters_Post', 'Filters_Term', 'Nav_Menu', 'Sync', 'Classic_Editor', 'Block_Editor' ); | |
| 135 | + $classes = array( 'Filters', 'Filters_Columns', 'Filters_Post', 'Filters_Term', 'Nav_Menu', 'Classic_Editor', 'Block_Editor' ); | |
| 107 | 136 | |
| 108 | 137 | // Don't load media filters if option is disabled or if user has no right |
| 109 | 138 | if ( $this->options['media_support'] && ( $obj = get_post_type_object( 'attachment' ) ) && ( current_user_can( $obj->cap->edit_posts ) || current_user_can( $obj->cap->create_posts ) ) ) { |
| 110 | 139 | $classes[] = 'Filters_Media'; |
| @@ -122,88 +151,30 @@ | ||
| 122 | 151 | */ |
| 123 | 152 | $class = apply_filters( 'pll_' . $obj, 'PLL_Admin_' . $class ); |
| 124 | 153 | $this->$obj = new $class( $this ); |
| 125 | 154 | } |
| 126 | - | |
| 127 | - $this->posts = new PLL_CRUD_Posts( $this ); | |
| 128 | - $this->terms = new PLL_CRUD_Terms( $this ); | |
| 129 | - | |
| 130 | - // Bulk Translate | |
| 131 | - // Needs to be loaded before other modules. | |
| 132 | - if ( class_exists( 'PLL_Bulk_Translate' ) ) { | |
| 133 | - $this->bulk_translate = new PLL_Bulk_Translate( $this->model ); | |
| 134 | - add_action( 'current_screen', array( $this->bulk_translate, 'init' ) ); | |
| 135 | - } | |
| 136 | - | |
| 137 | - // Advanced media | |
| 138 | - if ( $this->options['media_support'] && class_exists( 'PLL_Admin_Advanced_Media' ) ) { | |
| 139 | - $this->advanced_media = new PLL_Admin_Advanced_Media( $this ); | |
| 140 | - } | |
| 141 | - | |
| 142 | - // Share term slugs | |
| 143 | - if ( get_option( 'permalink_structure' ) && $this->options['force_lang'] && class_exists( 'PLL_Admin_Share_Term_Slug' ) ) { | |
| 144 | - $this->share_term_slug = new PLL_Admin_Share_Term_Slug( $this ); | |
| 145 | - } | |
| 146 | - | |
| 147 | - // Duplicate content | |
| 148 | - if ( class_exists( 'PLL_Duplicate' ) ) { | |
| 149 | - $this->duplicate = new PLL_Duplicate( $this ); | |
| 150 | - } | |
| 151 | - | |
| 152 | - if ( class_exists( 'PLL_Duplicate_REST' ) ) { | |
| 153 | - $this->duplicate_rest = new PLL_Duplicate_REST(); | |
| 154 | - } | |
| 155 | - | |
| 156 | - if ( class_exists( 'PLL_Sync_Post_Model' ) ) { | |
| 157 | - $this->sync_post_model = new PLL_Sync_Post_Model( $this ); | |
| 158 | - } | |
| 159 | - | |
| 160 | - // Block editor metabox | |
| 161 | - if ( pll_use_block_editor_plugin() ) { | |
| 162 | - $this->block_editor_plugin = new PLL_Block_Editor_Plugin( $this ); | |
| 163 | - } | |
| 164 | - | |
| 165 | - // FIXME: Specific for WP CRON and WP CLI as the action admin_init is not fired. | |
| 166 | - // Waiting for a better way to handle the cases without loading the complete admin. | |
| 167 | - if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
| 168 | - $this->maybe_load_sync_post(); | |
| 169 | - } | |
| 170 | - | |
| 171 | 155 | } |
| 172 | 156 | |
| 173 | 157 | /** |
| 174 | - * Load the post synchronization object, depending on the editor in use. | |
| 158 | + * Retrieve the locale according to the current language instead of the language | |
| 159 | + * of the admin interface. | |
| 175 | 160 | * |
| 176 | - * @since 2.6 | |
| 161 | + * @since 2.0 | |
| 162 | + * | |
| 163 | + * @return string | |
| 177 | 164 | */ |
| 178 | - public function maybe_load_sync_post() { | |
| 179 | - // Post synchronization | |
| 180 | - if ( 'post-new.php' === $GLOBALS['pagenow'] && function_exists( 'use_block_editor_for_post' ) ) { | |
| 181 | - // We need to wait until we know which editor is in use | |
| 182 | - add_filter( 'use_block_editor_for_post', array( $this, '_maybe_load_sync_post' ), 999 ); // After the plugin Classic Editor | |
| 183 | - } elseif ( 'post.php' === $GLOBALS['pagenow'] && function_exists( 'use_block_editor_for_post' ) && isset( $_GET['post'] ) && empty( $_GET['meta-box-loader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 184 | - $this->_maybe_load_sync_post( use_block_editor_for_post( (int) $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 185 | - } else { | |
| 186 | - $this->_maybe_load_sync_post( false ); | |
| 187 | - } | |
| 188 | - } | |
| 165 | + public function get_locale_for_sanitization() { | |
| 166 | + $locale = get_locale(); | |
| 189 | 167 | |
| 190 | - /** | |
| 191 | - * Load the post synchronization object, depending on the editor in use. | |
| 192 | - * | |
| 193 | - * @since 2.6 | |
| 194 | - * | |
| 195 | - * @param bool $is_block_editor Whether to use the block editor or not. | |
| 196 | - * @return bool | |
| 197 | - */ | |
| 198 | - public function _maybe_load_sync_post( $is_block_editor ) { | |
| 199 | - if ( ! isset( $this->sync_post ) ) { // Make sure to instantiate the class only once, as the function may be called from a filter. | |
| 200 | - if ( class_exists( 'PLL_Sync_Post_REST' ) && pll_use_block_editor_plugin() && $is_block_editor ) { | |
| 201 | - $this->sync_post = new PLL_Sync_Post_REST( $this ); | |
| 202 | - } elseif ( class_exists( 'PLL_Sync_Post' ) ) { | |
| 203 | - $this->sync_post = new PLL_Sync_Post( $this ); | |
| 204 | - } | |
| 168 | + if ( isset( $_POST['post_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 169 | + $locale = $lang->locale; | |
| 170 | + } elseif ( isset( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 171 | + $locale = $lang->locale; | |
| 172 | + } elseif ( isset( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 173 | + $locale = $lang->locale; | |
| 174 | + } elseif ( ! empty( $this->curlang ) ) { | |
| 175 | + $locale = $this->curlang->locale; | |
| 205 | 176 | } |
| 206 | 177 | |
| 207 | - return $is_block_editor; | |
| 178 | + return $locale; | |
| 208 | 179 | } |
| 209 | 180 | } |