PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/ait-languages/settings/settings.php

https://bitbucket.org/pblazhuk/beanera
PHP | 347 lines | 184 code | 48 blank | 115 comment | 23 complexity | d8144f90378c229c3c20fae4e8a88be1 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MIT
  1. <?php
  2. /**
  3. * A class for the Polylang settings pages
  4. * accessible in $polylang global object
  5. *
  6. * properties:
  7. * options => inherited, reference to Polylang options array
  8. * model => inherited, reference to PLL_Model object
  9. * links_model => inherited, reference to PLL_Links_Model object
  10. * links => inherited, reference to PLL_Admin_Links object
  11. * static_pages => inherited, reference to PLL_Admin_Static_Pages object
  12. * filters_links => inherited, reference to PLL_Filters_Links object
  13. * curlang => inherited, optional, current language used to filter admin content
  14. * pref_lang => inherited, preferred language used as default when saving posts or terms
  15. *
  16. * @since 1.2
  17. */
  18. class PLL_Settings extends PLL_Admin_Base {
  19. protected $active_tab, $modules;
  20. /**
  21. * Constructor
  22. *
  23. * @since 1.2
  24. *
  25. * @param object $links_model
  26. */
  27. public function __construct( &$links_model ) {
  28. parent::__construct( $links_model );
  29. $this->active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'lang';
  30. PLL_Admin_Strings::init();
  31. // FIXME put this as late as possible
  32. add_action( 'admin_init', array( $this, 'register_settings_modules' ) );
  33. /*ait*/
  34. add_action('admin_init', function(){
  35. // adds screen options and the about box in the languages admin panel
  36. add_action( 'load-' . AitLanguages::$pageSlug, array( $this, 'load_page' ) );
  37. });
  38. // saves per-page value in screen option
  39. add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
  40. }
  41. /**
  42. * Initializes the modules
  43. *
  44. * @since 1.8
  45. */
  46. public function register_settings_modules() {
  47. $modules = array(
  48. 'PLL_Settings_Tools',
  49. 'PLL_Settings_Licenses',
  50. );
  51. if ( $this->model->get_languages_list() ) {
  52. $modules = array_merge( array(
  53. 'PLL_Settings_Url',
  54. 'PLL_Settings_Browser',
  55. 'PLL_Settings_Media',
  56. 'PLL_Settings_CPT',
  57. 'PLL_Settings_Sync',
  58. 'PLL_Settings_WPML',
  59. 'PLL_Settings_Share_Slug',
  60. 'PLL_Settings_Translate_Slugs',
  61. ), $modules );
  62. }
  63. /**
  64. * Filter the list of setting modules
  65. *
  66. * @since 1.8
  67. *
  68. * @param array $modules the list of module classes
  69. */
  70. $modules = apply_filters( 'pll_settings_modules', $modules );
  71. foreach ( $modules as $key => $class ) {
  72. $key = is_numeric( $key ) ? strtolower( str_replace( 'PLL_Settings_', '', $class ) ) : $key;
  73. $this->modules[ $key ] = new $class( $this );
  74. }
  75. }
  76. /**
  77. * Adds the link to the languages panel in the WordPress admin menu
  78. *
  79. * @since 0.1
  80. */
  81. public function add_menus() {
  82. add_submenu_page( 'admin.php', $title = __( 'Languages', 'polylang' ), $title, 'manage_options', 'mlang', array( $this, 'languages_page' ) );
  83. }
  84. /**
  85. * Loads the about metabox
  86. *
  87. * @since 0.8
  88. */
  89. public function metabox_about() {
  90. include( PLL_SETTINGS_INC.'/view-about.php' );
  91. }
  92. /**
  93. * Adds screen options and the about box in the languages admin panel
  94. *
  95. * @since 0.9.5
  96. */
  97. public function load_page() {
  98. // test of $this->active_tab avoids displaying the automatically generated screen options on other tabs
  99. switch ( $this->active_tab ) {
  100. case 'lang':
  101. if ( ! defined( 'PLL_DISPLAY_ABOUT' ) || PLL_DISPLAY_ABOUT ) {
  102. add_meta_box(
  103. 'pll-about-box',
  104. __( 'About Polylang', 'polylang' ),
  105. array( $this, 'metabox_about' ),
  106. 'settings_page_mlang',
  107. 'normal'
  108. );
  109. }
  110. add_screen_option( 'per_page', array(
  111. 'label' => __( 'Languages', 'polylang' ),
  112. 'default' => 10,
  113. 'option' => 'pll_lang_per_page',
  114. ) );
  115. add_action( 'admin_notices', array( $this, 'notice_objects_with_no_lang' ) );
  116. break;
  117. case 'strings':
  118. add_screen_option( 'per_page', array(
  119. 'label' => __( 'Strings translations', 'polylang' ),
  120. 'default' => 10,
  121. 'option' => 'pll_strings_per_page',
  122. ) );
  123. break;
  124. }
  125. }
  126. /**
  127. * Save the "Views/Uploads per page" option set by this user
  128. *
  129. * @since 0.9.5
  130. *
  131. * @param mixed $status false or value returned by previous filter
  132. * @param string $option Name of the option being changed
  133. * @param string $value Value of the option
  134. *
  135. * @return string New value if this is our option, otherwise nothing
  136. */
  137. public function set_screen_option( $status, $option, $value ) {
  138. return 'pll_lang_per_page' === $option || 'pll_strings_per_page' === $option ? $value : $status;
  139. }
  140. /**
  141. * Manages the user input for the languages pages
  142. *
  143. * @since 1.9
  144. *
  145. * @param string $action
  146. */
  147. public function handle_actions( $action ) {
  148. switch ( $action ) {
  149. case 'add':
  150. check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
  151. if ( $this->model->add_language( $_POST ) && 'en_US' !== $_POST['locale'] ) {
  152. // attempts to install the language pack
  153. require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
  154. if ( ! wp_download_language_pack( $_POST['locale'] ) ) {
  155. add_settings_error( 'general', 'pll_download_mo', __( 'The language was created, but the WordPress language file was not downloaded. Please install it manually.', 'polylang' ) );
  156. }
  157. // force checking for themes and plugins translations updates
  158. wp_clean_themes_cache();
  159. wp_clean_plugins_cache();
  160. }
  161. self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
  162. break;
  163. case 'delete':
  164. check_admin_referer( 'delete-lang' );
  165. if ( ! empty( $_GET['lang'] ) ) {
  166. $this->model->delete_language( (int) $_GET['lang'] );
  167. }
  168. self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
  169. break;
  170. case 'update':
  171. check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
  172. $error = $this->model->update_language( $_POST );
  173. self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
  174. break;
  175. case 'default-lang':
  176. check_admin_referer( 'default-lang' );
  177. if ( $lang = $this->model->get_language( (int) $_GET['lang'] ) ) {
  178. $this->model->update_default_lang( $lang->slug );
  179. }
  180. self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
  181. break;
  182. case 'content-default-lang':
  183. check_admin_referer( 'content-default-lang' );
  184. if ( $nolang = $this->model->get_objects_with_no_lang() ) {
  185. if ( ! empty( $nolang['posts'] ) ) {
  186. $this->model->set_language_in_mass( 'post', $nolang['posts'], $this->options['default_lang'] );
  187. }
  188. if ( ! empty( $nolang['terms'] ) ) {
  189. $this->model->set_language_in_mass( 'term', $nolang['terms'], $this->options['default_lang'] );
  190. }
  191. }
  192. self::redirect(); // to refresh the page ( possible thanks to the $_GET['noheader']=true )
  193. break;
  194. case 'activate':
  195. check_admin_referer( 'pll_activate' );
  196. $this->modules[ $_GET['module'] ]->activate();
  197. self::redirect();
  198. break;
  199. case 'deactivate':
  200. check_admin_referer( 'pll_deactivate' );
  201. $this->modules[ $_GET['module'] ]->deactivate();
  202. self::redirect();
  203. break;
  204. default:
  205. /**
  206. * Fires when a non default action has been sent to Polylang settings
  207. *
  208. * @since 1.8
  209. */
  210. do_action( "mlang_action_$action" );
  211. break;
  212. }
  213. }
  214. /**
  215. * Displays the 3 tabs pages: languages, strings translations, settings
  216. * also manages user input for these pages
  217. *
  218. * @since 0.1
  219. */
  220. public function languages_page() {
  221. // prepare the list of tabs
  222. $tabs = array( 'lang' => __( 'Languages','polylang' ) );
  223. // only if at least one language has been created
  224. if ( $listlanguages = $this->model->get_languages_list() ) {
  225. $tabs['strings'] = __( 'Strings translations', 'polylang' );
  226. }
  227. $tabs['settings'] = __( 'Settings', 'polylang' );
  228. /**
  229. * Filter the list of tabs in Polylang settings
  230. *
  231. * @since 1.5.1
  232. *
  233. * @param array $tabs list of tab names
  234. */
  235. $tabs = apply_filters( 'pll_settings_tabs', $tabs );
  236. switch ( $this->active_tab ) {
  237. case 'lang':
  238. // prepare the list table of languages
  239. $list_table = new PLL_Table_Languages();
  240. $list_table->prepare_items( $listlanguages );
  241. break;
  242. case 'strings':
  243. $string_table = new PLL_Table_String( $listlanguages );
  244. $string_table->prepare_items();
  245. break;
  246. }
  247. // handle user input
  248. $action = isset( $_REQUEST['pll_action'] ) ? $_REQUEST['pll_action'] : '';
  249. if ( 'edit' === $action && ! empty( $_GET['lang'] ) ) {
  250. $edit_lang = $this->model->get_language( (int) $_GET['lang'] );
  251. } else {
  252. $this->handle_actions( $action );
  253. }
  254. // displays the page
  255. include( PLL_SETTINGS_INC.'/view-languages.php' );
  256. }
  257. /**
  258. * Enqueues scripts and styles
  259. */
  260. public function admin_enqueue_scripts() {
  261. parent::admin_enqueue_scripts();
  262. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  263. wp_enqueue_script( 'pll_admin', POLYLANG_URL .'/js/admin'.$suffix.'.js', array( 'jquery', 'wp-ajax-response', 'postbox', 'jquery-ui-selectmenu' ), POLYLANG_VERSION );
  264. wp_localize_script( 'pll_admin', 'pll_flag_base_url', POLYLANG_URL . '/flags/' );
  265. wp_enqueue_style( 'pll_selectmenu', POLYLANG_URL .'/css/selectmenu'.$suffix.'.css', array(), POLYLANG_VERSION );
  266. }
  267. /**
  268. * Displays a notice when there are objects with no language assigned
  269. *
  270. * @since 1.8
  271. */
  272. public function notice_objects_with_no_lang() {
  273. if ( ! empty( $this->options['default_lang'] ) && $this->model->get_objects_with_no_lang() ) {
  274. printf(
  275. '<div class="error"><p>%s <a href="%s">%s</a></p></div>',
  276. esc_html__( 'There are posts, pages, categories or tags without language.', 'polylang' ),
  277. wp_nonce_url( '?page=mlang&amp;pll_action=content-default-lang&amp;noheader=true', 'content-default-lang' ),
  278. esc_html__( 'You can set them all to the default language.', 'polylang' )
  279. );
  280. }
  281. }
  282. /**
  283. * Redirects to language page ( current active tab )
  284. * saves error messages in a transient for reuse in redirected page
  285. *
  286. * @since 1.5
  287. *
  288. * @param array $args query arguments to add to the url
  289. */
  290. static public function redirect( $args = array() ) {
  291. if ( $errors = get_settings_errors() ) {
  292. set_transient( 'settings_errors', $errors, 30 );
  293. $args['settings-updated'] = 1;
  294. }
  295. // remove possible 'pll_action' and 'lang' query args from the referer before redirecting
  296. wp_safe_redirect( add_query_arg( $args, remove_query_arg( array( 'pll_action', 'lang' ), wp_get_referer() ) ) );
  297. exit;
  298. }
  299. }