PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/polylang/admin/admin-base.php

https://gitlab.com/hop23typhu/bryepoxy
PHP | 319 lines | 166 code | 40 blank | 113 comment | 37 complexity | a8ce7f43e5c3767b3e7a910b4e8cdd8a MD5 | raw file
  1. <?php
  2. /**
  3. * Base class for both admin
  4. *
  5. * @since 1.8
  6. */
  7. class PLL_Admin_Base extends PLL_Base {
  8. public $filter_lang, $curlang, $pref_lang;
  9. /**
  10. * Loads the polylang text domain
  11. * Setups actions needed on all admin pages
  12. *
  13. * @since 1.8
  14. *
  15. * @param object $links_model
  16. */
  17. public function __construct( &$links_model ) {
  18. parent::__construct( $links_model );
  19. // Plugin i18n, only needed for backend
  20. load_plugin_textdomain( 'polylang', false, basename( POLYLANG_DIR ).'/languages' );
  21. // Adds the link to the languages panel in the WordPress admin menu
  22. add_action( 'admin_menu', array( $this, 'add_menus' ) );
  23. // Setup js scripts and css styles
  24. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  25. add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) );
  26. // Lingotek
  27. if ( ! defined( 'PLL_LINGOTEK_AD' ) || PLL_LINGOTEK_AD ) {
  28. require_once( POLYLANG_DIR . '/lingotek/lingotek.php' );
  29. }
  30. }
  31. /**
  32. * Setups filters and action needed on all admin pages and on plugins page
  33. * Loads the settings pages or the filters base on the request
  34. *
  35. * @since 1.2
  36. *
  37. * @param object $links_model
  38. */
  39. public function init() {
  40. if ( ! $this->model->get_languages_list() ) {
  41. return;
  42. }
  43. $this->links = new PLL_Admin_Links( $this ); // FIXME needed here ?
  44. $this->static_pages = new PLL_Admin_Static_Pages( $this ); // FIXME needed here ?
  45. $this->filters_links = new PLL_Filters_Links( $this ); // FIXME needed here ?
  46. // Filter admin language for users
  47. // We must not call user info before WordPress defines user roles in wp-settings.php
  48. add_filter( 'setup_theme', array( $this, 'init_user' ) );
  49. add_filter( 'request', array( $this, 'request' ) );
  50. // Adds the languages in admin bar
  51. add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 ); // 100 determines the position
  52. }
  53. /**
  54. * Adds the link to the languages panel in the WordPress admin menu
  55. *
  56. * @since 0.1
  57. */
  58. public function add_menus() {
  59. add_submenu_page( 'options-general.php', $title = __( 'Languages', 'polylang' ), $title, 'manage_options', 'mlang', '__return_null' );
  60. }
  61. /**
  62. * Setup js scripts & css styles ( only on the relevant pages )
  63. *
  64. * @since 0.6
  65. */
  66. public function admin_enqueue_scripts() {
  67. $screen = get_current_screen();
  68. if ( empty( $screen ) ) {
  69. return;
  70. }
  71. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  72. // For each script:
  73. // 0 => the pages on which to load the script
  74. // 1 => the scripts it needs to work
  75. // 2 => 1 if loaded even if languages have not been defined yet, 0 otherwise
  76. // 3 => 1 if loaded in footer
  77. // FIXME: check if I can load more scripts in footer
  78. $scripts = array(
  79. 'post' => array( array( 'post', 'media', 'async-upload', 'edit' ), array( 'jquery', 'wp-ajax-response', 'post', 'jquery-ui-autocomplete' ), 0 , 1 ),
  80. 'media' => array( array( 'upload' ), array( 'jquery' ), 0 , 1 ),
  81. 'term' => array( array( 'edit-tags', 'term' ), array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), 0, 1 ),
  82. 'user' => array( array( 'profile', 'user-edit' ), array( 'jquery' ), 0 , 0 ),
  83. );
  84. foreach ( $scripts as $script => $v ) {
  85. if ( in_array( $screen->base, $v[0] ) && ( $v[2] || $this->model->get_languages_list() ) ) {
  86. wp_enqueue_script( 'pll_' . $script, POLYLANG_URL . '/js/' . $script . $suffix . '.js', $v[1], POLYLANG_VERSION, $v[3] );
  87. }
  88. }
  89. wp_enqueue_style( 'polylang_admin', POLYLANG_URL . '/css/admin' . $suffix . '.css', array(), POLYLANG_VERSION );
  90. }
  91. /**
  92. * Sets pll_ajax_backend on all backend ajax request
  93. * The final goal is to detect if an ajax request is made on admin or frontend
  94. *
  95. * Takes care to various situations:
  96. * when the ajax request has no options.data thanks to ScreenfeedFr
  97. * see: https://wordpress.org/support/topic/ajaxprefilter-may-not-work-as-expected
  98. * when options.data is a json string
  99. * see: https://wordpress.org/support/topic/polylang-breaking-third-party-ajax-requests-on-admin-panels
  100. * when options.data is an empty string (GET request with the method 'load')
  101. * see: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation
  102. *
  103. * @since 1.4
  104. */
  105. public function admin_print_footer_scripts() {
  106. global $post_ID;
  107. $params = array( 'pll_ajax_backend' => 1 );
  108. if ( ! empty( $post_ID ) ) {
  109. $params = array_merge( $params, array( 'pll_post_id' => (int) $post_ID ) );
  110. }
  111. $str = http_build_query( $params );
  112. $arr = json_encode( $params );
  113. ?>
  114. <script type="text/javascript">
  115. if (typeof jQuery != 'undefined') {
  116. (function($){
  117. $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
  118. if ( -1 != options.url.indexOf( ajaxurl ) ) {
  119. if ( 'undefined' === typeof options.data ) {
  120. options.data = ( 'get' === options.type.toLowerCase() ) ? '<?php echo $str;?>' : <?php echo $arr;?>;
  121. } else {
  122. if ( 'string' === typeof options.data ) {
  123. if ( '' === options.data && 'get' === options.type.toLowerCase() ) {
  124. options.url = options.url+'&<?php echo $str;?>';
  125. } else {
  126. try {
  127. o = $.parseJSON(options.data);
  128. o = $.extend(o, <?php echo $arr;?>);
  129. options.data = JSON.stringify(o);
  130. }
  131. catch(e) {
  132. options.data = '<?php echo $str;?>&'+options.data;
  133. }
  134. }
  135. } else {
  136. options.data = $.extend(options.data, <?php echo $arr;?>);
  137. }
  138. }
  139. }
  140. });
  141. })(jQuery)
  142. }
  143. </script><?php
  144. }
  145. /**
  146. * Sets the admin current language, used to filter the content
  147. *
  148. * @since 2.0
  149. */
  150. public function set_current_language() {
  151. $this->curlang = $this->filter_lang;
  152. // Edit Post
  153. if ( isset( $_REQUEST['pll_post_id'] ) ) {
  154. $this->curlang = $this->model->post->get_language( (int) $_REQUEST['pll_post_id'] );
  155. } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && is_numeric( $_GET['post'] ) ) {
  156. $this->curlang = $this->model->post->get_language( (int) $_GET['post'] );
  157. } elseif ( 'post-new.php' === $GLOBALS['pagenow'] ) {
  158. $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( $_GET['new_lang'] );
  159. }
  160. // Edit Term
  161. // FIXME 'edit-tags.php' for backward compatibility with WP < 4.5
  162. elseif ( in_array( $GLOBALS['pagenow'], array( 'edit-tags.php', 'term.php' ) ) && isset( $_GET['tag_ID'] ) ) {
  163. $this->curlang = $this->model->term->get_language( (int) $_GET['tag_ID'] );
  164. } elseif ( 'edit-tags.php' === $GLOBALS['pagenow'] ) {
  165. if ( ! empty( $_GET['new_lang'] ) ) {
  166. $this->curlang = $this->model->get_language( $_GET['new_lang'] );
  167. } elseif ( empty( $this->curlang ) ) {
  168. $this->curlang = $this->pref_lang;
  169. }
  170. }
  171. // Ajax
  172. if ( defined( 'DOING_AJAX' ) && DOING_AJAX && ! empty( $_REQUEST['lang'] ) ) {
  173. $this->curlang = $this->model->get_language( $_REQUEST['lang'] );
  174. }
  175. }
  176. /**
  177. * Defines the backend language and the admin language filter based on user preferences
  178. *
  179. * @since 1.2.3
  180. */
  181. public function init_user() {
  182. // Backend locale
  183. add_filter( 'locale', array( $this, 'get_locale' ) );
  184. // Language for admin language filter: may be empty
  185. // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter
  186. if ( ! defined( 'DOING_AJAX' ) && ! empty( $_GET['lang'] ) && ! is_numeric( $_GET['lang'] ) && current_user_can( 'edit_user', $user_id = get_current_user_id() ) ) {
  187. update_user_meta( $user_id, 'pll_filter_content', ( $lang = $this->model->get_language( $_GET['lang'] ) ) ? $lang->slug : '' );
  188. }
  189. $this->filter_lang = $this->model->get_language( get_user_meta( get_current_user_id(), 'pll_filter_content', true ) );
  190. // Set preferred language for use when saving posts and terms: must not be empty
  191. $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_language( $this->options['default_lang'] ) : $this->filter_lang;
  192. /**
  193. * Filter the preferred language on amin side
  194. * The preferred language is used for example to determine the language of a new post
  195. *
  196. * @since 1.2.3
  197. *
  198. * @param object $pref_lang preferred language
  199. */
  200. $this->pref_lang = apply_filters( 'pll_admin_preferred_language', $this->pref_lang );
  201. $this->set_current_language();
  202. // Inform that the admin language has been set
  203. // Only if the admin language is one of the Polylang defined language
  204. if ( $curlang = $this->model->get_language( get_locale() ) ) {
  205. $GLOBALS['text_direction'] = $curlang->is_rtl ? 'rtl' : 'ltr'; // force text direction according to language setting
  206. /** This action is documented in frontend/choose-lang.php */
  207. do_action( 'pll_language_defined', $curlang->slug, $curlang );
  208. }
  209. else {
  210. /** This action is documented in include/class-polylang.php */
  211. do_action( 'pll_no_language_defined' ); // to load overriden textdomains
  212. }
  213. }
  214. /**
  215. * Avoids parsing a tax query when all languages are requested
  216. * Fixes https://wordpress.org/support/topic/notice-undefined-offset-0-in-wp-includesqueryphp-on-line-3877 introduced in WP 4.1
  217. * @see the suggestion of @boonebgorges, https://core.trac.wordpress.org/ticket/31246
  218. *
  219. * @since 1.6.5
  220. *
  221. * @param array $qvars
  222. * @return array
  223. */
  224. public function request( $qvars ) {
  225. if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) {
  226. unset( $qvars['lang'] );
  227. }
  228. return $qvars;
  229. }
  230. /**
  231. * Get the locale based on user preference
  232. *
  233. * @since 0.4
  234. *
  235. * @param string $locale
  236. * @return string modified locale
  237. */
  238. public function get_locale( $locale ) {
  239. return ( $loc = get_user_meta( get_current_user_id(), 'user_lang', 'true' ) ) ? $loc : $locale;
  240. }
  241. /**
  242. * Adds the languages list in admin bar for the admin languages filter
  243. *
  244. * @since 0.9
  245. *
  246. * @param object $wp_admin_bar
  247. */
  248. public function admin_bar_menu( $wp_admin_bar ) {
  249. $url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  250. $all_item = (object) array(
  251. 'slug' => 'all',
  252. 'name' => __( 'Show all languages', 'polylang' ),
  253. 'flag' => '<span class="ab-icon"></span>',
  254. );
  255. $selected = empty( $this->filter_lang ) ? $all_item : $this->filter_lang;
  256. $title = sprintf(
  257. '<span class="ab-label"%s>%s</span>',
  258. 'all' === $selected->slug ? '' : sprintf( ' lang="%s"', esc_attr( $selected->get_locale( 'display' ) ) ),
  259. esc_html( $selected->name )
  260. );
  261. $wp_admin_bar->add_menu( array(
  262. 'id' => 'languages',
  263. 'title' => $selected->flag . $title,
  264. 'meta' => array( 'title' => __( 'Filters content by language', 'polylang' ) ),
  265. ) );
  266. foreach ( array_merge( array( $all_item ), $this->model->get_languages_list() ) as $lang ) {
  267. if ( $selected->slug === $lang->slug ) {
  268. continue;
  269. }
  270. $wp_admin_bar->add_menu( array(
  271. 'parent' => 'languages',
  272. 'id' => $lang->slug,
  273. 'title' => $lang->flag . esc_html( $lang->name ),
  274. 'href' => esc_url( add_query_arg( 'lang', $lang->slug, remove_query_arg( 'paged', $url ) ) ),
  275. 'meta' => 'all' === $lang->slug ? array() : array( 'lang' => esc_attr( $lang->get_locale( 'display' ) ) ),
  276. ) );
  277. }
  278. }
  279. }