Guest User

Untitled

a guest
Mar 24th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tagdiv
  5. * Date: 19.10.2015
  6. * Time: 15:04
  7. *
  8. * @package td-mobile-plugin
  9. */
  10.  
  11. /**
  12. * Class td_mobile_theme
  13. * Helper class used to interface the used detection library.
  14. */
  15. class td_mobile_theme {
  16.  
  17. const TDM_MOBILE_ON_DESKTOP = 'tdm_mobile_on_desktop';
  18.  
  19. private static $td_theme_options = '';
  20.  
  21. /**
  22. * Directory name of the mobile theme.
  23. * @var $mobile_dir string
  24. */
  25. private static $mobile_dir = '/mobile';
  26.  
  27. /**
  28. * The detector object.
  29. * @var object $mobile_detect object
  30. */
  31. private static $mobile_detect;
  32.  
  33. /**
  34. * The absolute path to the mobile theme.
  35. * @var string $mobile_dir_path
  36. */
  37. static $mobile_dir_path;
  38.  
  39. /**
  40. * The remained uri path to the mobile theme.
  41. * @var string $mobile_uri_path
  42. */
  43. static $mobile_uri_path;
  44.  
  45. /**
  46. * The full url path to the main theme (It's internally used. It's public just for wp hook callback)
  47. * It's computed on the 'setup_theme' wp hook, before mobile theme switching.
  48. * @var string $main_uri_path
  49. */
  50. static $main_uri_path;
  51.  
  52. static $main_dir_path;
  53.  
  54. /**
  55. * this function returns a theme option
  56. * @note this is similar with the theme @td_util::get_option but defined here because at the time the plugin is loaded and the mobile theme needs to be set we can't use @td_util::get_option
  57. * @param $option_name - theme option name
  58. *
  59. * @return string - the theme option value
  60. */
  61. static function get_option($option_name){
  62.  
  63. $theme_options = get_option(self::$td_theme_options);
  64. if ( !empty( $theme_options[$option_name] ) ) {
  65. return $theme_options[$option_name];
  66. } else {
  67. return '';
  68. }
  69.  
  70. }
  71.  
  72. /**
  73. * this function sets the theme options name @see td_mobile_theme::$td_theme_options
  74. * @param $theme_name
  75. */
  76. public static function set_theme_options($theme_name) {
  77.  
  78. switch ($theme_name) {
  79. case 'Newspaper':
  80. self::$td_theme_options = 'td_011';
  81. break;
  82. case 'Newsmag':
  83. self::$td_theme_options = 'td_010';
  84. break;
  85. }
  86.  
  87. }
  88.  
  89. /**
  90. * this function checks if the default wp amp plugins is enabled
  91. * @return bool - true if amp is installed and enabled, false otherwise or if the old td-amp plugin is active
  92. */
  93. static function is_amp_plugin_installed() {
  94. if ( defined('AMP__VERSION') && ! defined('TD_AMP') ) {
  95. return true;
  96. }
  97.  
  98. return false;
  99. }
  100.  
  101. /**
  102. * this function uses the requests global to determine if a amp request was made
  103. * @return bool - true if amp, false otherwise
  104. */
  105. static function is_amp_request() {
  106. global $_REQUEST;
  107.  
  108. if ( self::is_amp_plugin_installed() && isset( $_REQUEST[AMP_Theme_Support::SLUG] ) ){
  109. return true;
  110. }
  111.  
  112. return false;
  113. }
  114.  
  115. /**
  116. * It detects the mobile.
  117. * For some situations, especially for debugging, to force the mobile version of the theme for desktop agents.
  118. * For this, it's enough to set the mobile version from theme panel and than call the function with $force_mobile_on_destop = true.
  119. * Without parameter, this function detects just the mobile version.
  120. *
  121. * Example of $force_mobile_on_desktop = true, @see td-mobile-plugin.php
  122. * The callback of the the_content wp hook:
  123. * add_filter('the_content', 'td_mobile_the_content', 999);
  124. *
  125. * It gets the mobile content of a page, on desktop agent.
  126. *
  127. * @param bool $force_mobile_on_desktop - Force the mobile version of the theme.
  128. *
  129. * @return bool
  130. */
  131. public static function is_mobile($force_mobile_on_desktop = false) {
  132. if (!isset(self::$mobile_detect)) {
  133. self::$mobile_detect = new Mobile_Detect();
  134. }
  135.  
  136. $current_theme = wp_get_theme();
  137. if ($current_theme !== null && is_a($current_theme, 'WP_Theme') === true) {
  138. $the_stylesheet = $current_theme->get_stylesheet();
  139.  
  140. if (empty($the_stylesheet)) {
  141. $removed = remove_action('option_stylesheet', array('td_mobile_theme', 'mobile'));
  142. $the_stylesheet = get_option('stylesheet');
  143.  
  144. if ($removed) {
  145. add_action('option_stylesheet', array('td_mobile_theme', 'mobile'));
  146. }
  147. }
  148. $theme_options = get_option('td_' . $the_stylesheet);
  149.  
  150. $use_mobile_version_on_desktop = '';
  151. $option_key = self::TDM_MOBILE_ON_DESKTOP;
  152.  
  153. if (is_array($theme_options) and array_key_exists($option_key, $theme_options)) {
  154. $use_mobile_version_on_desktop = $theme_options[ $option_key ];
  155. }
  156.  
  157. //self::$is_mobile = ((self::$mobile_detect->isMobile() and ! self::$mobile_detect->isTablet()) or ! empty($use_mobile_version_on_desktop)) and ! is_admin();
  158. return ($force_mobile_on_desktop && !empty($use_mobile_version_on_desktop)) || (self::$mobile_detect->isMobile() && !self::$mobile_detect->isTablet());
  159. }
  160. return false;
  161. }
  162.  
  163. /**
  164. * Sets the
  165. * 'theme_root',
  166. * 'theme_root_uri',
  167. * 'setup_theme'
  168. * for the mobile theme.
  169. * These must be the necessary hooks to set up the theme settings (also see the 'setup_theme' callback function)
  170. * The priority must be higher than any other previous settings, to overwrite them.
  171. */
  172. private static function set_mobile_theme() {
  173. // 'stylesheet_directory' has been added here to solve theme.json issue for mobile theme. Without this it is trying to find
  174. // the json file NOT inside of main theme directory path
  175. add_action('stylesheet_directory', array(__CLASS__, 'mobile_stylesheet_directory'), 10, 3);
  176. add_action('template_directory', array(__CLASS__, 'mobile_stylesheet_directory'), 10, 3);
  177.  
  178. add_action('setup_theme', array(__CLASS__, 'mobile_theme_setup'), 999);
  179. add_filter('theme_root', array(__CLASS__, 'mobile_theme_root'), 999, 1);
  180. add_filter('theme_root_uri', array(__CLASS__, 'mobile_theme_root_uri'), 999, 1);
  181.  
  182. /**
  183. * hook here to check the post mobile theme status
  184. */
  185. add_action('setup_theme', function (){
  186. global $_SERVER;
  187.  
  188. $req_scheme = 'http';
  189. if(is_ssl()){
  190. $req_scheme = 'https';
  191. }
  192.  
  193. $post_id = url_to_postid( $req_scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
  194.  
  195. // get the mob theme status from post meta
  196. $status = get_post_meta( $post_id, 'tdm_status', true );
  197.  
  198. // if the mobile theme is disabled for this post remove the mobile theme setup actions/filters
  199. if ( $status && $status === 'disabled' ) {
  200. remove_action('setup_theme', array(__CLASS__, 'mobile_theme_setup'), 999);
  201. remove_filter('theme_root', array(__CLASS__, 'mobile_theme_root'), 999);
  202. remove_filter('theme_root_uri', array(__CLASS__, 'mobile_theme_root_uri'), 999);
  203. remove_filter('the_content', 'td_mobile_get_the_content_for_vc', 9);
  204. }else{
  205. remove_action('template_directory', array(__CLASS__, 'mobile_stylesheet_directory'));
  206. }
  207.  
  208. }, 998);
  209. }
  210.  
  211. /**
  212. * IF amp options are enabled from panel this function sets the paired mode and loads the mobile theme accordingly
  213. * IF amp options are disabled, this sets and loads the mobile theme settings, on mobile devices
  214. */
  215. static function set_the_theme() {
  216.  
  217. if ( self::get_option('tdm_amp') !== '' ) {
  218.  
  219. if ( self::is_amp_plugin_installed() ) {
  220.  
  221. // accept sanitization
  222. add_filter( 'amp_validation_error_sanitized', function (){ return true; } );
  223.  
  224. // this removes the amp options > validation submenu pages from admin menu && other validation messages
  225. add_action('init', function() {
  226. add_action( 'admin_menu', function (){
  227. remove_submenu_page( 'amp-options', 'edit.php?post_type=amp_validated_url' );
  228. remove_submenu_page( 'amp-options', esc_attr('edit-tags.php?taxonomy=amp_validation_error&post_type=amp_validated_url') );
  229. }, 999);
  230. add_action( 'wp_loaded', function (){
  231. remove_action( 'edit_form_top', 'AMP_Validation_Manager::print_edit_form_validation_status');
  232. remove_action( 'edit_form_top', 'AMP_Validated_URL_Post_Type::print_url_as_title');
  233. remove_action( 'all_admin_notices', 'AMP_Validation_Manager::print_plugin_notice' );
  234.  
  235. remove_filter( 'dashboard_glance_items', 'AMP_Validated_URL_Post_Type::filter_dashboard_glance_items');
  236. remove_action( 'rightnow_end', 'AMP_Validated_URL_Post_Type::print_dashboard_glance_styles');
  237.  
  238. remove_action( 'enqueue_block_editor_assets', 'AMP_Validation_Manager::enqueue_block_validation');
  239. remove_action( 'admin_bar_menu', 'AMP_Validation_Manager::add_admin_bar_menu_items', 101);
  240.  
  241. // remove support for cloud templates post type
  242. remove_post_type_support( 'tdb_templates', AMP_Post_Type_Support::SLUG );
  243.  
  244. // disable the all_templates_supported option to allow users to manually set amp support for templates and content types
  245. if ( AMP_Options_Manager::get_option( 'all_templates_supported' ) === true ) {
  246. AMP_Options_Manager::update_option( 'all_templates_supported', false );
  247. }
  248.  
  249. // automatically set transitionl mode ( amp plugins ver 1.0.2 support )
  250. if ( AMP_Options_Manager::get_option( 'theme_support' ) !== 'transitional' ) {
  251. AMP_Options_Manager::update_option( 'theme_support', 'transitional' );
  252. } else {
  253. add_action( 'admin_print_styles', function (){
  254. ?>
  255.  
  256. <style type='text/css'>
  257. .amp-website-mode fieldset {
  258. display: none;
  259. }
  260. </style>
  261.  
  262. <?php
  263.  
  264. });
  265. add_action('admin_footer', function(){
  266. ?>
  267.  
  268. <script>
  269. ( function () {
  270. jQuery('.amp-website-mode > td').html('<div class="notice notice-info notice-alt inline"><p>Your install is configured via theme to use the Trasitional mode.</p></div>');
  271. }())
  272. </script>
  273.  
  274. <?php
  275. });
  276. }
  277.  
  278. // prevent amp validation on posts save
  279. remove_action( 'save_post', 'AMP_Validation_Manager::handle_save_post_prompting_validation' ); // classic editor
  280. remove_action( 'rest_api_init', 'AMP_Validation_Manager::add_rest_api_fields' ); // blocks editor
  281.  
  282. });
  283. });
  284.  
  285. // on admin add theme support for amp
  286. if ( is_admin() ) {
  287. /*
  288. if( wp_doing_ajax() ) {
  289. if ( self::is_amp_request() ) {
  290. self::set_mobile_theme();
  291. }
  292. }
  293. */
  294.  
  295. add_action( 'td_global_after', function (){
  296. // this adds theme support for amp in paired mode on admin
  297. add_theme_support(
  298. AMP_Theme_Support::SLUG,
  299. array(
  300. 'paired' => true,
  301. )
  302. );
  303. });
  304.  
  305. return;
  306. }
  307.  
  308. // if 'Mobile Theme on Amp & Mobile' option is enabled from panel,
  309. // the mobile theme amp support is loaded on mobile devices & on ?amp requests
  310. if ( self::get_option('tdm_amp') === 'mobile_amp' ) {
  311.  
  312. add_theme_support(
  313. AMP_Theme_Support::SLUG,
  314. array(
  315. 'paired' => true,
  316. )
  317. );
  318.  
  319. if( self::is_mobile(true) ) {
  320. self::set_mobile_theme();
  321. } else {
  322. if( self::is_amp_request() ) {
  323. self::set_mobile_theme();
  324. }
  325. }
  326. }
  327.  
  328. // if 'Mobile Theme Just On Amp' option is enabled from panel,
  329. // the mobile theme amp support is loaded just on ?amp requests
  330. if( self::get_option('tdm_amp') === 'amp' ) {
  331.  
  332. add_theme_support(
  333. AMP_Theme_Support::SLUG,
  334. array(
  335. 'paired' => true,
  336. )
  337. );
  338.  
  339. if ( self::is_amp_request() ) {
  340. self::set_mobile_theme();
  341. }
  342. }
  343.  
  344. } else {
  345. // if the default wp amp plugin is not active reset mobile theme amp settings
  346. // we hook to 'td_global_after' to have access to theme's td_util
  347. add_action( 'td_global_after', function (){
  348. td_util::update_option( 'tdm_amp', '' );
  349. });
  350. }
  351.  
  352. } else {
  353.  
  354. // this function disables amp if the default wp amp plugin is used with the mobile theme plugin and amp settings are disabled in the mobile theme
  355. add_filter( 'amp_is_enabled', function ( $is_enabled ) {
  356. if ( self::is_amp_plugin_installed() ) {
  357. $is_enabled = false;
  358. }
  359. return $is_enabled;
  360. } );
  361.  
  362. // deprecated
  363. //if ( self::is_amp_plugin_installed() ) {
  364. // add_action( 'after_setup_theme', function (){
  365. // add_filter( 'amp_is_enabled', '__return_false' );
  366. // }, 4);
  367. //}
  368.  
  369. if ( self::is_mobile(true) && !is_admin() && !wp_is_json_request() ) {
  370. self::set_mobile_theme();
  371. }
  372. }
  373. }
  374.  
  375. /**
  376. * Helper function used by the 'wp_cache_check_mobile' function of the wp super cache plugin, to determine
  377. * the proper cache key
  378. *
  379. * @param string $cache_key - the current cache key.
  380. *
  381. * @return string
  382. */
  383. static function get_theme_setting($cache_key) {
  384. if (self::is_mobile()) {
  385. return 'mobile';
  386. }
  387. return 'normal';
  388. }
  389.  
  390. /**
  391. * Custom hook function used for the following wp hooks:
  392. * 'stylesheet',
  393. * 'template',
  394. * 'option_template',
  395. * 'option_stylesheet'
  396. *
  397. * @param string $theme - theme id.
  398. *
  399. * @return mixed
  400. */
  401. static function mobile($theme) {
  402. return apply_filters('td_mobile', '', $theme);
  403. }
  404.  
  405. static function mobile_stylesheet_directory_uri($stylesheet_dir_uri, $stylesheet, $theme_root_uri) {
  406. return rtrim($stylesheet_dir_uri, '/');
  407. }
  408.  
  409. static function mobile_stylesheet_directory($stylesheet_dir, $stylesheet, $theme_root) {
  410. $main_theme = wp_get_theme();
  411. $main_dir_path = $main_theme->get_template_directory();
  412. if ( td_mobile_theme::get_option('tdm_is_multisite') === 'yes' && is_multisite() ) {
  413. $main_dir_path = $main_theme->theme_root . '/themes' . get_template_directory();
  414. }
  415. $mobile_dir_path = TDC_PATH . self::$mobile_dir;
  416.  
  417. if ( file_exists( $mobile_dir_path ) ) {
  418. return $main_dir_path;
  419. }
  420. return $stylesheet_dir;
  421. }
  422.  
  423. /**
  424. * Hook function used for the wp 'theme_root' hook.
  425. *
  426. * @param string $theme_root - theme root path.
  427. *
  428. * @return string
  429. */
  430. static function mobile_theme_root($theme_root) {
  431. return self::$mobile_dir_path;
  432. }
  433.  
  434. /**
  435. * Hook function used for the wp 'theme_root_uri' hook.
  436. *
  437. * @param string $theme_root_uri - theme URI path.
  438. *
  439. * @return string
  440. */
  441. static function mobile_theme_root_uri($theme_root_uri) {
  442. if (isset(self::$mobile_uri_path)) {
  443. return self::$mobile_uri_path;
  444. }
  445. return $theme_root_uri;
  446. }
  447.  
  448. /**
  449. * Hook function used for the wp 'setup_theme' hook.
  450. * It executes before 'theme_root' and 'theme_root_uri'.
  451. *
  452. * The 'stylesheet', 'template', 'option_template', 'option_stylesheet' wp hooks are modified, so
  453. * any existing settings can be saved before changes.
  454. *
  455. * The mobile theme directory path is set. It's too late to compute it on 'theme_root' or 'theme_root_uri' hooks,
  456. * because they are called after 'theme_setup'.
  457. */
  458. static function mobile_theme_setup() {
  459. $main_theme = wp_get_theme();
  460. $main_dir_path = $main_theme->get_template_directory();
  461. if ( td_mobile_theme::get_option('tdm_is_multisite') === 'yes' && is_multisite() ) {
  462. $main_dir_path = $main_theme->theme_root . '/themes' . get_template_directory();
  463. }
  464. $mobile_dir_path = TDC_PATH . self::$mobile_dir;
  465.  
  466. if ( file_exists( $mobile_dir_path ) ) {
  467.  
  468. self::$main_dir_path = $main_dir_path;
  469. self::$mobile_dir_path = $mobile_dir_path;
  470.  
  471. self::$main_uri_path = get_template_directory_uri();
  472. self::$mobile_uri_path = TDC_URL . self::$mobile_dir;
  473.  
  474. } else {
  475. echo "The mobile theme path couldn't be set. Please check if you already have another mobile theme plugin active and disable it (ex. Jetpack Mobile Theme or similar). If you use a cache plugin clear it before checking the results.";
  476. die;
  477. }
  478.  
  479. /*
  480. echo PHP_EOL;
  481. print_r(self::$main_dir_path);
  482. echo PHP_EOL;
  483. print_r(self::$mobile_dir_path);
  484. echo PHP_EOL;
  485. print_r(self::$main_uri_path);
  486. echo PHP_EOL;
  487. print_r(self::$mobile_uri_path);
  488. echo PHP_EOL;
  489. */
  490.  
  491. add_action('stylesheet', array(__CLASS__, 'mobile'));
  492. add_action('template', array(__CLASS__, 'mobile'));
  493. add_action('option_template', array(__CLASS__, 'mobile'));
  494. add_action('option_stylesheet', array(__CLASS__, 'mobile'));
  495.  
  496. add_action('stylesheet_directory_uri', array(__CLASS__, 'mobile_stylesheet_directory_uri'), 10, 3);
  497. }
  498. }
  499.  
Add Comment
Please, Sign In to add comment