Guest User

Untitled

a guest
Jun 13th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 128.01 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * globals to hold the state for all pages
  5. */
  6. global $tdb_state_single_page, $tdb_state_single, $tdb_state_category, $tdb_state_author, $tdb_state_search, $tdb_state_date, $tdb_state_tag, $tdb_state_attachment;
  7.  
  8. // load the config
  9. require_once "tdb_util.php";
  10. require_once "tdb_config.php";
  11. add_action('tdc_loaded', array('tdb_config', 'on_tdc_loaded'), 10); // the theme runs on 9 priority... so we can change stuff if we want
  12.  
  13.  
  14. require_once "tdb_state_base.php";
  15. require_once "tdb_state_template.php";
  16. require_once "tdb_state_content.php";
  17. require_once "tdb_state.php"; // state for the cloud template plugin, used on single post templates > autoload(inf) posts feature.
  18. require_once "tdb_global_wp_query.php";
  19.  
  20.  
  21. require_once "tdb_module.php";
  22. require_once "tdb_ajax.php";
  23. require_once "tdb_cpt.php"; // load the cpt things
  24. require_once "tdb_menu.php"; // load the td menu shortcode support
  25. require_once "tdb_term_meta.php"; // load taxonomies tdb term meta support
  26.  
  27. // The mobile detection library used.
  28. if ( ! class_exists('Mobile_Detect', false)) {
  29. require_once 'Mobile_Detect.php';
  30. }
  31.  
  32. // make the page state
  33. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/page/tdb_state_single_page.php";
  34. $tdb_state_single_page = new tdb_state_single_page();
  35.  
  36. // make the single post state
  37. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/single/tdb_state_single.php";
  38. $tdb_state_single = new tdb_state_single(); // the state already comes with default data
  39.  
  40. // the category state
  41. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/category/tdb_state_category.php";
  42. $tdb_state_category = new tdb_state_category(); // the state already comes with default data
  43.  
  44. // the author state
  45. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/author/tdb_state_author.php";
  46. $tdb_state_author = new tdb_state_author(); // the state already comes with default data
  47.  
  48. // the search state
  49. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/search/tdb_state_search.php";
  50. $tdb_state_search = new tdb_state_search(); // the state already comes with default data
  51.  
  52. // the date state
  53. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/date/tdb_state_date.php";
  54. $tdb_state_date = new tdb_state_date(); // the state already comes with default data
  55.  
  56. // the tag state
  57. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/tag/tdb_state_tag.php";
  58. $tdb_state_tag = new tdb_state_tag(); // the state already comes with default data
  59.  
  60. // the attachment state
  61. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/attachment/tdb_state_attachment.php";
  62. $tdb_state_attachment = new tdb_state_attachment(); // the state already comes with default data
  63.  
  64. // theme panel vue endpoint
  65. require_once TDB_TEMPLATE_BUILDER_DIR . "/includes/panel/tdb_panel_vue.php";
  66.  
  67. /**
  68. * Load the single state for now
  69. * - template_include runs after template_redirect!
  70. */
  71. require_once TDB_TEMPLATE_BUILDER_DIR . "/state/tdb_state_loader.php";
  72. add_action('template_redirect', array('tdb_state_loader', 'on_template_redirect_load_state')); // we use this for front end. (we need the global wp_query)
  73. add_action('tdc_loaded', array('tdb_state_loader', 'on_tdc_loaded_load_state')); // we use this for ajax and composer iframe. (we don't have the global wp_query while editing)
  74.  
  75. /**
  76. * Set the tdb_state
  77. */
  78. $tdb_action = tdb_util::get_get_val( 'tdb_action' );
  79. if ( false === $tdb_action ) {
  80. tdb_state::set_is_ajax( false );
  81. } else {
  82. tdb_state::set_is_ajax( true );
  83. }
  84.  
  85. /**
  86. * Modify the main query for wp templates pages
  87. * - we need to do this to set the shortcode posts limit and get the right pagination
  88. * - we need this on 'tdc_loaded' beacuse we need to use the is_live_editor_ajax/is_live_editor_iframe methods to check for composer's iframe and ajax rendering blocks @see tdc_state
  89. * - on tdc_init hook where this functions file is loaded we're to early to use this methods
  90. */
  91. add_action('tdc_loaded', function() {
  92.  
  93. /**
  94. * DISQUS plugin compatibility
  95. * this prevents disqus from loading on composer iframe/ajax requests and on autoloaded posts or if posts autoload feature is enabled
  96. */
  97. if ( tdc_state::is_live_editor_iframe() ||
  98. tdc_state::is_live_editor_ajax() ||
  99. tdb_state::is_ajax()
  100. ) {
  101. add_filter( 'dsq_can_load', '__return_false' );
  102. }
  103.  
  104. /**
  105. * DISQUS plugin compatibility
  106. * this hook determines if a cloud single post template uses the 'tdb_single_comments' shortcode
  107. * ..and prevents disqus from trying to load if it doesn't use the 'tdb_single_comments' comments
  108. */
  109. add_action( 'pre_get_posts', function( $query ) {
  110.  
  111. if ( is_admin() ||
  112. ! $query->is_main_query() ||
  113. tdc_state::is_live_editor_ajax() ||
  114. tdc_state::is_live_editor_iframe() ||
  115. td_util::is_mobile_theme()
  116. )
  117. return;
  118.  
  119. if ( is_single() ) {
  120. //print_r($query);
  121.  
  122. if ( isset( $query->query['post_type'] ) && $query->query['post_type'] !== 'post' ) {
  123. return;
  124. }
  125.  
  126. if ( isset( $query->query['name'] ) ) {
  127. $post_obj = get_page_by_path( $query->query['name'], OBJECT, 'post' );
  128. }
  129.  
  130. $post_id = '';
  131. if ( !empty( $post_obj ) ) {
  132. $post_id = $post_obj->ID;
  133. }
  134.  
  135. // check if we have a specific template set on the current post
  136. $td_post_theme_settings = td_util::get_post_meta_array( $post_id, 'td_post_theme_settings' );
  137.  
  138. $tdb_template_id = '';
  139.  
  140. if ( !empty( $td_post_theme_settings['td_post_template'] ) ) {
  141. $single_template_id = $td_post_theme_settings['td_post_template'];
  142.  
  143. if ( td_global::is_tdb_template( $single_template_id ) ) {
  144.  
  145. // make sure the template exists, maybe it was deleted or something
  146. if ( td_global::is_tdb_template( $single_template_id, true ) ) {
  147.  
  148. $tdb_template_id = td_global::tdb_get_template_id($single_template_id);
  149.  
  150. } else {
  151. // just reset the post template here, the panel default post template will kick in and load, if available
  152. $td_post_theme_settings['td_post_template'] = '';
  153. update_post_meta( $post_id, 'td_post_theme_settings', $td_post_theme_settings );
  154. }
  155. }
  156. } else {
  157.  
  158. // read the global setting
  159. $default_template_id = td_util::get_option('td_default_site_post_template');
  160.  
  161. // check the default template
  162. if ( td_global::is_tdb_template( $default_template_id ) ) {
  163.  
  164. // make sure the template exists, maybe it was deleted or something
  165. if ( td_global::is_tdb_template( $default_template_id, true ) ) {
  166.  
  167. // load the default tdb template
  168. $tdb_template_id = td_global::tdb_get_template_id($default_template_id);
  169.  
  170. } else {
  171.  
  172. // if we have an non-existent cloud template update the default site wide post template
  173. td_util::update_option('td_default_site_post_template', '' );
  174. }
  175.  
  176. }
  177. }
  178.  
  179. if ( !empty( $tdb_template_id ) ) {
  180.  
  181. // load the cloud template
  182. $wp_query_template = new WP_Query( array(
  183. 'p' => $tdb_template_id,
  184. 'post_type' => 'tdb_templates',
  185. )
  186. );
  187. }
  188.  
  189. // if we have a template look for the 'tdb_single_comments' shortcode
  190. if ( !empty( $wp_query_template ) && $wp_query_template->have_posts() ) {
  191. $tdb_single_comments = tdb_util::get_shortcode( $wp_query_template->post->post_content, 'tdb_single_comments' );
  192.  
  193. if ( ! $tdb_single_comments || td_util::get_option('tdb_p_autoload_status', 'off' ) === 'on' ) {
  194. add_filter( 'dsq_can_load', '__return_false' );
  195. }
  196. }
  197.  
  198. }
  199.  
  200. });
  201.  
  202. /**
  203. * the autoload(infinite) posts script.
  204. * it's needed and loaded just on frontend
  205. */
  206. if ( !tdb_state::is_ajax() && !tdc_state::is_live_editor_ajax() && !tdc_state::is_live_editor_iframe() && !td_util::is_mobile_theme() ) {
  207. add_filter('wp_enqueue_scripts', function(){
  208. if ( TDB_DEPLOY_MODE == 'dev' ) {
  209. wp_enqueue_script( 'tdbAutoload', TDB_URL . '/assets/js/tdbAutoload.js', array( 'jquery', 'underscore' ), TD_CLOUD_LIBRARY, true);
  210. } else {
  211. wp_enqueue_script( 'tdb_js_posts_autoload', TDB_URL . '/assets/js/js_posts_autoload.min.js', array( 'jquery', 'underscore' ), TD_CLOUD_LIBRARY, true );
  212. }
  213. });
  214. }
  215.  
  216. add_action( 'pre_get_posts', function( $query ) {
  217. if ( ! is_admin() && ! $query->is_main_query() )
  218. return;
  219.  
  220. $orderby = $query->get('orderby');
  221.  
  222. if ( 'tdb_template_type' == $orderby ) {
  223. $query->set( 'meta_key', 'tdb_template_type' );
  224. }
  225.  
  226. $template_type = $query->get('template_type');
  227.  
  228. $settings = array();
  229.  
  230. if ( ! empty( $template_type ) ) {
  231. $settings[] = array(
  232. 'key' => 'tdb_template_type',
  233. 'value' => $template_type
  234. );
  235. }
  236.  
  237. $is_mobile_template = $query->get('is_mobile_template');
  238.  
  239. if ( ! empty( $is_mobile_template ) ) {
  240. if ( 'mobile' === $is_mobile_template ) {
  241. $settings[] = array(
  242. 'key' => 'tdc_is_mobile_template',
  243. 'value' => 1
  244. );
  245. } else if ( 'normal' === $is_mobile_template ) {
  246.  
  247. $settings[] = array(
  248. 'key' => 'tdc_is_mobile_template',
  249. 'value' => 1,
  250. 'compare' => 'NOT EXISTS',
  251. );
  252. }
  253. }
  254.  
  255. if (!empty($settings)) {
  256.  
  257. $final_settings = [];
  258. foreach ($settings as $setting) {
  259. if ( !empty($final_settings)) {
  260. $final_settings['relation'] = 'AND';
  261. }
  262. $final_settings[] = $setting;
  263. }
  264.  
  265. $query->set('meta_query', $final_settings );
  266. }
  267. });
  268.  
  269. /**
  270. * - filter 'wpseo_title' is used by WordPress SEO plugin and, by default, it returns a seo title that hasn't the page number inside of it,
  271. * when it's used on td pages [those who have a custom pagination]. At that seo title is added the page info, and just for pages greater than 1
  272. */
  273. add_action( 'pre_get_posts', function($query) {
  274.  
  275. if( is_page() && $query->is_main_query() ){
  276.  
  277. $page_id = isset($query->queried_object) ? $query->queried_object->ID : $query->query_vars['page_id'];
  278.  
  279. // load the page template
  280. $page_template = new WP_Query(array(
  281. 'p' => $page_id,
  282. 'post_type' => 'page',
  283. )
  284. );
  285.  
  286. if (!empty($page_template)) {
  287.  
  288. // get the page template
  289. $_wp_page_template = get_post_meta($page_id, '_wp_page_template', true);
  290.  
  291. $tdb_is_loop = isset( $page_template->post ) && tdb_util::get_shortcode( $page_template->post->post_content, 'tdb_loop' );
  292. $tdb_is_loop_2 = isset( $page_template->post ) && tdb_util::get_shortcode($page_template->post->post_content, 'tdb_loop_2');
  293.  
  294. // don't apply on page-pagebuilder-latest.php
  295. if ( 'page-pagebuilder-latest.php' !== $_wp_page_template & $tdb_is_loop === true || $tdb_is_loop_2 === true ) {
  296. add_filter('wpseo_title', 'td_wpseo_title_for_loop', 11, 1);
  297.  
  298. function td_wpseo_title_for_loop($seo_title){
  299.  
  300. $td_page = (get_query_var('page')) ? get_query_var('page') : 1; //rewrite the global var
  301. $td_paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //rewrite the global var
  302.  
  303. if ($td_paged > $td_page) {
  304. $local_paged = $td_paged;
  305. } else {
  306. $local_paged = $td_page;
  307. }
  308.  
  309. // the custom title is when the pagination is greater than 1
  310. if ($local_paged > 1) {
  311. return $seo_title . ' - ' . __td('Page', TD_THEME_NAME) . ' ' . $local_paged;
  312. }
  313.  
  314. return $seo_title;
  315. }
  316. }
  317. }
  318.  
  319. }
  320.  
  321. } );
  322.  
  323. /**
  324. * Head canonical links on pages with numbered pagination (loop shortcode).
  325. *
  326. */
  327. add_action( 'wp_head', function() {
  328.  
  329. global $tdb_state_single_page;
  330.  
  331. if( is_main_query() && is_page() ){
  332.  
  333. global $wp_query;
  334.  
  335. $page_id = isset($wp_query->queried_object) ? $wp_query->queried_object->ID : $wp_query->query_vars['page_id'];
  336.  
  337. // load the page template
  338. $page_template = new WP_Query(array(
  339. 'p' => $page_id,
  340. 'post_type' => 'page',
  341. )
  342. );
  343.  
  344. if (! empty($page_template)) {
  345.  
  346. // get the page template
  347. $_wp_page_template = get_post_meta($page_id, '_wp_page_template', true);
  348.  
  349. $tdb_is_loop_pagination = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop','ajax_pagination');
  350. $tdb_is_loop_2_pagination = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop_2','ajax_pagination');
  351.  
  352. // don't apply on page-pagebuilder-latest.php
  353. if ( 'page-pagebuilder-latest.php' !== $_wp_page_template && $tdb_is_loop_pagination === 'numbered' ) {
  354.  
  355. $atts = array();
  356.  
  357. $limit = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop', 'limit');
  358. if (!empty($limit)) {
  359. $atts['post_ids'] = $limit;
  360. }
  361. $offset = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop', 'offset');
  362. if (!empty($offset)) {
  363. $atts['post_ids'] = $offset;
  364. }
  365. $post_ids = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop', 'post_ids');
  366. if (!empty($post_ids)) {
  367. $atts['post_ids'] = $post_ids;
  368. }
  369.  
  370. $atts = array(
  371. 'limit' => $limit,
  372. 'offset' => $offset,
  373. 'post_ids' => $post_ids
  374. );
  375.  
  376. $loop_data = $tdb_state_single_page->loop->__invoke($atts);
  377.  
  378. }
  379.  
  380. // don't apply on page-pagebuilder-latest.php
  381. if ( 'page-pagebuilder-latest.php' !== $_wp_page_template && $tdb_is_loop_2_pagination === 'numbered' ) {
  382.  
  383. $atts = array();
  384.  
  385. $limit = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop_2', 'limit');
  386. if (!empty($limit)) {
  387. $atts['post_ids'] = $limit;
  388. }
  389. $offset = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop_2', 'offset');
  390. if (!empty($offset)) {
  391. $atts['post_ids'] = $offset;
  392. }
  393. $post_ids = tdb_util::get_shortcode_att($page_template->post->post_content, 'tdb_loop_2', 'post_ids');
  394. if (!empty($post_ids)) {
  395. $atts['post_ids'] = $post_ids;
  396. }
  397.  
  398. $atts = array(
  399. 'limit' => $limit,
  400. 'offset' => $offset,
  401. 'post_ids' => $post_ids
  402. );
  403.  
  404. $loop_data = $tdb_state_single_page->loop->__invoke($atts);
  405.  
  406. }
  407.  
  408. if (!empty($loop_data)) {
  409.  
  410. $max_page = (int)$loop_data ["loop_pagination"]['max_page'];
  411.  
  412. $td_page = get_query_var('page') ? get_query_var('page') : 1; //rewrite the global var
  413. $td_paged = get_query_var('paged') ? get_query_var('paged') : 1; //rewrite the global var
  414.  
  415. $td_page = intval($td_page);
  416. $td_paged = intval($td_paged);
  417.  
  418. //paged works on single pages, page - works on homepage
  419. if ($td_paged > $td_page) {
  420. $paged = $td_paged;
  421. } else {
  422. $paged = $td_page;
  423. }
  424.  
  425. // Remove the wp action links
  426. remove_action('wp_head', 'rel_canonical');
  427. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
  428.  
  429. if (class_exists('WPSEO_Frontend')) {
  430. // Remove the canonical action of the Yoast SEO plugin
  431. add_filter( 'wpseo_canonical', '__return_false' );
  432. }
  433.  
  434. $td_current_page = '<link rel="canonical" href="' . get_pagenum_link($paged) . '"/>';
  435. $td_prev_page = '<link rel="prev" href="' . get_pagenum_link($paged - 1) . '"/>';
  436. $td_next_page = '<link rel="next" href="' . get_pagenum_link($paged + 1) . '"/>';
  437.  
  438. if ( td_util::is_amp() ){
  439. $td_current_page = str_replace( '?amp', "", $td_current_page);
  440. $td_prev_page = str_replace( '?amp', "", $td_prev_page);
  441. $td_next_page = str_replace( '?amp', "", $td_next_page);
  442. }
  443.  
  444. echo $td_current_page;
  445.  
  446. if ($paged > 1) {
  447. echo $td_prev_page;
  448. }
  449. if ($paged < $max_page) {
  450. echo $td_next_page;
  451. }
  452.  
  453. }
  454.  
  455. }
  456. }
  457. }, 1);
  458.  
  459.  
  460. add_action( 'pre_get_posts', 'tdb_modify_main_query_for_wp_templates_page' );
  461. function tdb_modify_main_query_for_wp_templates_page( $query ) {
  462.  
  463. // checking for main query ONLY ON frontend - Does not run on ajax or TDC iFrame!!!
  464. if( ( !is_admin() && $query->is_main_query() && !tdc_state::is_live_editor_ajax() && !tdc_state::is_live_editor_iframe()) ) {
  465.  
  466. $template_id = '';
  467.  
  468. $lang = '';
  469. if ( class_exists( 'SitePress', false ) ) {
  470. global $sitepress;
  471. $sitepress_settings = $sitepress->get_settings();
  472. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  473. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  474. if (1 === $translation_mode) {
  475. $lang = $sitepress->get_current_language();
  476. }
  477. }
  478. }
  479.  
  480. if ( is_tax() && ! td_util::is_mobile_theme() ) {
  481.  
  482. $option_id = 'tdb_category_template' . $lang;
  483. $td_cpt_tax = td_util::get_option('td_cpt_tax');
  484.  
  485. $queried_object = get_queried_object();
  486.  
  487. if ( $queried_object instanceof WP_Term ) {
  488.  
  489. if (!empty($td_cpt_tax[$queried_object->taxonomy])) {
  490. $tdb_tax_template = $td_cpt_tax[$queried_object->taxonomy][$option_id];
  491. if ( td_global::is_tdb_template( $tdb_tax_template, true ) ) {
  492. $template_id = td_global::tdb_get_template_id( $tdb_tax_template );
  493. }
  494. }
  495. }
  496.  
  497. } else if ( is_category() && ! td_util::is_mobile_theme() ) {
  498.  
  499. $current_category_obj = '';
  500. $current_category_id = '';
  501.  
  502. if ( isset( $query->query['cat'] ) ) {
  503. $current_category_obj = get_category( $query->query['cat'] );
  504. } elseif( isset( $query->query_vars['category_name'] ) ) {
  505. $current_category_obj = get_category_by_slug( $query->query_vars['category_name'] );
  506. }
  507.  
  508. if ( !empty( $current_category_obj ) ) {
  509. $current_category_id = $current_category_obj->cat_ID;
  510. }
  511.  
  512. $option_id = 'tdb_category_template';
  513. if (class_exists('SitePress', false )) {
  514. global $sitepress;
  515. $sitepress_settings = $sitepress->get_settings();
  516. $translation_mode = (int) $sitepress_settings['custom_posts_sync_option'][ 'tdb_templates'];
  517. if ( 1 === $translation_mode ) {
  518. $option_id .= $sitepress->get_current_language();
  519. }
  520. }
  521.  
  522. // read the individual cat template
  523. $tdb_individual_category_template = td_util::get_category_option( $current_category_id, $option_id );
  524.  
  525.  
  526. // read the global template
  527. $tdb_category_template = td_options::get( $option_id );
  528.  
  529. // if ( !empty( $tdb_individual_category_template ) && td_global::is_tdb_template( $tdb_individual_category_template, true ) ) {
  530. // $template_id = td_global::tdb_get_template_id( $tdb_individual_category_template );
  531. // } else {
  532. // if ( td_global::is_tdb_template( $tdb_category_template, true ) ) {
  533. // $template_id = td_global::tdb_get_template_id( $tdb_category_template );
  534. // }
  535. // }
  536.  
  537. if ( empty($tdb_individual_category_template)) {
  538. //get global option if individual is not tdb
  539. if ( td_global::is_tdb_template( $tdb_category_template, true )) {
  540. $template_id = td_global::tdb_get_template_id( $tdb_category_template );
  541. }
  542. } else if ( td_global::is_tdb_template( $tdb_individual_category_template, true ) ) {
  543. $template_id = td_global::tdb_get_template_id( $tdb_individual_category_template );
  544. }
  545.  
  546.  
  547. } elseif ( is_author() && ! td_util::is_mobile_theme() ) {
  548.  
  549. $option_id = 'tdb_author_templates';
  550. if ( class_exists( 'SitePress', false ) ) {
  551. global $sitepress;
  552. $sitepress_settings = $sitepress->get_settings();
  553. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  554. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  555. if (1 === $translation_mode) {
  556. $option_id .= $sitepress->get_current_language();
  557. }
  558. }
  559. }
  560.  
  561. // user templates
  562. $tdb_author_templates = td_util::get_option( $option_id );
  563. // author id
  564. $author_id = '';
  565. if (!empty($query->query_vars['author'])) {
  566. $author_id = $query->query_vars['author'];
  567. } else if(!empty($query->query_vars['author_name'])) {
  568. $user = get_user_by('slug', $query->query_vars['author_name']);
  569. if( $user ){
  570. $author_id = $user->ID;
  571. }
  572. }
  573.  
  574. if ( !empty( $tdb_author_templates[$author_id] ) && td_global::is_tdb_template( $tdb_author_templates[$author_id], true ) ) {
  575. // individual author template
  576. $template_id = td_global::tdb_get_template_id( $tdb_author_templates[$author_id] );
  577. } else {
  578. $option_id = 'tdb_author_template';
  579. if ( class_exists( 'SitePress', false ) ) {
  580. global $sitepress;
  581. $sitepress_settings = $sitepress->get_settings();
  582. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  583. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  584. if (1 === $translation_mode) {
  585. $option_id .= $sitepress->get_current_language();
  586. }
  587. }
  588. }
  589. // default template
  590. $tdb_author_template = td_options::get( $option_id );
  591. if ( td_global::is_tdb_template( $tdb_author_template, true ) ) {
  592. $template_id = td_global::tdb_get_template_id( $tdb_author_template );
  593. }
  594. }
  595.  
  596. } elseif ( is_search() && ! td_util::is_mobile_theme() ) {
  597.  
  598. $option_id = 'tdb_search_template';
  599. if ( class_exists( 'SitePress', false ) ) {
  600. global $sitepress;
  601. $sitepress_settings = $sitepress->get_settings();
  602. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  603. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  604. if (1 === $translation_mode) {
  605. $option_id .= $sitepress->get_current_language();
  606. }
  607. }
  608. }
  609.  
  610. // read the template
  611. $tdb_search_template = td_options::get( $option_id );
  612. if ( td_global::is_tdb_template( $tdb_search_template, true ) ) {
  613. $template_id = td_global::tdb_get_template_id( $tdb_search_template );
  614. }
  615.  
  616. } elseif ( is_date() && ! td_util::is_mobile_theme() ) {
  617.  
  618. $option_id = 'tdb_date_template';
  619. if ( class_exists( 'SitePress', false ) ) {
  620. global $sitepress;
  621. $sitepress_settings = $sitepress->get_settings();
  622. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  623. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  624. if (1 === $translation_mode) {
  625. $option_id .= $sitepress->get_current_language();
  626. }
  627. }
  628. }
  629.  
  630. // read the template
  631. $tdb_date_template = td_options::get( $option_id );
  632. if ( td_global::is_tdb_template( $tdb_date_template, true ) ) {
  633. $template_id = td_global::tdb_get_template_id( $tdb_date_template );
  634. }
  635.  
  636. } elseif ( is_tag() && ! td_util::is_mobile_theme() ) {
  637.  
  638. $template_found = false;
  639.  
  640. $option_id = 'tdb_tag_templates';
  641. if ( class_exists( 'SitePress', false ) ) {
  642. global $sitepress;
  643. $sitepress_settings = $sitepress->get_settings();
  644. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  645. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  646. if (1 === $translation_mode) {
  647. $option_id .= $sitepress->get_current_language();
  648. }
  649. }
  650. }
  651.  
  652. $tdb_tag_templates = td_options::get( $option_id );
  653. if ( is_array($tdb_tag_templates)) {
  654. $queried_object = get_queried_object();
  655. foreach ( $tdb_tag_templates as $tdb_tag_template_id => $tags ) {
  656.  
  657. if ( false !== strpos( $tags, $queried_object->slug) && td_global::is_tdb_template( $tdb_tag_template_id, true )) {
  658. $template_found = true;
  659. $template_id = td_global::tdb_get_template_id( $tdb_tag_template_id );
  660. }
  661. }
  662. }
  663.  
  664. if ( ! $template_found ) {
  665.  
  666. $option_id = 'tdb_tag_template';
  667. if ( class_exists( 'SitePress', false ) ) {
  668. global $sitepress;
  669. $sitepress_settings = $sitepress->get_settings();
  670. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  671. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  672. if (1 === $translation_mode) {
  673. $option_id .= $sitepress->get_current_language();
  674. }
  675. }
  676. }
  677.  
  678. // read the default tag template
  679. $tdb_tag_template = td_options::get( $option_id );
  680.  
  681. if ( td_global::is_tdb_template( $tdb_tag_template, true ) ) {
  682. $template_id = td_global::tdb_get_template_id( $tdb_tag_template );
  683. }
  684. }
  685.  
  686. } elseif ( is_page() && ! td_util::is_mobile_theme() ) {
  687.  
  688. $page_id = isset($query->queried_object) ? $query->queried_object->ID : $query->query_vars['page_id'];
  689.  
  690. // load the page template
  691. $page_template = new WP_Query( array(
  692. 'p' => $page_id,
  693. 'post_type' => 'page',
  694. )
  695. );
  696.  
  697. if ( !empty( $page_template ) && $page_template->have_posts() ) {
  698. $offset = tdb_util::get_shortcode_att( $page_template->post->post_content, 'tdb_loop','offset' );
  699. tdb_state_template::set_template_loop_offset( (int)$offset );
  700. }
  701. }
  702.  
  703. if ( !empty( $template_id ) ) {
  704.  
  705. if ( class_exists('Mobile_Detect')) {
  706. $mobile_detect = new Mobile_Detect();
  707. if ( $mobile_detect->isMobile() ) {
  708. $tdc_mobile_template_id = get_post_meta( $template_id, 'tdc_mobile_template_id', true );
  709. if ( ! empty( $tdc_mobile_template_id ) && 'publish' === get_post_status( $tdc_mobile_template_id ) ) {
  710. $template_id = $tdc_mobile_template_id;
  711. }
  712. }
  713. }
  714.  
  715. // load the tdb template
  716. $wp_query_template = new WP_Query( array(
  717. 'p' => $template_id,
  718. 'post_type' => 'tdb_templates',
  719. )
  720. );
  721. }
  722.  
  723. // if we have a template
  724. if ( !empty( $wp_query_template ) && $wp_query_template->have_posts() ) {
  725.  
  726. /**
  727. * set the tdb_template_overwrite filter
  728. * this runs in the theme and is used by plugins to tell the theme not to do the default modifications for the main query on category pages
  729. * @see td_modify_main_query_for_category_page in ..\theme\includes\wp_booster\td_wp_booster_functions.php
  730. */
  731. add_filter( 'tdb_category_template_query_overwrite', function() {
  732. return true;
  733. });
  734.  
  735. // set the template query
  736. tdb_state_template::set_wp_query( $wp_query_template );
  737.  
  738. $limit = tdb_util::get_shortcode_att( $wp_query_template->post->post_content, 'tdb_loop','limit' );
  739.  
  740. if ( empty($limit) )
  741. $limit = get_option('posts_per_page');
  742.  
  743. $offset = tdb_util::get_shortcode_att( $wp_query_template->post->post_content, 'tdb_loop','offset' );
  744. tdb_state_template::set_template_loop_offset( intval($offset) );
  745.  
  746. // detect and handle pagination
  747. if ( $query->is_paged ) {
  748.  
  749. // if we have an offset, manually determine page query offset (offset + current page (minus one) x posts per page)
  750. if ( !empty($offset) || $offset == '0')
  751. $offset = (int)$offset + ( ($query->query_vars['paged']-1) * (int)$limit );
  752. }
  753.  
  754. // exclude certain posts or pages from your posts loop
  755. $post_ids = tdb_util::get_shortcode_att( $wp_query_template->post->post_content, 'tdb_loop','post_ids' );
  756.  
  757. if ( !empty($post_ids) ) {
  758.  
  759. // split posts ids string
  760. $post_ids_array = explode (',', $post_ids);
  761. $posts_not_in = array();
  762. $posts_in = array();
  763.  
  764. // split ids
  765. foreach ($post_ids_array as $post_id) {
  766. $post_id = trim($post_id);
  767.  
  768. // check if the ID is actually a number
  769. if (is_numeric($post_id)) {
  770. if (intval($post_id) < 0) {
  771. $posts_not_in[] = str_replace('-', '', $post_id);
  772. } else {
  773. $posts_in[] = $post_id;
  774. }
  775. }
  776. }
  777.  
  778. // don't pass an empty post__in because it will return has_posts()
  779. if (!empty($posts_in)) {
  780. $query->set( 'post__in', $posts_in );
  781. $query->set( 'orderby', 'post__in' );
  782. }
  783.  
  784. // set the posts to exclude if any
  785. if (!empty($posts_not_in)) {
  786. $query->set( 'post__not_in', $posts_not_in );
  787. }
  788. }
  789.  
  790. // set post type
  791. $installed_post_types = tdb_util::get_shortcode_att($wp_query_template->post->post_content, 'tdb_loop', 'installed_post_types');
  792.  
  793. if (!empty($installed_post_types)) {
  794. $array_selected_post_types = array();
  795. $expl_installed_post_types = explode(',', $installed_post_types);
  796.  
  797. foreach ($expl_installed_post_types as $val_this_post_type) {
  798. if (trim($val_this_post_type) != '') {
  799. $array_selected_post_types[] = trim($val_this_post_type);
  800. }
  801. }
  802.  
  803. $query->set( 'post_type', $array_selected_post_types );
  804. }
  805.  
  806. $sort = tdb_util::get_shortcode_att( $wp_query_template->post->post_content, 'tdb_loop','sort' );
  807. switch ($sort) {
  808. case 'oldest_posts':
  809. $query->set( 'order', 'ASC' );
  810. break;
  811.  
  812. case 'modified_date':
  813. $query->set( 'orderby', 'post_modified' );
  814. break;
  815.  
  816. case 'alphabetical_order':
  817. $query->set( 'orderby', 'title' );
  818. $query->set( 'order', 'ASC' );
  819. break;
  820.  
  821. case 'popular':
  822. $query->set( 'meta_key', td_page_views::$post_view_counter_key );
  823. $query->set( 'orderby', 'meta_value_num' );
  824. $query->set( 'order', 'DESC' );
  825. break;
  826.  
  827. case 'popular7':
  828. $query->set( 'meta_query', array(
  829. 'relation' => 'AND',
  830. array(
  831. 'key' => td_page_views::$post_view_counter_7_day_total,
  832. 'type' => 'numeric'
  833. ),
  834. array(
  835. 'key' => td_page_views::$post_view_counter_7_day_last_date,
  836. 'value' => (date('U') - 604800), // current date minus 7 days
  837. 'type' => 'numeric',
  838. 'compare' => '>'
  839. )
  840. ) );
  841. $query->set( 'orderby', td_page_views::$post_view_counter_7_day_total );
  842. $query->set( 'order', 'DESC' );
  843. break;
  844.  
  845. case 'review_high':
  846. $query->set( 'meta_key', 'td_review_key' );
  847. $query->set( 'orderby', 'meta_value_num' );
  848. $query->set( 'order', 'DESC' );
  849. break;
  850.  
  851. case 'comment_count':
  852. $query->set( 'orderby', 'comment_count' );
  853. $query->set( 'order', 'DESC' );
  854. break;
  855. }
  856.  
  857. // locked content
  858. $locked_only = tdb_util::get_shortcode_att( $wp_query_template->post->post_content, 'tdb_loop','locked_only' );
  859.  
  860. // set the query limit/offset/sort
  861. // except feeds
  862. if( empty( $query->is_feed ) ) {
  863. $query->set('main_query_offset', true);
  864. $query->set('offset', $offset);
  865. $query->set('posts_per_page', $limit);
  866.  
  867. if ( defined('TD_SUBSCRIPTION') && !empty( $locked_only ) ) {
  868. $query->set( 'meta_key', 'tds_lock_content' );
  869. }
  870. }
  871.  
  872. }
  873. }
  874. }
  875.  
  876. add_filter( 'found_posts', 'tdb_on_found_posts_adjust_offset_pagination', 10, 2 );
  877. function tdb_on_found_posts_adjust_offset_pagination( $found_posts, $query ) {
  878.  
  879. if( $query->get('main_query_offset') ) {
  880. $offset = tdb_state_template::get_template_loop_offset();
  881. return $found_posts - $offset;
  882. }
  883.  
  884. return $found_posts;
  885. }
  886.  
  887. // fix dsq 'comments_template' action hook that does not return the path to the theme template file(original template passed as parameter) ...
  888. //... and breaks cloud library's tdb_single_comments shortcode functionality
  889. if ( class_exists( 'Disqus_Conditional_Load' ) ) {
  890. //add_filter( 'dsq_can_load', '__return_false' );
  891. // remove the dsq 'comments_template' > remove_disqus_template hook to prevent breaking cloud library's tdb_single_comments shortcode functionality
  892. remove_action( 'comments_template', array( dcl_instance()->public, 'remove_disqus_template' ), 15 );
  893. }
  894.  
  895. });
  896.  
  897.  
  898. /**
  899. * resources loaded just on ajax autoloaded(infinite) posts
  900. */
  901. if ( tdb_state::is_ajax() ) {
  902.  
  903. // do not show the admin bar on autoload posts ajax calls
  904. add_filter('show_admin_bar', '__return_false');
  905.  
  906. // enqueue for posts autoload(infinite) ajax loaded posts
  907. add_filter('wp_enqueue_scripts', function(){
  908.  
  909. // load the js
  910. if ( TDB_DEPLOY_MODE == 'dev' ) {
  911. wp_enqueue_script( 'tdbAutoloadAjax', TDB_URL . '/assets/js/tdbAutoloadAjax.js', array( 'jquery' ), TD_CLOUD_LIBRARY, true);
  912. } else {
  913. wp_enqueue_script( 'tdb_js_posts_autoload_ajax', TDB_URL . '/assets/js/js_posts_autoload_ajax.min.js', array( 'jquery', 'underscore' ), TD_CLOUD_LIBRARY, true );
  914. }
  915.  
  916. // load the css
  917. if ( TDB_DEPLOY_MODE == 'dev' ) {
  918. wp_enqueue_style( 'tdb_autoload_ajax', TDB_URL . '/td_less_style.css.php?part=less_ajax', false, TD_CLOUD_LIBRARY );
  919. } else {
  920. wp_enqueue_style( 'tdb_posts_autoload_ajax_style', TDB_URL . '/assets/css/tdb_less_ajax.css', false, TD_CLOUD_LIBRARY );
  921. }
  922. });
  923.  
  924. // do not index '/?tdb_action=tdb_ajax' url - autoload article
  925. add_filter('wp_head', function(){
  926. echo '<meta name="robots" content="noindex, nofollow" />';
  927. },1);
  928. }
  929.  
  930.  
  931. /**
  932. * when posting a comment on a ajax autoloaded posts make sure the redirect sets the ajax state
  933. */
  934. add_filter('comment_post_redirect', function( $url ){
  935.  
  936. if ( strpos( $_SERVER["HTTP_REFERER"], '?tdb_action=tdb_ajax' ) !== false ) {
  937. $url = add_query_arg( 'tdb_action', 'tdb_ajax', $url );
  938. }
  939.  
  940. return $url;
  941. });
  942.  
  943.  
  944. /**
  945. * redirect the view template
  946. * - template_include runs after template_redirect
  947. * - RUNS BEFORE the one that we have in the theme @see on_td_wp_booster_functions.php
  948. * - The theme does nothing on single pages when it detects a template builder template so we have to do all the work here
  949. */
  950. add_filter( 'template_include', 'tdb_on_template_include' );
  951. function tdb_on_template_include( $original_template ) {
  952.  
  953. $lang = '';
  954. if ( class_exists( 'SitePress', false ) ) {
  955. global $sitepress;
  956. $sitepress_settings = $sitepress->get_settings();
  957. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  958. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  959. if (1 === $translation_mode) {
  960. $lang = $sitepress->get_current_language();
  961. }
  962. }
  963. }
  964.  
  965. // we are viewing a single post template
  966. if ( is_singular( array( 'tdb_templates' ) ) && ! td_util::is_mobile_theme() ) {
  967.  
  968. add_filter( 'the_content', function( $content ) {
  969. if ( td_util::is_template_header() ) {
  970.  
  971. global $post;
  972. $tdb_template_type = get_post_meta( $post->ID, 'tdb_template_type', true );
  973.  
  974. if ( 'header' === $tdb_template_type ) {
  975. return '';
  976. }
  977. return $content;
  978. }
  979. return $content;
  980. });
  981.  
  982. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_template.php';
  983. }
  984.  
  985. // we are viewing a category template
  986. if ( is_tax() && ! td_util::is_mobile_theme() ) {
  987.  
  988. $current_term_id = get_queried_object()->term_id;
  989. $tax_query_var = get_query_var('taxonomy');
  990. $option_id = 'tdb_category_template' . $lang;
  991. $td_cpt_tax = td_util::get_option('td_cpt_tax');
  992.  
  993. if (!empty($tax_query_var) && !empty($td_cpt_tax[$tax_query_var])) {
  994. $tdb_tax_template = $td_cpt_tax[$tax_query_var][$option_id];
  995. if ( td_global::is_tdb_template( $tdb_tax_template, true ) ) {
  996. $template_id = td_global::tdb_get_template_id( $tdb_tax_template );
  997. }
  998. }
  999.  
  1000. // if we don't have a template return the original temp
  1001. if ( !empty( $template_id ) ) {
  1002.  
  1003. // load the tdb template
  1004. $wp_query_template = new WP_Query( array(
  1005. 'p' => $template_id,
  1006. 'post_type' => 'tdb_templates',
  1007. )
  1008. );
  1009. }
  1010.  
  1011. // do not redirect the theme template if we don't find the template
  1012. // the template was probably deleted or something
  1013. if ( empty( $wp_query_template ) || !$wp_query_template->have_posts() ) {
  1014. return $original_template; // do nothing if the template is not found!
  1015. }
  1016.  
  1017. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_category.php';
  1018. }
  1019.  
  1020. // we are viewing a single page template
  1021. if ( is_singular( array( 'attachment' ) ) && ! td_util::is_mobile_theme() ) {
  1022.  
  1023. $template_id = '';
  1024.  
  1025. $option_id = 'tdb_attachment_template';
  1026. if ( class_exists( 'SitePress', false ) ) {
  1027. global $sitepress;
  1028. $sitepress_settings = $sitepress->get_settings();
  1029. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1030. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1031. if (1 === $translation_mode) {
  1032. $option_id .= $sitepress->get_current_language();
  1033. }
  1034. }
  1035. }
  1036.  
  1037. // read template
  1038. $tdb_attachment_template = td_options::get( $option_id );
  1039. if ( td_global::is_tdb_template( $tdb_attachment_template, true ) ) {
  1040. $template_id = td_global::tdb_get_template_id( $tdb_attachment_template );
  1041. }
  1042.  
  1043. if ( !empty( $template_id ) ) {
  1044.  
  1045. if (class_exists('Mobile_Detect')) {
  1046. $mobile_detect = new Mobile_Detect();
  1047. if ( $mobile_detect->isMobile() ) {
  1048. $tdc_mobile_template_id = get_post_meta( $template_id, 'tdc_mobile_template_id', true );
  1049. if ( ! empty( $tdc_mobile_template_id ) ) {
  1050. $template_id = $tdc_mobile_template_id;
  1051. }
  1052. }
  1053. }
  1054.  
  1055. // load the tdb template
  1056. $wp_query_template = new WP_Query( array(
  1057. 'p' => $template_id,
  1058. 'post_type' => 'tdb_templates',
  1059. )
  1060. );
  1061. }
  1062.  
  1063. // do not redirect the theme template if we don't find the template
  1064. // the template was probably deleted or something
  1065. if ( empty( $wp_query_template ) || !$wp_query_template->have_posts() ) {
  1066. return $original_template; // do nothing if the template is not found!
  1067. }
  1068.  
  1069. // save our template wp_query & load
  1070. tdb_state_template::set_wp_query( $wp_query_template );
  1071.  
  1072. // do the redirect
  1073. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_attachment.php';
  1074. }
  1075.  
  1076. // we are viewing a category template
  1077. if ( is_category() && ! td_util::is_mobile_theme() ) {
  1078.  
  1079. $template_id = '';
  1080. $current_category_obj = '';
  1081.  
  1082. $cat_query_var = get_query_var('cat');
  1083. $category_name_query_var = get_query_var('category_name');
  1084. $current_category_id = '';
  1085.  
  1086. if ( !empty( $cat_query_var ) ) {
  1087. $current_category_obj = get_category( $cat_query_var );
  1088. } elseif( !empty( $category_name_query_var ) ) {
  1089. $current_category_obj = get_category_by_slug( $category_name_query_var );
  1090. }
  1091.  
  1092. if ( !empty( $current_category_obj ) ) {
  1093. $current_category_id = $current_category_obj->cat_ID;
  1094. }
  1095.  
  1096. $option_id = 'tdb_category_template' . $lang;
  1097.  
  1098. // read the individual cat template
  1099. $tdb_individual_category_template = td_util::get_category_option( $current_category_id, $option_id );
  1100.  
  1101. // read the global template
  1102. $tdb_category_template = td_options::get( $option_id );
  1103.  
  1104. if ( empty( $tdb_individual_category_template ) ) {
  1105.  
  1106. if ( td_global::is_tdb_template( $tdb_category_template, true ) ) {
  1107. $template_id = td_global::tdb_get_template_id( $tdb_category_template );
  1108. }
  1109.  
  1110. } else {
  1111.  
  1112. if ( td_global::is_tdb_template( $tdb_individual_category_template, true ) ) {
  1113. $template_id = td_global::tdb_get_template_id( $tdb_individual_category_template );
  1114.  
  1115. } else if ( 'theme_templates' === $tdb_individual_category_template ) {
  1116.  
  1117. // do nothing if the template is not found!
  1118. return $original_template;
  1119.  
  1120. } else if ( td_global::is_tdb_template( $tdb_category_template, true ) ) {
  1121.  
  1122. // look and set the global cat cloud template if we have an individual category but it's not found
  1123. $template_id = td_global::tdb_get_template_id( $tdb_category_template );
  1124.  
  1125. }
  1126. }
  1127.  
  1128. // if we don't have a template return the original temp
  1129. if ( !empty( $template_id ) ) {
  1130.  
  1131. if (class_exists('Mobile_Detect')) {
  1132. $mobile_detect = new Mobile_Detect();
  1133. if ( $mobile_detect->isMobile() ) {
  1134. $tdc_mobile_template_id = get_post_meta( $template_id, 'tdc_mobile_template_id', true );
  1135. if ( ! empty( $tdc_mobile_template_id ) ) {
  1136. $template_id = $tdc_mobile_template_id;
  1137. }
  1138. }
  1139. }
  1140.  
  1141. // load the tdb template
  1142. $wp_query_template = new WP_Query( array(
  1143. 'p' => $template_id,
  1144. 'post_type' => 'tdb_templates',
  1145. )
  1146. );
  1147. }
  1148.  
  1149. // do not redirect the theme template if we don't find the template
  1150. // it was probably deleted or something
  1151. if ( empty( $wp_query_template ) || !$wp_query_template->have_posts() ) {
  1152. return $original_template; // do nothing if the template is not found!
  1153. }
  1154.  
  1155. // save our template wp_query & load
  1156. tdb_state_template::set_wp_query( $wp_query_template );
  1157.  
  1158. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_category.php';
  1159. }
  1160.  
  1161. // we are viewing a author template
  1162. if ( is_author() && ! td_util::is_mobile_theme() ) {
  1163.  
  1164. $option_id = 'tdb_author_templates';
  1165. if ( class_exists( 'SitePress', false ) ) {
  1166. global $sitepress;
  1167. $sitepress_settings = $sitepress->get_settings();
  1168. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1169. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1170. if (1 === $translation_mode) {
  1171. $option_id .= $sitepress->get_current_language();
  1172. }
  1173. }
  1174. }
  1175.  
  1176. // user templates
  1177. $tdb_author_templates = td_util::get_option( $option_id );
  1178. // author id
  1179. $author_query_var = get_query_var('author');
  1180. $author_name_query_var = get_query_var('author_name');
  1181. $author_id = '';
  1182. if (!empty($author_query_var)) {
  1183. $author_id = $author_query_var;
  1184. } else if(!empty($author_name_query_var)) {
  1185. $user = get_user_by('login', $author_name_query_var);
  1186. if( $user ){
  1187. $author_id = $user->ID;
  1188. }
  1189. }
  1190.  
  1191. if ( !empty( $tdb_author_templates[$author_id] ) && td_global::is_tdb_template( $tdb_author_templates[$author_id], true ) ) {
  1192. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_author.php';
  1193. }
  1194.  
  1195. $option_id = 'tdb_author_template';
  1196. if ( class_exists( 'SitePress', false ) ) {
  1197. global $sitepress;
  1198. $sitepress_settings = $sitepress->get_settings();
  1199. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1200. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1201. if (1 === $translation_mode) {
  1202. $option_id .= $sitepress->get_current_language();
  1203. }
  1204. }
  1205. }
  1206.  
  1207. // read template
  1208. $template_id = td_options::get( $option_id );
  1209. if (td_global::is_tdb_template($template_id, true)) {
  1210. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_author.php';
  1211. }
  1212. }
  1213.  
  1214. // we are viewing a search template
  1215. if ( is_search() && ! td_util::is_mobile_theme() ) {
  1216.  
  1217. $option_id = 'tdb_search_template';
  1218. if ( class_exists( 'SitePress', false ) ) {
  1219. global $sitepress;
  1220. $sitepress_settings = $sitepress->get_settings();
  1221. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1222. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1223. if (1 === $translation_mode) {
  1224. $option_id .= $sitepress->get_current_language();
  1225. }
  1226. }
  1227. }
  1228.  
  1229.  
  1230. // read template
  1231. $tdb_search_template = td_options::get( $option_id );
  1232. if ( td_global::is_tdb_template( $tdb_search_template, true ) ) {
  1233. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_search.php';
  1234. }
  1235.  
  1236. }
  1237.  
  1238. // we are viewing a date template
  1239. if ( is_date() && ! td_util::is_mobile_theme() ) {
  1240.  
  1241. $option_id = 'tdb_date_template';
  1242. if ( class_exists( 'SitePress', false ) ) {
  1243. global $sitepress;
  1244. $sitepress_settings = $sitepress->get_settings();
  1245. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1246. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1247. if (1 === $translation_mode) {
  1248. $option_id .= $sitepress->get_current_language();
  1249. }
  1250. }
  1251. }
  1252.  
  1253. // read template
  1254. $tdb_date_template = td_options::get( $option_id );
  1255. if ( td_global::is_tdb_template( $tdb_date_template, true ) ) {
  1256. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_date.php';
  1257. }
  1258.  
  1259. }
  1260.  
  1261. // we are viewing a tag template
  1262. if ( is_tag() && ! td_util::is_mobile_theme() ) {
  1263.  
  1264. $option_id = 'tdb_tag_templates';
  1265. if ( class_exists( 'SitePress', false ) ) {
  1266. global $sitepress;
  1267. $sitepress_settings = $sitepress->get_settings();
  1268. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1269. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1270. if (1 === $translation_mode) {
  1271. $option_id .= $sitepress->get_current_language();
  1272. }
  1273. }
  1274. }
  1275.  
  1276. $tdb_tag_templates = td_options::get( $option_id );
  1277. if ( is_array( $tdb_tag_templates ) ) {
  1278. $queried_object = get_queried_object();
  1279. foreach ( $tdb_tag_templates as $tdb_tag_template_id => $tags ) {
  1280.  
  1281. if ( false !== strpos( $tags, $queried_object->slug ) && td_global::is_tdb_template( $tdb_tag_template_id, true ) ) {
  1282. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_tag.php';
  1283. }
  1284. }
  1285. }
  1286.  
  1287. $option_id = 'tdb_tag_template';
  1288. if ( class_exists( 'SitePress', false ) ) {
  1289. global $sitepress;
  1290. $sitepress_settings = $sitepress->get_settings();
  1291. if ( isset( $sitepress_settings['custom_posts_sync_option'][ 'tdb_templates'] ) ) {
  1292. $translation_mode = (int) $sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1293. if ( 1 === $translation_mode ) {
  1294. $option_id .= $sitepress->get_current_language();
  1295. }
  1296. }
  1297. }
  1298.  
  1299. // read template
  1300. $tdb_tag_template = td_options::get( $option_id );
  1301. if ( td_global::is_tdb_template( $tdb_tag_template, true ) ) {
  1302. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_tag.php';
  1303. }
  1304.  
  1305. }
  1306.  
  1307. // we are viewing a 404 template
  1308. if ( is_404() && ! td_util::is_mobile_theme() ) {
  1309.  
  1310. $template_id = '';
  1311.  
  1312. $option_id = 'tdb_404_template';
  1313. if ( class_exists( 'SitePress', false ) ) {
  1314. global $sitepress;
  1315. $sitepress_settings = $sitepress->get_settings();
  1316. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  1317. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  1318. if (1 === $translation_mode) {
  1319. $option_id .= $sitepress->get_current_language();
  1320. }
  1321. }
  1322. }
  1323.  
  1324. // read template
  1325. $tdb_404_template = td_options::get( $option_id );
  1326. if ( td_global::is_tdb_template( $tdb_404_template, true ) ) {
  1327. $template_id = td_global::tdb_get_template_id( $tdb_404_template );
  1328. }
  1329.  
  1330. if ( !empty( $template_id ) ) {
  1331.  
  1332. if (class_exists('Mobile_Detect')) {
  1333. $mobile_detect = new Mobile_Detect();
  1334. if ( $mobile_detect->isMobile() ) {
  1335. $tdc_mobile_template_id = get_post_meta( $template_id, 'tdc_mobile_template_id', true );
  1336. if ( ! empty( $tdc_mobile_template_id ) ) {
  1337. $template_id = $tdc_mobile_template_id;
  1338. }
  1339. }
  1340. }
  1341.  
  1342. // load the tdb template
  1343. $wp_query_template = new WP_Query( array(
  1344. 'p' => $template_id,
  1345. 'post_type' => 'tdb_templates',
  1346. )
  1347. );
  1348. }
  1349.  
  1350. // do not redirect the theme template if we don't find the template
  1351. // the template was probably deleted or something
  1352. if ( empty( $wp_query_template ) || !$wp_query_template->have_posts() ) {
  1353. return $original_template; // do nothing if the template is not found!
  1354. }
  1355.  
  1356. // save our template wp_query & load
  1357. tdb_state_template::set_wp_query( $wp_query_template );
  1358.  
  1359. // do the redirect
  1360. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_404.php';
  1361. }
  1362.  
  1363. // we are viewing a 404 template
  1364. if ( is_page() && ! td_util::is_mobile_theme() ) {
  1365.  
  1366. if (class_exists('Mobile_Detect')) {
  1367. $mobile_detect = new Mobile_Detect();
  1368. if ( $mobile_detect->isMobile() ) {
  1369. $tdc_mobile_template_id = get_post_meta( get_the_ID(), 'tdc_mobile_template_id', true );
  1370.  
  1371. if ( ! empty( $tdc_mobile_template_id ) ) {
  1372.  
  1373. if ( get_query_var( 'paged' ) ) {
  1374. $paged = get_query_var( 'paged' );
  1375. }
  1376. elseif ( get_query_var( 'page' ) ) {
  1377. $paged = get_query_var( 'page' );
  1378. }
  1379. else {
  1380. $paged = 1;
  1381. }
  1382.  
  1383. // load the tdb template
  1384. $wp_query_template = new WP_Query( array(
  1385. 'p' => $tdc_mobile_template_id,
  1386. 'post_type' => 'page',
  1387. 'paged' => $paged
  1388. )
  1389. );
  1390. }
  1391. }
  1392. }
  1393.  
  1394. // do not redirect the theme template if we don't find the template
  1395. // the template was probably deleted or something
  1396. if ( empty( $wp_query_template ) || !$wp_query_template->have_posts() ) {
  1397. return $original_template; // do nothing if the template is not found!
  1398. }
  1399.  
  1400. // save our template wp_query & load
  1401. tdb_state_template::set_wp_query( $wp_query_template );
  1402.  
  1403. // do the redirect
  1404. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_page.php';
  1405. }
  1406.  
  1407. return $original_template;
  1408. }
  1409.  
  1410.  
  1411. /**
  1412. * This hook is in the theme, it allows us to provide a redirect for the single pages on the front end
  1413. * we run it on the post template if set and on the global template if no post template is set. Not the best solution...
  1414. * in: the template id
  1415. * out: the new template path
  1416. */
  1417. add_filter('td_single_override', function($template_id) {
  1418.  
  1419. if ( !empty($template_id) && class_exists('Mobile_Detect')) {
  1420. $mobile_detect = new Mobile_Detect();
  1421. if ( $mobile_detect->isMobile()) {
  1422. $tdc_mobile_template_id = get_post_meta($template_id, 'tdc_mobile_template_id', true);
  1423. if ( !empty($tdc_mobile_template_id) && 'publish' === get_post_status( $tdc_mobile_template_id ) ) {
  1424. $template_id = $tdc_mobile_template_id;
  1425. }
  1426. }
  1427. }
  1428.  
  1429. /*
  1430. * Code from wp get_posts (previous was with WP_Query). Diff from previous WP_Query is that wmpl hooks on query without default wp_parse_args. That's why wp get_posts inside code is used
  1431. */
  1432. $args = array(
  1433. 'p' => $template_id,
  1434. 'post_type' => 'tdb_templates'
  1435. );
  1436.  
  1437. $defaults = array(
  1438. 'numberposts' => 5,
  1439. 'category' => 0,
  1440. 'orderby' => 'date',
  1441. 'order' => 'DESC',
  1442. 'include' => array(),
  1443. 'exclude' => array(),
  1444. 'meta_key' => '',
  1445. 'meta_value' => '',
  1446. 'post_type' => 'post',
  1447. 'suppress_filters' => true,
  1448. );
  1449.  
  1450. $parsed_args = wp_parse_args( $args, $defaults );
  1451. if ( empty( $parsed_args['post_status'] ) ) {
  1452. $parsed_args['post_status'] = ( 'attachment' == $parsed_args['post_type'] ) ? 'inherit' : 'publish';
  1453. }
  1454. if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
  1455. $parsed_args['posts_per_page'] = $parsed_args['numberposts'];
  1456. }
  1457. if ( ! empty( $parsed_args['category'] ) ) {
  1458. $parsed_args['cat'] = $parsed_args['category'];
  1459. }
  1460. if ( ! empty( $parsed_args['include'] ) ) {
  1461. $incposts = wp_parse_id_list( $parsed_args['include'] );
  1462. $parsed_args['posts_per_page'] = count( $incposts ); // only the number of posts included
  1463. $parsed_args['post__in'] = $incposts;
  1464. } elseif ( ! empty( $parsed_args['exclude'] ) ) {
  1465. $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
  1466. }
  1467.  
  1468. $parsed_args['ignore_sticky_posts'] = true;
  1469. $parsed_args['no_found_rows'] = true;
  1470.  
  1471. $wp_query_template = new WP_Query;
  1472. $posts = $wp_query_template->query( $parsed_args );
  1473.  
  1474. // do not redirect the theme template if we don't find the template
  1475. // the template was probably deleted or something
  1476. if (count($posts) === 0) {
  1477. return $template_id; // do nothing if the template is not found!
  1478. }
  1479.  
  1480. // save our template wp_query & load
  1481. tdb_state_template::set_wp_query($wp_query_template);
  1482.  
  1483. // do the redirect
  1484. return TDB_TEMPLATE_BUILDER_DIR . '/wp_templates/tdb_view_single.php';
  1485. });
  1486.  
  1487.  
  1488. /**
  1489. * JS: add tdb_globals to wp-admin
  1490. */
  1491. add_filter('admin_head', function(){
  1492.  
  1493. if ( td_util::is_mobile_theme() ) {
  1494. return;
  1495. }
  1496.  
  1497. $tdb_globals = [
  1498. 'wpRestNonce' => wp_create_nonce('wp_rest'),
  1499. 'wpRestUrl' => rest_url(),
  1500. 'wpAdminUrl' => admin_url(),
  1501. 'permalinkStructure' => get_option('permalink_structure'),
  1502. 'tdbTemplateType' => tdc_util::get_get_val('tdbTemplateType'),
  1503. 'deployMode' => TDB_DEPLOY_MODE,
  1504. 'globalSettings' => [],
  1505. ];
  1506.  
  1507. foreach (['tdcSavingHistory'] as $item) {
  1508. $existing_val = td_util::get_option($item);
  1509. $tdb_globals['globalSettings'][$item] = empty($existing_val) ? false : $existing_val;
  1510. }
  1511.  
  1512. ?>
  1513. <script>
  1514. window.tdb_globals = <?php echo json_encode( $tdb_globals );?>;
  1515. </script>
  1516.  
  1517. <style>
  1518. body.post-type-tdb_templates .page-title-action {
  1519. display: none;
  1520. }
  1521. </style>
  1522. <?php
  1523.  
  1524. // add tdc_wm_global_colors js global
  1525. $tdc_wm_global_colors = td_util::get_option(
  1526. 'tdc_wm_global_colors',
  1527. array(
  1528. 'accent_color' => array(
  1529. 'name' => 'Theme Color',
  1530. 'color' => '#fff',
  1531. 'default' => true
  1532. )
  1533. )
  1534. );
  1535.  
  1536. // add tdc_wm_global_fonts js global
  1537. $tdc_wm_global_fonts = td_util::get_option('tdc_wm_global_fonts' );
  1538.  
  1539. if ( empty( $tdc_wm_global_fonts ) ) {
  1540. $tdc_wm_global_fonts = array();
  1541. }
  1542.  
  1543. // add tdc_wm_custom_svg_icons js global
  1544. $tdc_wm_custom_svg_icons = td_util::get_option('tdc_wm_custom_svg_icons');
  1545. if( empty( $tdc_wm_custom_svg_icons ) ) {
  1546. $tdc_wm_custom_svg_icons = array();
  1547. }
  1548.  
  1549. ?>
  1550. <script>
  1551. window.tdc_wm_global_colors = <?php echo json_encode( $tdc_wm_global_colors ); ?>;
  1552. window.tdc_wm_global_fonts = <?php echo json_encode( $tdc_wm_global_fonts ); ?>;
  1553. window.tdc_wm_custom_svg_icons = <?php echo json_encode( $tdc_wm_custom_svg_icons ); ?>;
  1554. var tdc_wm_global_colors_nonce = <?php echo json_encode( wp_create_nonce('tdc-wm-global-colors') ); ?>;
  1555. var tdc_wm_custom_svg_icons_nonce = <?php echo json_encode( wp_create_nonce('tdc-wm-custom-svg-icons') ); ?>;
  1556. var tdc_wm_global_fonts_nonce = <?php echo json_encode( wp_create_nonce('tdc-wm-global-fonts') ); ?>;
  1557. </script>
  1558. <?php
  1559.  
  1560. });
  1561.  
  1562. /**
  1563. * JS: add the tdb_globals for frontend also..
  1564. * @note: this is need for autoload(infinite) posts ajax calls
  1565. */
  1566. add_filter('wp_head', function () {
  1567.  
  1568. if ( td_util::is_mobile_theme() ) {
  1569. return;
  1570. }
  1571.  
  1572. $tdb_globals = array (
  1573. 'wpRestNonce' => wp_create_nonce('wp_rest'),
  1574. 'wpRestUrl' => rest_url(),
  1575. 'permalinkStructure' => get_option('permalink_structure'),
  1576. 'globalSettings' => [],
  1577. );
  1578.  
  1579. foreach (['tdcSavingHistory'] as $item) {
  1580. $existing_val = td_util::get_option($item);
  1581. $tdb_globals['globalSettings'][$item] = empty($existing_val) ? false : $existing_val;
  1582. }
  1583.  
  1584. if ( ! tdb_state::is_ajax() ) {
  1585. $tdb_globals['isAjax'] = false;
  1586. $tdb_globals['isAdminBarShowing'] = is_admin_bar_showing();
  1587.  
  1588. $tdb_p_autoload_scroll_percent = intval( td_util::get_option('tdb_p_autoload_scroll_percent', 50 ) );
  1589.  
  1590. if ( tdb_util::check_in_range( $tdb_p_autoload_scroll_percent, 1, 100 ) ) {
  1591. $tdb_globals['autoloadScrollPercent'] = $tdb_p_autoload_scroll_percent;
  1592. } else {
  1593. $tdb_globals['autoloadScrollPercent'] = 50;
  1594. }
  1595.  
  1596. global $post;
  1597.  
  1598. if ( is_singular('post') ) {
  1599. $tdb_globals['postAutoloadStatus'] = td_util::get_option('tdb_p_autoload_status', 'off');
  1600. if ( !empty($post) ){
  1601. $tdb_globals['origPostEditUrl'] = get_edit_post_link($post->ID);
  1602. }
  1603. }
  1604.  
  1605. } else {
  1606. $tdb_globals['isAjax'] = true;
  1607. }
  1608.  
  1609. ?>
  1610. <script>
  1611. window.tdb_globals = <?php echo json_encode( $tdb_globals );?>;
  1612. </script>
  1613. <?php
  1614. });
  1615.  
  1616.  
  1617. /**
  1618. * add tdc website manager global colors style for admin/frontend
  1619. */
  1620. add_filter( 'wp_head', 'tdc_website_manager_global_colors_style' );
  1621. add_filter( 'admin_head', 'tdc_website_manager_global_colors_style' );
  1622. function tdc_website_manager_global_colors_style() {
  1623.  
  1624. // mobile theme exception
  1625. if ( td_util::is_mobile_theme() ) {
  1626. return;
  1627. }
  1628.  
  1629. // tdc website manager global colors style
  1630. $tdc_wm_global_colors = td_util::get_option(
  1631. 'tdc_wm_global_colors',
  1632. array(
  1633. 'accent_color' => array(
  1634. 'name' => 'Theme Color',
  1635. 'color' => '#fff',
  1636. 'default' => true
  1637. )
  1638. )
  1639. );
  1640.  
  1641. echo PHP_EOL;
  1642. ?>
  1643. <style id="tdb-global-colors">
  1644. <?php
  1645. echo ':root {';
  1646. foreach ( $tdc_wm_global_colors as $color_option_id => $color_data ) {
  1647. echo '--' . str_replace( '_', '-', $color_option_id ) . ': ' . $color_data['color'] . ';';
  1648. }
  1649. echo '}' . PHP_EOL;
  1650. ?>
  1651. </style>
  1652. <?php
  1653.  
  1654. // tdc website manager global fonts style
  1655. $tdc_wm_global_fonts = td_util::get_option('tdc_wm_global_fonts' );
  1656.  
  1657. echo PHP_EOL;
  1658.  
  1659. if ( !empty( $tdc_wm_global_fonts ) ) {
  1660. ?>
  1661. <style id="tdb-global-fonts">
  1662. <?php
  1663. echo ':root {';
  1664. $font_family_list = td_util::get_font_family_list();
  1665. foreach ( $tdc_wm_global_fonts as $font_option_id => $font_data ) {
  1666.  
  1667. // get the font family name using global font key from font fam list
  1668. $font_family = array_search( $font_data['key'], $font_family_list );
  1669.  
  1670. if ( $font_family ) {
  1671. //echo '--' . str_replace( '_', '-', $font_option_id ) . ': ' . $font_family . ';';
  1672. echo '--' . $font_option_id . ': ' . $font_family . ';';
  1673. }
  1674.  
  1675. }
  1676. echo '}' . PHP_EOL;
  1677. ?>
  1678. </style>
  1679. <?php
  1680. }
  1681.  
  1682. }
  1683.  
  1684. /**
  1685. * Get the template id to get icon fonts
  1686. */
  1687. add_filter( 'tdc_filter_icon_fonts_post_id', function( $post_id ) {
  1688. if ( tdb_state_template::has_wp_query() ) {
  1689. return tdb_state_template::get_wp_query()->post->ID;
  1690. }
  1691. return $post_id;
  1692.  
  1693. }, 10, 1);
  1694.  
  1695.  
  1696. /**
  1697. * Get the template id to get google fonts
  1698. */
  1699. add_filter( 'td_filter_google_fonts_post_id', function( $post_id ) {
  1700. if ( tdb_state_template::has_wp_query() ) {
  1701. return tdb_state_template::get_wp_query()->post->ID;
  1702. }
  1703. return $post_id;
  1704.  
  1705. }, 10, 1);
  1706.  
  1707.  
  1708. add_action('wp_before_admin_bar_render', 'tdb_on_before_admin_bar_render');
  1709. function tdb_on_before_admin_bar_render() {
  1710. global $wp_admin_bar, $post, $wp_query;
  1711.  
  1712. if ( td_util::is_mobile_theme() ) {
  1713. return;
  1714. }
  1715.  
  1716. if ( is_user_logged_in() && current_user_can( 'manage_categories' ) && is_admin_bar_showing() ) {
  1717.  
  1718. if ( tdb_state_content::has_wp_query() ) {
  1719.  
  1720. $tdbLoadDataFromId = '';
  1721. switch ( tdb_state_template::get_template_type() ) {
  1722. case 'single':
  1723. case 'cpt':
  1724. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->post->ID;
  1725. break;
  1726.  
  1727. case 'category':
  1728. case 'cpt_tax':
  1729. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object_id;
  1730. break;
  1731.  
  1732. case 'woo_archive':
  1733. if ( isset( tdb_state_content::get_wp_query()->queried_object ) ) {
  1734. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object->term_id;
  1735. } else {
  1736. $term = get_term_by('slug', tdb_state_content::get_wp_query()->get('term'), tdb_state_content::get_wp_query()->get('taxonomy') );
  1737. $tdbLoadDataFromId = $term->term_id;
  1738. }
  1739. break;
  1740.  
  1741. case 'author':
  1742. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->query_vars[ 'author' ];
  1743. break;
  1744.  
  1745. case 'search':
  1746. case 'woo_search_archive':
  1747. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->query_vars['s'];
  1748. break;
  1749.  
  1750. case 'date':
  1751. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->query_vars[ 'year' ];
  1752. break;
  1753.  
  1754. case 'tag':
  1755. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->query_vars[ 'tag_id' ];
  1756. break;
  1757.  
  1758. case 'woo_product':
  1759. case 'attachment':
  1760. $tdbLoadDataFromId = tdb_state_content::get_wp_query()->queried_object->ID;
  1761. break;
  1762.  
  1763. case 'woo_shop_base':
  1764. $tdbLoadDataFromId = wc_get_page_id('shop');
  1765. break;
  1766.  
  1767. }
  1768.  
  1769.  
  1770. $urlFromId = '';
  1771. if (!empty($tdbLoadDataFromId)) {
  1772. $urlFromId = '&tdbLoadDataFromId=' . $tdbLoadDataFromId;
  1773. }
  1774.  
  1775. // edit single page
  1776. $wp_admin_bar->add_menu(
  1777. array(
  1778. 'id' => 'tdb_template_builder',
  1779. 'title' => 'Edit with TagDiv Composer<span class="td-mobile-title"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1536 1024"><path d="M1518.592 537.6l-494.592 460.8c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-171.008-171.008-179.2 171.008c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-486.4-460.8c-25.6-17.408-25.6-51.2 0-68.608l494.592-451.584c8.192-9.216 17.408-17.408 33.792-17.408 17.408 0 25.6 8.192 43.008 17.408l179.2 161.792 179.2-161.792c8.192-9.216 25.6-17.408 43.008-17.408 16.384 0 25.6 8.192 41.984 17.408l495.616 451.584c16.384 17.408 16.384 51.2-9.216 68.608zM1288.192 486.4l-272.384-256c-9.216-8.192-9.216-8.192-17.408-8.192s-17.408 0-17.408 8.192l-425.984 392.192-137.216-118.784 230.4-214.016-84.992-59.392c0-8.192-8.192-8.192-17.408-8.192-8.192 0-16.384 0-16.384 8.192l-273.408 256c-17.408 8.192-17.408 25.6-8.192 33.792l272.384 256c9.216 9.216 9.216 9.216 17.408 9.216s17.408 0 17.408-9.216l425.984-392.192 128 119.808-221.184 204.8 76.8 67.584c8.192 9.216 8.192 9.216 16.384 9.216 9.216 0 17.408 0 17.408-9.216l273.408-256c25.6-8.192 25.6-25.6 16.384-33.792z" fill="%23fff"></path></svg> Edit</span>',
  1780. 'href' => admin_url( 'post.php?post_id=' . tdb_state_template::get_wp_query()->post->ID . '&td_action=tdc&' . $urlFromId . '&tdbTemplateType=' . tdb_state_template::get_template_type() . '&prev_url=' . rawurlencode( tdc_util::get_current_url() ) ),
  1781. 'meta' => array(
  1782. 'title' => 'Edit the template!'
  1783. ),
  1784. )
  1785. );
  1786.  
  1787. if ( 'single' === tdb_state_template::get_template_type() ) {
  1788.  
  1789. $td_post_theme_settings = td_util::get_post_meta_array( $tdbLoadDataFromId, 'td_post_theme_settings' );
  1790. if ( empty( $td_post_theme_settings[ 'td_post_template' ] ) ) {
  1791.  
  1792. $default_template_id = td_util::get_option( 'td_default_site_post_template' );
  1793. if ( td_global::is_tdb_template( $default_template_id, true ) ) {
  1794. $default_template_id = td_global::tdb_get_template_id( $default_template_id );
  1795. $current_template = '<span class="tdb-template-el-global-checked">Global</span> ' . get_the_title( $default_template_id );
  1796. } else {
  1797.  
  1798. if ( empty( $default_template_id ) ) {
  1799. $default_template_id = 'DEFAULT';
  1800. }
  1801. $current_template = '<span class="tdb-template-el-global-checked">Global</span> ' . $default_template_id;
  1802. }
  1803.  
  1804. } else {
  1805. $current_template = get_the_title( tdb_state_template::get_wp_query()->post->ID );
  1806. }
  1807.  
  1808. $wp_admin_bar->add_menu(
  1809. array(
  1810. 'id' => 'tdb_template_single',
  1811. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  1812. 'href' => '#',
  1813. 'meta' => array(
  1814. 'title' => 'Current Single Template',
  1815. 'class' => 'singleid-' . $tdbLoadDataFromId
  1816. ),
  1817. )
  1818. );
  1819.  
  1820. } else if ( 'category' === tdb_state_template::get_template_type() ) {
  1821.  
  1822. td_show_category_template_menu();
  1823.  
  1824. } else if ( 'date' === tdb_state_template::get_template_type() ) {
  1825.  
  1826. td_show_template_menu('date');
  1827.  
  1828. } else if ( 'search' === tdb_state_template::get_template_type() ) {
  1829.  
  1830. td_show_template_menu('search');
  1831.  
  1832. } else if ( 'attachment' === tdb_state_template::get_template_type() ) {
  1833.  
  1834. td_show_template_menu('attachment');
  1835.  
  1836. } else if ( 'author' === tdb_state_template::get_template_type() ) {
  1837.  
  1838. td_show_author_template_menu();
  1839.  
  1840. } else if ( 'tag' === tdb_state_template::get_template_type() ) {
  1841.  
  1842. td_show_tag_template_menu();
  1843.  
  1844. } else if ( 'woo_product' === tdb_state_template::get_template_type()) {
  1845.  
  1846. td_show_woo_product_template_menu();
  1847.  
  1848. } else if ( 'woo_archive' === tdb_state_template::get_template_type() ) {
  1849.  
  1850. td_show_woo_archive_template_menu();
  1851.  
  1852. } else if ( 'woo_search_archive' === tdb_state_template::get_template_type()) {
  1853.  
  1854. td_show_woo_search_archive_template_menu();
  1855.  
  1856. } else if ( 'woo_shop_base' === tdb_state_template::get_template_type()) {
  1857.  
  1858. td_show_woo_shop_base_template_menu();
  1859.  
  1860. } else if ( 'cpt' === tdb_state_template::get_template_type() ) {
  1861.  
  1862. td_show_cpt_template_menu(tdb_state_content::get_wp_query()->post->post_type);
  1863.  
  1864. } else if ( 'cpt_tax' === tdb_state_template::get_template_type() ) {
  1865.  
  1866. if ( isset( tdb_state_content::get_wp_query()->queried_object ) ) {
  1867. $term = tdb_state_content::get_wp_query()->queried_object;
  1868. } else {
  1869. $term = get_term_by('slug', tdb_state_content::get_wp_query()->get('term'), tdb_state_content::get_wp_query()->get('taxonomy') );
  1870. }
  1871.  
  1872. if (!empty($term)) {
  1873. td_show_cpt_tax_template_menu($term);
  1874. }
  1875. }
  1876.  
  1877. //var_dump(tdb_state_template::get_template_type()); die;
  1878. // Add cloud library menu link
  1879. $class_menu_item = '';
  1880.  
  1881. if ( 'woo_product' === tdb_state_template::get_template_type() && (function_exists('is_product') && is_product()) ) {
  1882. $class_menu_item = 'woo_productid-' . $post->ID;
  1883.  
  1884. } else if ( 'woo_archive' === tdb_state_template::get_template_type() || ( ( function_exists('is_product_category') && is_product_category() ) || ( function_exists( 'is_product_tag' ) && is_product_tag() ) ) ) {
  1885. $class_menu_item = 'woo_archive';
  1886.  
  1887. } else if ( 'single' === tdb_state_template::get_template_type() || 'single' === tdb_state_template::get_template_type() || is_singular( 'post' ) ) {
  1888. $class_menu_item = 'singleid-' . $post->ID;
  1889.  
  1890. } else if ( 'category' === tdb_state_template::get_template_type() || 'category' === tdb_state_template::get_template_type() || is_category() ) {
  1891. $queried_object = get_queried_object();
  1892. if ( $queried_object instanceof WP_Term ) {
  1893. $class_menu_item = 'categoryid-' . $queried_object->term_id;
  1894. }
  1895.  
  1896. } else if ( '404' === tdb_state_template::get_template_type() || is_404() ) {
  1897. $class_menu_item = '404';
  1898.  
  1899. } else if ( 'date' === tdb_state_template::get_template_type() || is_date() ) {
  1900. $class_menu_item = 'date';
  1901.  
  1902. } else if (( 'woo_search_archive' === tdb_state_template::get_template_type() || is_search() ) && ( $wp_query->get( 'wc_query' ) )) {
  1903. $class_menu_item = 'woo_search_archive';
  1904.  
  1905. } else if (( 'search' === tdb_state_template::get_template_type() || is_search() ) && ( ! $wp_query->get( 'wc_query' ) )) {
  1906. $class_menu_item = 'search';
  1907.  
  1908. } else if ( 'attachment' === tdb_state_template::get_template_type() || is_singular( 'attachment' ) ) {
  1909. $class_menu_item = 'attachment';
  1910.  
  1911. } else if ( 'author' === tdb_state_template::get_template_type() || is_author() ) {
  1912. $queried_object = get_queried_object();
  1913. if ( $queried_object instanceof WP_User ) {
  1914. $class_menu_item = 'authorid-' . $queried_object->ID;
  1915. }
  1916.  
  1917. } else if ( 'tag' === tdb_state_template::get_template_type() || 'tag' === tdb_state_template::get_template_type() || is_tag() ) {
  1918. $queried_object = get_queried_object();
  1919. if ( $queried_object instanceof WP_Term ) {
  1920. $class_menu_item = 'tagid-' . $queried_object->term_id;
  1921. }
  1922. }
  1923.  
  1924. if ( ! empty( $class_menu_item ) ) {
  1925. $wp_admin_bar->add_menu(
  1926. array(
  1927. 'id' => 'tdb_template_load',
  1928. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  1929. 'href' => '#',
  1930. 'meta' => array(
  1931. 'title' => 'Load template from TagDiv Cloud Library',
  1932. 'class' => $class_menu_item
  1933. ),
  1934. )
  1935. );
  1936. }
  1937.  
  1938. } elseif ( tdb_state_template::has_wp_query() ) {
  1939.  
  1940. $tdb_template_type = get_post_meta($post->ID, 'tdb_template_type', true);
  1941. if ( '404' === $tdb_template_type || is_404()) {
  1942.  
  1943. if ( is_404() && ( empty(tdb_state_template::get_wp_query()->post->ID) || ! get_post(tdb_state_template::get_wp_query()->post->ID) instanceof WP_Post ) ) {
  1944. $wp_admin_bar->add_menu(
  1945. array(
  1946. 'id' => 'tdb_template_builder_disabled',
  1947. 'title' => 'Edit with TagDiv Composer<span class="td-mobile-title"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1536 1024"><path d="M1518.592 537.6l-494.592 460.8c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-171.008-171.008-179.2 171.008c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-486.4-460.8c-25.6-17.408-25.6-51.2 0-68.608l494.592-451.584c8.192-9.216 17.408-17.408 33.792-17.408 17.408 0 25.6 8.192 43.008 17.408l179.2 161.792 179.2-161.792c8.192-9.216 25.6-17.408 43.008-17.408 16.384 0 25.6 8.192 41.984 17.408l495.616 451.584c16.384 17.408 16.384 51.2-9.216 68.608zM1288.192 486.4l-272.384-256c-9.216-8.192-9.216-8.192-17.408-8.192s-17.408 0-17.408 8.192l-425.984 392.192-137.216-118.784 230.4-214.016-84.992-59.392c0-8.192-8.192-8.192-17.408-8.192-8.192 0-16.384 0-16.384 8.192l-273.408 256c-17.408 8.192-17.408 25.6-8.192 33.792l272.384 256c9.216 9.216 9.216 9.216 17.408 9.216s17.408 0 17.408-9.216l425.984-392.192 128 119.808-221.184 204.8 76.8 67.584c8.192 9.216 8.192 9.216 16.384 9.216 9.216 0 17.408 0 17.408-9.216l273.408-256c25.6-8.192 25.6-25.6 16.384-33.792z" fill="%23fff"></path></svg> Edit</span>',
  1948. 'href' => '#',
  1949. 'meta' => array(
  1950. 'title' => 'Please select a tagDiv Builder template.'
  1951. ),
  1952. )
  1953. );
  1954. } else {
  1955. // edit template
  1956. $wp_admin_bar->add_menu(
  1957. array(
  1958. 'id' => 'tdb_template_builder',
  1959. 'title' => 'Edit with TagDiv Composer<span class="td-mobile-title"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1536 1024"><path d="M1518.592 537.6l-494.592 460.8c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-171.008-171.008-179.2 171.008c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-486.4-460.8c-25.6-17.408-25.6-51.2 0-68.608l494.592-451.584c8.192-9.216 17.408-17.408 33.792-17.408 17.408 0 25.6 8.192 43.008 17.408l179.2 161.792 179.2-161.792c8.192-9.216 25.6-17.408 43.008-17.408 16.384 0 25.6 8.192 41.984 17.408l495.616 451.584c16.384 17.408 16.384 51.2-9.216 68.608zM1288.192 486.4l-272.384-256c-9.216-8.192-9.216-8.192-17.408-8.192s-17.408 0-17.408 8.192l-425.984 392.192-137.216-118.784 230.4-214.016-84.992-59.392c0-8.192-8.192-8.192-17.408-8.192-8.192 0-16.384 0-16.384 8.192l-273.408 256c-17.408 8.192-17.408 25.6-8.192 33.792l272.384 256c9.216 9.216 9.216 9.216 17.408 9.216s17.408 0 17.408-9.216l425.984-392.192 128 119.808-221.184 204.8 76.8 67.584c8.192 9.216 8.192 9.216 16.384 9.216 9.216 0 17.408 0 17.408-9.216l273.408-256c25.6-8.192 25.6-25.6 16.384-33.792z" fill="%23fff"></path></svg> Edit</span>',
  1960. 'href' => admin_url( 'post.php?post_id=' . tdb_state_template::get_wp_query()->post->ID . '&td_action=tdc&tdbTemplateType=' . tdb_state_template::get_template_type() . '&prev_url=' . rawurlencode( tdc_util::get_current_url() ) ),
  1961. 'meta' => array(
  1962. 'title' => 'Edit the ' . tdb_state_template::get_template_type() . ' template.'
  1963. ),
  1964. )
  1965. );
  1966. }
  1967.  
  1968. td_show_template_menu('404');
  1969.  
  1970. } else if ( 'footer' !== $tdb_template_type ) {
  1971.  
  1972. // edit template
  1973. $wp_admin_bar->add_menu(
  1974. array(
  1975. 'id' => 'tdb_template_builder',
  1976. 'title' => 'Edit with TagDiv Composer<span class="td-mobile-title"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1536 1024"><path d="M1518.592 537.6l-494.592 460.8c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-171.008-171.008-179.2 171.008c-8.192 8.192-25.6 17.408-43.008 17.408-16.384 0-25.6-9.216-41.984-17.408l-486.4-460.8c-25.6-17.408-25.6-51.2 0-68.608l494.592-451.584c8.192-9.216 17.408-17.408 33.792-17.408 17.408 0 25.6 8.192 43.008 17.408l179.2 161.792 179.2-161.792c8.192-9.216 25.6-17.408 43.008-17.408 16.384 0 25.6 8.192 41.984 17.408l495.616 451.584c16.384 17.408 16.384 51.2-9.216 68.608zM1288.192 486.4l-272.384-256c-9.216-8.192-9.216-8.192-17.408-8.192s-17.408 0-17.408 8.192l-425.984 392.192-137.216-118.784 230.4-214.016-84.992-59.392c0-8.192-8.192-8.192-17.408-8.192-8.192 0-16.384 0-16.384 8.192l-273.408 256c-17.408 8.192-17.408 25.6-8.192 33.792l272.384 256c9.216 9.216 9.216 9.216 17.408 9.216s17.408 0 17.408-9.216l425.984-392.192 128 119.808-221.184 204.8 76.8 67.584c8.192 9.216 8.192 9.216 16.384 9.216 9.216 0 17.408 0 17.408-9.216l273.408-256c25.6-8.192 25.6-25.6 16.384-33.792z" fill="%23fff"></path></svg> Edit</span>',
  1977. 'href' => admin_url( 'post.php?post_id=' . tdb_state_template::get_wp_query()->post->ID . '&td_action=tdc&tdbTemplateType=' . tdb_state_template::get_template_type() . '&prev_url=' . rawurlencode( tdc_util::get_current_url() ) ),
  1978. 'meta' => array(
  1979. 'title' => 'Edit the ' . tdb_state_template::get_template_type() . ' template.'
  1980. ),
  1981. )
  1982. );
  1983.  
  1984. }
  1985.  
  1986. } elseif (
  1987. ( is_singular( 'post' ) && ! is_admin() ) ||
  1988. is_singular( 'attachment' ) ||
  1989. ( is_category() && ! is_admin() ) ||
  1990. ( is_author() && ! is_admin() ) ||
  1991. ( is_search() && ! is_admin() ) ||
  1992. ( is_date() && ! is_admin() ) ||
  1993. ( is_tag() && ! is_admin() ) ||
  1994. ( function_exists('is_product') && is_product() && ! is_admin() ) ||
  1995. ( ( ( function_exists('is_product_category') && is_product_category() ) || ( function_exists( 'is_product_tag' ) && is_product_tag() ) || ( is_tax() && function_exists( 'taxonomy_is_product_attribute' ) && taxonomy_is_product_attribute( $wp_query->get( 'taxonomy' ) ) ) ) && ! is_admin() ) ||
  1996. ( function_exists('is_shop') && is_shop() && ! is_admin() ) ||
  1997. is_404()
  1998. ) {
  1999.  
  2000. // Important! product first - because is_single
  2001.  
  2002. if ( function_exists('is_product') && is_product() ) {
  2003. td_show_woo_product_template_menu();
  2004.  
  2005. } else if ( ( function_exists('is_product_category') && is_product_category() ) || ( function_exists( 'is_product_tag' ) && is_product_tag() ) || ( is_tax() && function_exists( 'taxonomy_is_product_attribute' ) && taxonomy_is_product_attribute( $wp_query->get( 'taxonomy' ) ) ) ) {
  2006. td_show_woo_archive_template_menu();
  2007.  
  2008. } else if ( is_search() && $wp_query->get( 'wc_query' ) ) {
  2009. td_show_woo_search_archive_template_menu();
  2010.  
  2011. } else if ( function_exists('is_shop') && is_shop() ) {
  2012. td_show_woo_shop_base_template_menu();
  2013.  
  2014. } else if ( is_single() ) {
  2015.  
  2016. $current_template = '';
  2017.  
  2018. $td_post_theme_settings = td_util::get_post_meta_array($post->ID, 'td_post_theme_settings');
  2019. if (empty($td_post_theme_settings) || empty($td_post_theme_settings['td_post_template'])) {
  2020. $current_template = '<span class="tdb-template-el-global-checked">Global</span> STANDARD - ';
  2021. $default_template_id = td_util::get_option( 'td_default_site_post_template' );
  2022. if ( empty($default_template_id )) {
  2023. $current_template .= 'DEFAULT';
  2024. } else {
  2025. $current_template .= $default_template_id;
  2026. }
  2027. } else if ( !empty($td_post_theme_settings['td_post_template'])){
  2028. $current_template = $td_post_theme_settings['td_post_template'];
  2029. }
  2030.  
  2031. if ( !empty($current_template)) {
  2032.  
  2033. $wp_admin_bar->add_menu(
  2034. array(
  2035. 'id' => 'tdb_template_single',
  2036. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2037. 'href' => '#',
  2038. 'meta' => array(
  2039. 'title' => 'Current Standard Template',
  2040. 'class' => 'singleid-' . $post->ID
  2041. ),
  2042. )
  2043. );
  2044.  
  2045. $wp_admin_bar->add_menu(
  2046. array(
  2047. 'id' => 'tdb_template_load',
  2048. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2049. 'href' => '#',
  2050. 'meta' => array(
  2051. 'title' => 'Load template from TagDiv Cloud Library',
  2052. 'class' => 'singleid-' . $post->ID
  2053. ),
  2054. )
  2055. );
  2056. }
  2057.  
  2058. } else if (is_category()) {
  2059. td_show_category_template_menu();
  2060. } else if (is_404()) {
  2061. td_show_template_menu('404');
  2062. } else if (is_date()) {
  2063. td_show_template_menu('date');
  2064. } else if (is_search()) {
  2065. td_show_template_menu('search');
  2066. } else if (is_attachment()) {
  2067. td_show_template_menu('attachment');
  2068. } else if (is_author()) {
  2069. td_show_author_template_menu();
  2070. } else if (is_tag()) {
  2071. td_show_tag_template_menu();
  2072. }
  2073. }
  2074. }
  2075. }
  2076.  
  2077. function td_show_category_template_menu() {
  2078. global $wp_admin_bar;
  2079.  
  2080. $global_title = '';
  2081. $category_id = '';
  2082. $is_global = false;
  2083. $is_tdb_template = false;
  2084.  
  2085. $template_id = td_api_category_template::get_current_category_template( $category_id,$is_global, $is_tdb_template );
  2086. if ( $is_tdb_template ) {
  2087. if ( $is_global ) {
  2088. $global_title = '<span class="tdb-template-el-global-checked">Global</span> ';
  2089. }
  2090. } else {
  2091. if ( $is_global ) {
  2092. $global_title = '<span class="tdb-template-el-global-checked">Global</span> ';
  2093. }
  2094. }
  2095.  
  2096. if ( empty( $template_id ) ) {
  2097. $template_name = 'Category Template 1';
  2098. } else if ( 0 === strpos( $template_id, 'td_category_template_' ) ) {
  2099. $template_name = 'Category Template ' . str_replace('td_category_template_', '', $template_id );
  2100. } else {
  2101. $template_name = get_the_title( $template_id );
  2102. }
  2103.  
  2104. $current_template = $global_title . $template_name;
  2105.  
  2106. if ( !empty( $current_template ) ) {
  2107.  
  2108. $wp_admin_bar->add_menu(
  2109. array(
  2110. 'id' => 'tdb_template_category',
  2111. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2112. 'href' => '#',
  2113. 'meta' => array(
  2114. 'title' => 'Current Category Template',
  2115. 'class' => 'categoryid-' . $category_id
  2116. ),
  2117. )
  2118. );
  2119.  
  2120. $wp_admin_bar->add_menu(
  2121. array(
  2122. 'id' => 'tdb_template_load',
  2123. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2124. 'href' => '#',
  2125. 'meta' => array(
  2126. 'title' => 'Load template from TagDiv Cloud Library',
  2127. 'class' => 'categoryid-' . $category_id
  2128. ),
  2129. )
  2130. );
  2131. }
  2132. }
  2133.  
  2134. function td_show_author_template_menu() {
  2135. global $wp_admin_bar;
  2136.  
  2137. $is_global = false;
  2138. $is_tdb_template = false;
  2139.  
  2140. $queried_object = get_queried_object();
  2141. if ( $queried_object instanceof WP_User ) {
  2142.  
  2143. $global_title = '';
  2144. $author_id = $queried_object->ID;
  2145. $lang = '';
  2146. if (class_exists('SitePress', false)) {
  2147. global $sitepress;
  2148. $sitepress_settings = $sitepress->get_settings();
  2149. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  2150. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2151. if (1 === $translation_mode) {
  2152. $lang = $sitepress->get_current_language();
  2153. }
  2154. }
  2155. }
  2156.  
  2157. $option_id = 'tdb_author_templates' . $lang;
  2158. $tdb_author_templates = td_util::get_option( $option_id );
  2159.  
  2160. $tdb_individual_author_template = $tdb_author_templates[$author_id];
  2161.  
  2162. if ( empty( $tdb_individual_author_template ) ) {
  2163. $is_global = true;
  2164.  
  2165. $tdb_author_template = td_util::get_option( 'tdb_author_template' );
  2166. if ( !empty( $tdb_author_template ) && td_global::is_tdb_template( $tdb_author_template, true ) ) {
  2167. $is_tdb_template = true;
  2168. $template_id = td_global::tdb_get_template_id( $tdb_author_template );
  2169. } else {
  2170. $template_id = td_util::get_option( 'tds_author_page_layout' );
  2171. }
  2172.  
  2173. } else {
  2174.  
  2175. if ( td_global::is_tdb_template( $tdb_individual_author_template, true ) ) {
  2176. $is_tdb_template = true;
  2177. $template_id = td_global::tdb_get_template_id( $tdb_individual_author_template );
  2178. } else {
  2179. $template_id = td_util::get_option( 'tds_author_page_layout' );
  2180. }
  2181.  
  2182. }
  2183.  
  2184.  
  2185. if ( $is_global ) {
  2186. $global_title = '<span class="tdb-template-el-global-checked">Global</span> ';
  2187. }
  2188.  
  2189. if ( $is_tdb_template) {
  2190. $template_name = get_the_title( $template_id );
  2191. } else {
  2192. $template_name = 'Author Template ' . $template_id;
  2193. }
  2194.  
  2195. $current_template = $global_title . $template_name;
  2196.  
  2197. if ( !empty( $current_template ) ) {
  2198.  
  2199. $wp_admin_bar->add_menu(
  2200. array(
  2201. 'id' => 'tdb_template_author',
  2202. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2203. 'href' => '#',
  2204. 'meta' => array(
  2205. 'title' => 'Current Author Template',
  2206. 'class' => 'authorid-' . $author_id
  2207. ),
  2208. )
  2209. );
  2210.  
  2211. $wp_admin_bar->add_menu(
  2212. array(
  2213. 'id' => 'tdb_template_load',
  2214. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2215. 'href' => '#',
  2216. 'meta' => array(
  2217. 'title' => 'Load template from TagDiv Cloud Library',
  2218. 'class' => 'authorid-' . $author_id
  2219. ),
  2220. )
  2221. );
  2222. }
  2223. }
  2224. }
  2225.  
  2226. function td_show_tag_template_menu() {
  2227. global $wp_admin_bar;
  2228.  
  2229. $is_global = false;
  2230. $is_tdb_template = false;
  2231.  
  2232. $queried_object = get_queried_object();
  2233. if ( $queried_object instanceof WP_Term ) {
  2234.  
  2235. $global_title = '';
  2236. $lang = '';
  2237. if (class_exists('SitePress', false)) {
  2238. global $sitepress;
  2239. $sitepress_settings = $sitepress->get_settings();
  2240. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  2241. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2242. if (1 === $translation_mode) {
  2243. $lang = $sitepress->get_current_language();
  2244. }
  2245. }
  2246. }
  2247.  
  2248. $option_id = 'tdb_tag_templates' . $lang;
  2249.  
  2250. $tdb_tag_templates = td_options::get( $option_id );
  2251. if ( is_array($tdb_tag_templates)) {
  2252. $queried_object = get_queried_object();
  2253. foreach ( $tdb_tag_templates as $tdb_tag_template_id => $tags ) {
  2254.  
  2255. if ( false !== strpos( $tags, $queried_object->slug) && td_global::is_tdb_template( $tdb_tag_template_id, true )) {
  2256. $is_tdb_template = true;
  2257. $template_id = td_global::tdb_get_template_id( $tdb_tag_template_id );
  2258. }
  2259. }
  2260. }
  2261.  
  2262. if ( empty($template_id) ) {
  2263.  
  2264. $is_global = true;
  2265. $lang = '';
  2266. if (class_exists('SitePress', false)) {
  2267. global $sitepress;
  2268. $sitepress_settings = $sitepress->get_settings();
  2269. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  2270. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2271. if (1 === $translation_mode) {
  2272. $lang = $sitepress->get_current_language();
  2273. }
  2274. }
  2275. }
  2276.  
  2277. $option_id = 'tdb_tag_template' . $lang;
  2278. $tdb_tag_template = td_options::get( $option_id );
  2279.  
  2280. if ( td_global::is_tdb_template( $tdb_tag_template, true ) ) {
  2281. $is_tdb_template = true;
  2282. $template_id = td_global::tdb_get_template_id( $tdb_tag_template );
  2283. }
  2284. }
  2285.  
  2286. if ( empty( $template_id )) {
  2287. $template_id = td_options::get('tds_tag_page_layout');
  2288. }
  2289.  
  2290.  
  2291. if ( $is_global ) {
  2292. $global_title = '<span class="tdb-template-el-global-checked">Global</span> ';
  2293. }
  2294.  
  2295. if ( $is_tdb_template) {
  2296. $template_name = get_the_title( $template_id );
  2297. } else {
  2298. $template_name = 'Tag Template ' . $template_id;
  2299. }
  2300.  
  2301. $current_template = $global_title . $template_name;
  2302.  
  2303. if ( !empty($current_template)) {
  2304.  
  2305. $wp_admin_bar->add_menu(
  2306. array(
  2307. 'id' => 'tdb_template_tag',
  2308. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2309. 'href' => '#',
  2310. 'meta' => array(
  2311. 'title' => 'Current Tag Template',
  2312. 'class' => 'tagid-' . $queried_object->term_id
  2313. ),
  2314. )
  2315. );
  2316.  
  2317. $wp_admin_bar->add_menu(
  2318. array(
  2319. 'id' => 'tdb_template_load',
  2320. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2321. 'href' => '#',
  2322. 'meta' => array(
  2323. 'title' => 'Load template from TagDiv Cloud Library',
  2324. 'class' => 'tagid-' . $queried_object->term_id
  2325. ),
  2326. )
  2327. );
  2328. }
  2329. }
  2330. }
  2331.  
  2332. function td_show_woo_product_template_menu() {
  2333.  
  2334. if ( !defined( 'TD_WOO' ) )
  2335. return;
  2336.  
  2337. global $wp_admin_bar;
  2338.  
  2339. $template_type = 'woo_product';
  2340.  
  2341. $tdb_option_key = 'tdb_' . $template_type . '_template';
  2342.  
  2343. $post_id = '';
  2344.  
  2345. $queried_object = get_queried_object();
  2346. if ( $queried_object instanceof WP_Post && 'product' === $queried_object->post_type ) {
  2347. $post_id = $queried_object->ID;
  2348. $td_post_theme_settings = td_util::get_post_meta_array( $post_id, 'td_post_theme_settings' );
  2349.  
  2350. $template_id = null;
  2351. if ( ! empty( $td_post_theme_settings[ 'td_post_template' ] ) ) {
  2352. if ( td_global::is_tdb_template( $td_post_theme_settings[ 'td_post_template' ], true ) ) {
  2353. $template_id = td_global::tdb_get_template_id( $td_post_theme_settings[ 'td_post_template' ] );
  2354. }
  2355. }
  2356.  
  2357. if ( empty( $template_id ) ) {
  2358. $current_template = '<span class="tdb-template-el-global-checked">Global</span>';
  2359. $default_template_id = td_util::get_option( $tdb_option_key );
  2360.  
  2361. if ( td_global::is_tdb_template( $default_template_id, true ) ) {
  2362. $current_template .= ' ' . get_the_title( td_global::tdb_get_template_id( $default_template_id ) );
  2363. } else {
  2364. $current_template .= ' DEFAULT';
  2365. }
  2366.  
  2367. } else {
  2368. $current_template = get_the_title( $template_id );
  2369. }
  2370. }
  2371.  
  2372. if ( ! empty( $current_template ) ) {
  2373.  
  2374. $wp_admin_bar->add_menu(
  2375. array(
  2376. 'id' => 'tdb_template_' . $template_type,
  2377. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2378. 'href' => '#',
  2379. 'meta' => array(
  2380. 'title' => 'Current Standard Template',
  2381. 'class' => $template_type . 'id-' . $post_id
  2382. ),
  2383. )
  2384. );
  2385.  
  2386. $wp_admin_bar->add_menu(
  2387. array(
  2388. 'id' => 'tdb_template_load',
  2389. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2390. 'href' => '#',
  2391. 'meta' => array(
  2392. 'title' => 'Load template from TagDiv Cloud Library',
  2393. 'class' => $template_type . 'id-' . $post_id
  2394. ),
  2395. )
  2396. );
  2397. }
  2398. }
  2399.  
  2400. function td_show_woo_archive_template_menu() {
  2401.  
  2402. if ( !defined( 'TD_WOO' ) )
  2403. return;
  2404.  
  2405. global $wp_admin_bar;
  2406.  
  2407. $queried_object = get_queried_object();
  2408.  
  2409. if ( $queried_object instanceof WP_Term ) {
  2410. $term_id = $queried_object->term_id;
  2411.  
  2412. switch ( $queried_object->taxonomy ) {
  2413. case 'product_tag':
  2414. $tdb_option_key = 'tdb_woo_archive_tag_template';
  2415. break;
  2416. case taxonomy_is_product_attribute( $queried_object->taxonomy ):
  2417. $tdb_option_key = 'tdb_woo_archive_attribute_template';
  2418. break;
  2419. case 'product_cat':
  2420. default:
  2421. $tdb_option_key = 'tdb_woo_archive_template';
  2422. break;
  2423. }
  2424.  
  2425. $tdb_woo_archive_template = get_term_meta( $term_id, $tdb_option_key, true );
  2426.  
  2427. if ( empty( $tdb_woo_archive_template ) ) {
  2428. $current_template = '<span class="tdb-template-el-global-checked">Global</span>';
  2429.  
  2430. // check for global prod attribute taxonomy template
  2431. if ( $tdb_option_key === 'tdb_woo_archive_attribute_template' ) {
  2432.  
  2433. // check for global prod attribute taxonomy template
  2434. $tdb_pa_tax_woo_archive_attribute_template = td_options::get( 'tdb_woo_attribute_' . $queried_object->taxonomy . '_tax_template' );
  2435. if ( td_global::is_tdb_template( $tdb_pa_tax_woo_archive_attribute_template, true ) ) {
  2436.  
  2437. // get the global prod attributes taxonomy tdb template
  2438. $default_template_id = $tdb_pa_tax_woo_archive_attribute_template;
  2439.  
  2440. } else {
  2441.  
  2442. // get the global prod attributes tdb template
  2443. $default_template_id = td_util::get_option( $tdb_option_key );
  2444.  
  2445. }
  2446.  
  2447. } else {
  2448.  
  2449. // get the global tdb template
  2450. $default_template_id = td_util::get_option( $tdb_option_key );
  2451.  
  2452. }
  2453.  
  2454. if ( empty( $default_template_id ) ) {
  2455. $current_template .= ' DEFAULT';
  2456. } elseif ( td_global::is_tdb_template( $default_template_id, true ) ) {
  2457. $current_template .= ' ' . get_the_title( td_global::tdb_get_template_id( $default_template_id ) );
  2458. }
  2459.  
  2460. } else {
  2461. $current_template = get_the_title( td_global::tdb_get_template_id( $tdb_woo_archive_template ) );
  2462. }
  2463. }
  2464.  
  2465. $template_type = 'woo_archive';
  2466. if ( !empty( $current_template ) ) {
  2467.  
  2468. $wp_admin_bar->add_menu(
  2469. array(
  2470. 'id' => 'tdb_template_' . $template_type,
  2471. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2472. 'href' => '#',
  2473. 'meta' => array(
  2474. 'title' => 'Current ' . ucfirst( $template_type ) . ' Template',
  2475. 'class' => $template_type . 'id-' . $term_id
  2476. ),
  2477. )
  2478. );
  2479.  
  2480. $wp_admin_bar->add_menu(
  2481. array(
  2482. 'id' => 'tdb_template_load',
  2483. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2484. 'href' => '#',
  2485. 'meta' => array(
  2486. 'title' => 'Load template from TagDiv Cloud Library',
  2487. 'class' => $template_type . 'id-' . $term_id
  2488. ),
  2489. )
  2490. );
  2491. }
  2492. }
  2493.  
  2494. function td_show_woo_shop_base_template_menu() {
  2495.  
  2496. if ( !defined( 'TD_WOO' ) )
  2497. return;
  2498.  
  2499. global $wp_admin_bar;
  2500.  
  2501. $global_title = '';
  2502. $is_tdb_template = false;
  2503.  
  2504. $template_type = 'woo_shop_base';
  2505.  
  2506. $tdb_option_key = 'tdb_' . $template_type . '_template';
  2507.  
  2508. $tdb_template = td_util::get_option( $tdb_option_key );
  2509.  
  2510. if ( td_global::is_tdb_template( $tdb_template, true ) ) {
  2511. $template_id = td_global::tdb_get_template_id( $tdb_template );
  2512. $is_tdb_template = true;
  2513. }
  2514.  
  2515. if (empty($template_id)) {
  2516. $template_name = 'Default';
  2517. } else {
  2518. if ( $is_tdb_template ) {
  2519. $template_name = get_the_title( $template_id );
  2520. } else {
  2521. $template_name = ucfirst($template_type) . ' Template ' . $template_id;
  2522. }
  2523. }
  2524.  
  2525. $current_template = $global_title . $template_name;
  2526.  
  2527. if ( !empty($current_template)) {
  2528.  
  2529. $menu_template_id = '';
  2530. if ($is_tdb_template) {
  2531. $menu_template_id = $template_id;
  2532. }
  2533.  
  2534. $wp_admin_bar->add_menu(
  2535. array(
  2536. 'id' => 'tdb_template_' . $template_type,
  2537. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2538. 'href' => '#',
  2539. 'meta' => array(
  2540. 'title' => 'Current ' . ucfirst( $template_type ) . ' Template',
  2541. 'class' => 'templateid-' . $menu_template_id
  2542. ),
  2543. )
  2544. );
  2545.  
  2546. $wp_admin_bar->add_menu(
  2547. array(
  2548. 'id' => 'tdb_template_load',
  2549. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2550. 'href' => '#',
  2551. 'meta' => array(
  2552. 'title' => 'Load template from TagDiv Cloud Library',
  2553. 'class' => $template_type
  2554. ),
  2555. )
  2556. );
  2557. }
  2558. }
  2559.  
  2560. function td_show_woo_search_archive_template_menu() {
  2561.  
  2562. if ( !defined( 'TD_WOO' ) )
  2563. return;
  2564.  
  2565. global $wp_admin_bar;
  2566.  
  2567. $global_title = '';
  2568. $is_tdb_template = false;
  2569.  
  2570. $template_type = 'woo_search_archive';
  2571.  
  2572. $tdb_option_key = 'tdb_' . $template_type . '_template';
  2573.  
  2574. $tdb_template = td_util::get_option( $tdb_option_key );
  2575.  
  2576. if ( td_global::is_tdb_template( $tdb_template, true ) ) {
  2577. $template_id = td_global::tdb_get_template_id( $tdb_template );
  2578. $is_tdb_template = true;
  2579. }
  2580.  
  2581. if (empty($template_id)) {
  2582. $template_name = 'Default';
  2583. } else {
  2584. if ( $is_tdb_template ) {
  2585. $template_name = get_the_title( $template_id );
  2586. } else {
  2587. $template_name = ucfirst($template_type) . ' Template ' . $template_id;
  2588. }
  2589. }
  2590.  
  2591. $current_template = $global_title . $template_name;
  2592.  
  2593. if ( !empty($current_template)) {
  2594.  
  2595. $menu_template_id = '';
  2596. if ($is_tdb_template) {
  2597. $menu_template_id = $template_id;
  2598. }
  2599.  
  2600. $wp_admin_bar->add_menu(
  2601. array(
  2602. 'id' => 'tdb_template_' . $template_type,
  2603. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2604. 'href' => '#',
  2605. 'meta' => array(
  2606. 'title' => 'Current ' . ucfirst( $template_type ) . ' Template',
  2607. 'class' => 'templateid-' . $menu_template_id
  2608. ),
  2609. )
  2610. );
  2611.  
  2612. $wp_admin_bar->add_menu(
  2613. array(
  2614. 'id' => 'tdb_template_load',
  2615. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2616. 'href' => '#',
  2617. 'meta' => array(
  2618. 'title' => 'Load template from TagDiv Cloud Library',
  2619. 'class' => $template_type
  2620. ),
  2621. )
  2622. );
  2623. }
  2624. }
  2625.  
  2626. function td_show_template_menu($template_type) {
  2627. global $wp_admin_bar;
  2628.  
  2629. $global_title = '';
  2630. $is_tdb_template = false;
  2631.  
  2632. $lang = '';
  2633. if (class_exists('SitePress', false)) {
  2634. global $sitepress;
  2635. $sitepress_settings = $sitepress->get_settings();
  2636. if ( isset($sitepress_settings['custom_posts_sync_option'][ 'tdb_templates']) ) {
  2637. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2638. if (1 === $translation_mode) {
  2639. $lang = $sitepress->get_current_language();
  2640. }
  2641. }
  2642. }
  2643.  
  2644. $tdb_option_key = 'tdb_' . $template_type . '_template' . $lang;
  2645. $tds_option_key = 'tds_' . $template_type . '_page_layout';
  2646.  
  2647. $tdb_template = td_util::get_option( $tdb_option_key );
  2648.  
  2649. if ( td_global::is_tdb_template( $tdb_template, true ) ) {
  2650. $template_id = td_global::tdb_get_template_id( $tdb_template );
  2651. $is_tdb_template = true;
  2652. } else {
  2653. $template_id = td_util::get_option( $tds_option_key );
  2654. }
  2655.  
  2656. if (empty($template_id)) {
  2657. $template_name = ucfirst($template_type) . ' Template 1';
  2658. } else {
  2659. if ( $is_tdb_template ) {
  2660. $template_name = get_the_title( $template_id );
  2661. } else {
  2662. $template_name = ucfirst($template_type) . ' Template ' . $template_id;
  2663. }
  2664. }
  2665.  
  2666. $current_template = $global_title . $template_name;
  2667.  
  2668. if ( !empty($current_template)) {
  2669.  
  2670. $menu_template_id = '';
  2671. if ($is_tdb_template) {
  2672. $menu_template_id = $template_id;
  2673. }
  2674.  
  2675. $wp_admin_bar->add_menu(
  2676. array(
  2677. 'id' => 'tdb_template_' . $template_type,
  2678. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2679. 'href' => '#',
  2680. 'meta' => array(
  2681. 'title' => 'Current ' . ucfirst( $template_type ) . ' Template',
  2682. 'class' => 'templateid-' . $menu_template_id
  2683. ),
  2684. )
  2685. );
  2686.  
  2687. $wp_admin_bar->add_menu(
  2688. array(
  2689. 'id' => 'tdb_template_load',
  2690. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2691. 'href' => '#',
  2692. 'meta' => array(
  2693. 'title' => 'Load template from TagDiv Cloud Library',
  2694. 'class' => $template_type
  2695. ),
  2696. )
  2697. );
  2698. }
  2699. }
  2700.  
  2701. function td_show_cpt_template_menu($template_type) {
  2702.  
  2703. global $wp_admin_bar;
  2704.  
  2705. $global_title = '';
  2706. $is_tdb_template = false;
  2707.  
  2708. $lang = '';
  2709. if ( class_exists('SitePress', false ) ) {
  2710. global $sitepress;
  2711. $sitepress_settings = $sitepress->get_settings();
  2712. if ( isset( $sitepress_settings['custom_posts_sync_option'][ 'tdb_templates'] ) ) {
  2713. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2714. if ( 1 === $translation_mode ) {
  2715. $lang = $sitepress->get_current_language();
  2716. }
  2717. }
  2718. }
  2719.  
  2720. $td_cpt = td_util::get_option('td_cpt');
  2721. $option_id = 'td_default_site_post_template' . $lang;
  2722.  
  2723. $cpts = td_util::get_cpts();
  2724.  
  2725. foreach ($cpts as $cpt) {
  2726. if ($template_type === $cpt->name && !empty($td_cpt[$cpt->name][$option_id])) {
  2727. $default_template_id = $td_cpt[$cpt->name][$option_id];
  2728.  
  2729. if ( td_global::is_tdb_template( $default_template_id, true ) ) {
  2730. $template_id = td_global::tdb_get_template_id( $default_template_id );
  2731. $is_tdb_template = true;
  2732. }
  2733. }
  2734. }
  2735.  
  2736. if (empty($template_id)) {
  2737. $template_name = strtoupper($template_type) . ' Template Default';
  2738. } else {
  2739. if ( $is_tdb_template ) {
  2740. $template_name = get_the_title( $template_id );
  2741. } else {
  2742. $template_name = strtoupper($template_type) . ' Template ' . $template_id;
  2743. }
  2744. }
  2745.  
  2746. $current_template = $global_title . $template_name;
  2747.  
  2748. if ( !empty($current_template)) {
  2749.  
  2750. $menu_template_id = '';
  2751. if ($is_tdb_template) {
  2752. $menu_template_id = $template_id;
  2753. }
  2754.  
  2755. $wp_admin_bar->add_menu(
  2756. array(
  2757. 'id' => 'tdb_template_cpt',
  2758. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2759. 'href' => '#',
  2760. 'meta' => array(
  2761. 'title' => 'Current ' . strtoupper( $template_type ) . ' Template',
  2762. 'class' => 'templateid-' . $template_type
  2763. ),
  2764. )
  2765. );
  2766.  
  2767. $wp_admin_bar->add_menu(
  2768. array(
  2769. 'id' => 'tdb_template_load',
  2770. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2771. 'href' => '#',
  2772. 'meta' => array(
  2773. 'title' => 'Load template from TagDiv Cloud Library',
  2774. 'class' => 'cpt'
  2775. ),
  2776. )
  2777. );
  2778. }
  2779. }
  2780.  
  2781. function td_show_cpt_tax_template_menu($term) {
  2782.  
  2783. global $wp_admin_bar;
  2784.  
  2785. $global_title = '';
  2786. $is_tdb_template = false;
  2787.  
  2788. $lang = '';
  2789. if ( class_exists('SitePress', false ) ) {
  2790. global $sitepress;
  2791. $sitepress_settings = $sitepress->get_settings();
  2792. if ( isset( $sitepress_settings['custom_posts_sync_option'][ 'tdb_templates'] ) ) {
  2793. $translation_mode = (int)$sitepress_settings['custom_posts_sync_option']['tdb_templates'];
  2794. if ( 1 === $translation_mode ) {
  2795. $lang = $sitepress->get_current_language();
  2796. }
  2797. }
  2798. }
  2799.  
  2800. $td_cpt_tax = td_util::get_option('td_cpt_tax');
  2801. $option_id = 'tdb_category_template' . $lang;
  2802.  
  2803. $ctaxes = td_util::get_ctaxes();
  2804.  
  2805. foreach ($ctaxes as $ctax) {
  2806.  
  2807. if ($ctax->name === $term->taxonomy && !empty($td_cpt_tax[$ctax->name][$option_id])) {
  2808. $default_template_id = $td_cpt_tax[$ctax->name][$option_id];
  2809.  
  2810. if ( td_global::is_tdb_template( $default_template_id, true ) ) {
  2811. $template_id = td_global::tdb_get_template_id( $default_template_id );
  2812. $is_tdb_template = true;
  2813. }
  2814. }
  2815. }
  2816.  
  2817. if (empty($template_id)) {
  2818. $template_name = strtoupper($term->taxonomy) . ' Template Default';
  2819. } else {
  2820. if ( $is_tdb_template ) {
  2821. $template_name = get_the_title( $template_id );
  2822. } else {
  2823. $template_name = strtoupper($term->taxonomy) . ' Template ' . $template_id;
  2824. }
  2825. }
  2826.  
  2827. $current_template = $global_title . $template_name;
  2828.  
  2829. if ( !empty($current_template)) {
  2830.  
  2831. $menu_template_id = '';
  2832. if ($is_tdb_template) {
  2833. $menu_template_id = $template_id;
  2834. }
  2835.  
  2836. $wp_admin_bar->add_menu(
  2837. array(
  2838. 'id' => 'tdb_template_cpt_tax',
  2839. 'title' => $current_template . '<span class="td-mobile-title">Template</span>',
  2840. 'href' => '#',
  2841. 'meta' => array(
  2842. 'title' => 'Current ' . strtoupper( $term->taxonomy ) . ' Template',
  2843. 'class' => 'templateid-' . $term->taxonomy
  2844. ),
  2845. )
  2846. );
  2847.  
  2848. $wp_admin_bar->add_menu(
  2849. array(
  2850. 'id' => 'tdb_template_load',
  2851. 'title' => 'Cloud Library<span class="td-mobile-title">Cloud</span>',
  2852. 'href' => '#',
  2853. 'meta' => array(
  2854. 'title' => 'Load template from TagDiv Cloud Library',
  2855. 'class' => 'cpt_tax'
  2856. ),
  2857. )
  2858. );
  2859. }
  2860. }
  2861.  
  2862. // add the load template button on the welcome screen of td-composer
  2863. add_action('tdc_welcome_panel_text', function() {
  2864. if (tdc_util::get_get_val('tdbTemplateType') !== false) {
  2865. ?>
  2866. <div class="tdc-start-tips">
  2867. <p>OR</p>
  2868. </div>
  2869. <div class="tdc-sidebar-w-button tdb-load-template" title="Import Pre-designed Templates from tagDiv Cloud Library">Load Template</div>
  2870. <?php
  2871. }
  2872. });
  2873.  
  2874.  
  2875.  
  2876. add_action('admin_footer', 'tdb_on_wp_admin_footer');
  2877. function tdb_on_wp_admin_footer () {
  2878. include_once( plugin_dir_path(__FILE__) . 'tdb_template_import.php');
  2879. }
  2880.  
  2881. if (is_user_logged_in()) {
  2882. add_action( 'wp_footer', 'tdb_on_wp_admin_footer1' );
  2883. function tdb_on_wp_admin_footer1() {
  2884. require_once( plugin_dir_path(__FILE__) . 'tdb_template_import.php' );
  2885. }
  2886. }
  2887.  
  2888.  
  2889. if (!empty($_GET['tdbTemplateType'])) {
  2890. $tdbTemplateType = @$_GET['tdbTemplateType'];
  2891. }
  2892. if (!empty($_GET['post_type'])) {
  2893. $post_type = @$_GET['post_type'];
  2894. }
  2895.  
  2896. if ( ! empty( $tdbTemplateType ) ||
  2897. ( ! empty( $post_type ) && ( 'tdb_templates' === $post_type || 'page' === $post_type ) ) ||
  2898. ( isset( $_GET['page'] ) && ( $_GET['page'] === 'td_theme_welcome' || $_GET['page'] === 'tdb_cloud_templates' || $_GET['page'] === 'td_theme_demos' || $_GET['page'] === 'td_theme_updates' || $_GET['page'] === 'td_licence_panel' ) ) ) {
  2899.  
  2900. // enqueue for wp-admin
  2901. add_action( 'admin_enqueue_scripts', function () {
  2902.  
  2903. // load the css
  2904. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2905. wp_enqueue_style( 'tdb_wp_admin', TDB_URL . '/td_less_style.css.php?part=wp_admin_main', false, TD_CLOUD_LIBRARY );
  2906. } else {
  2907. wp_enqueue_style( 'tdb_wp_admin', TDB_URL . '/assets/css/tdb_wp_admin.css', false, TD_CLOUD_LIBRARY );
  2908. }
  2909.  
  2910. // load the vue modal js
  2911. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2912. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_modals, array( 'jquery', 'underscore' ) );
  2913. } else {
  2914. wp_enqueue_script( 'tdb_js_files_vue_modals', TDB_URL . '/assets/js/js_files_vue_modals.min.js', array(
  2915. 'jquery',
  2916. 'underscore'
  2917. ), TD_CLOUD_LIBRARY, true );
  2918. }
  2919.  
  2920. }, 1011 ); // load them last after td-composer
  2921.  
  2922. //enqueue files that must go at the end
  2923. add_action( 'admin_enqueue_scripts', function () {
  2924.  
  2925. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2926. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_modals_last, array( 'jquery', 'underscore' ) );
  2927. } else {
  2928. wp_enqueue_script( 'js_files_vue_modals_last', TDB_URL . '/assets/js/js_files_vue_modals_last.min.js', array(
  2929. 'jquery',
  2930. 'underscore'
  2931. ), TD_CLOUD_LIBRARY, true );
  2932. }
  2933.  
  2934. }, 1012 );
  2935. }
  2936.  
  2937.  
  2938.  
  2939. if (is_user_logged_in()) {
  2940.  
  2941. // enqueue for wp-admin
  2942. add_action( 'wp_enqueue_scripts', function () {
  2943.  
  2944. // load the css
  2945. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2946. wp_enqueue_style( 'tdb_wp_admin', TDB_URL . '/td_less_style.css.php?part=wp_admin_main', false, TD_CLOUD_LIBRARY );
  2947. } else {
  2948. wp_enqueue_style( 'tdb_wp_admin', TDB_URL . '/assets/css/tdb_wp_admin.css', false, TD_CLOUD_LIBRARY );
  2949. }
  2950.  
  2951. // load the vue modal js
  2952. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2953. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_modals, array( 'jquery', 'underscore' ) );
  2954. } else {
  2955. wp_enqueue_script( 'tdb_js_files_vue_modals', TDB_URL . '/assets/js/js_files_vue_modals.min.js', array(
  2956. 'jquery',
  2957. 'underscore'
  2958. ), TD_CLOUD_LIBRARY, true );
  2959. }
  2960.  
  2961. }, 11 ); // load them last after td-composer
  2962.  
  2963. //enqueue files that must go at the end
  2964. add_action( 'wp_enqueue_scripts', function () {
  2965.  
  2966. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2967. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_modals_last, array( 'jquery', 'underscore' ) );
  2968. } else {
  2969. wp_enqueue_script( 'js_files_vue_modals_last', TDB_URL . '/assets/js/js_files_vue_modals_last.min.js', array(
  2970. 'jquery',
  2971. 'underscore'
  2972. ), TD_CLOUD_LIBRARY, true );
  2973. }
  2974.  
  2975. }, 12 );
  2976. }
  2977.  
  2978.  
  2979. // enqueue files that must go at the end
  2980. add_action( 'admin_enqueue_scripts', function () {
  2981.  
  2982. // load cloud templates vue js/css
  2983. if ( isset( $_GET['page'] ) && $_GET['page'] === 'tdb_cloud_templates' ) {
  2984.  
  2985. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2986. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_cloud_templates, array( 'jquery', 'underscore' ) );
  2987. wp_enqueue_style( 'tdb_style_cloud_templates', TDB_URL . '/td_less_style.css.php?part=wp_admin_cloud_templates', false, TD_CLOUD_LIBRARY );
  2988. } else {
  2989. wp_enqueue_script( 'tdb_js_files_vue_cloud_templates', TDB_URL . '/assets/js/js_files_vue_cloud_templates.min.js', array( 'jquery', 'underscore' ), TD_CLOUD_LIBRARY, true );
  2990. wp_enqueue_style( 'tdb_style_cloud_templates', TDB_URL . '/assets/css/tdb_wp_admin_cloud_templates.css', false, TD_CLOUD_LIBRARY );
  2991. }
  2992.  
  2993. }
  2994.  
  2995. // wp admin js files
  2996. if ( TDB_DEPLOY_MODE == 'dev' ) {
  2997. tdb_util::enqueue_js_files_array( tdb_config::$js_files_wp_admin, array( 'jquery', 'underscore' ) );
  2998. } else {
  2999. if ( !empty( $_GET['action'] ) && 'update-selected-themes' === @$_GET['action'] ) {
  3000. return;
  3001. }
  3002. wp_enqueue_script( 'tdb_js_files_for_wp_admin', TDB_URL . '/assets/js/js_files_wp_admin.min.js', array(
  3003. 'jquery',
  3004. 'underscore'
  3005. ), TD_CLOUD_LIBRARY, true );
  3006. }
  3007.  
  3008. // Vue files theme panel
  3009. if ( isset( $_GET['page'] ) && $_GET['page'] === 'td_theme_panel' ) {
  3010. if ( TDB_DEPLOY_MODE == 'dev' ) {
  3011. tdb_util::enqueue_js_files_array( tdb_config::$js_files_vue_theme_panel, array( 'jquery', 'underscore' ) );
  3012. } else {
  3013. wp_enqueue_script( 'tdb_js_files_vue_theme_panel', TDB_URL . '/assets/js/js_files_vue_theme_panel.min.js', array(
  3014. 'jquery',
  3015. 'underscore'
  3016. ), TD_CLOUD_LIBRARY, true );
  3017. }
  3018. }
  3019.  
  3020. }, 1012 );
  3021.  
  3022.  
  3023. // enqueue for front
  3024. add_action( 'wp_enqueue_scripts', function () {
  3025.  
  3026. if ( td_util::is_mobile_theme() ) {
  3027. return;
  3028. }
  3029.  
  3030. // load the css
  3031. if ( TDB_DEPLOY_MODE == 'dev' ) {
  3032. wp_enqueue_style( 'tdb_style_cloud_templates_front', TDB_URL . '/td_less_style.css.php?part=tdb_main', false, TD_CLOUD_LIBRARY );
  3033. } else {
  3034. wp_enqueue_style( 'tdb_style_cloud_templates_front', TDB_URL . '/assets/css/tdb_main.css', false, TD_CLOUD_LIBRARY );
  3035. }
  3036.  
  3037. // load the js
  3038. if ( TDB_DEPLOY_MODE == 'dev' ) {
  3039. tdb_util::enqueue_js_files_array( tdb_config::$js_files_for_front, array( 'jquery' ) );
  3040. } else {
  3041. wp_enqueue_script( 'tdb_js_files_for_front', TDB_URL . '/assets/js/js_files_for_front.min.js', array( 'jquery' ), TD_CLOUD_LIBRARY, true );
  3042. }
  3043.  
  3044. $gm_api_key = td_util::get_gm_api_key();
  3045. if( $gm_api_key != '' ) {
  3046. wp_enqueue_script( 'tdb_js_google_maps_api', 'https://maps.googleapis.com/maps/api/js?key=' . $gm_api_key . '&libraries=places', array( 'jquery' ), TD_CLOUD_LIBRARY, true );
  3047. }
  3048.  
  3049. }, 1011 ); // load them last after td-composer
  3050.  
  3051.  
  3052. /**
  3053. * Patch the theme panel and metaboxes with our builder templates.
  3054. * Here we add the templates to the API so that we can see them in the panels
  3055. */
  3056. function tdb_patch_panel() {
  3057. if (is_admin()) {
  3058.  
  3059. /**
  3060. * patch single templates
  3061. */
  3062. $args = array(
  3063. 'post_type' => array('tdb_templates'),
  3064. 'meta_query' => array(
  3065. array(
  3066. 'key' => 'tdb_template_type',
  3067. 'value' => 'single',
  3068. ),
  3069. ),
  3070. 'posts_per_page' => '-1'
  3071. );
  3072.  
  3073. if (class_exists('SitePress', false )) {
  3074.  
  3075. // WPML bug - it doesn't use all the time 'suppress_filters' param (@see WPML forums)
  3076. $args['suppress_filters'] = false;
  3077. }
  3078.  
  3079. /**
  3080. * @var WP_Query
  3081. */
  3082. $wp_query_templates = new WP_Query( $args );
  3083.  
  3084. if (!empty($wp_query_templates->posts)) {
  3085.  
  3086. /**
  3087. * @var $post WP_Post
  3088. */
  3089. foreach ($wp_query_templates->posts as $post) {
  3090.  
  3091. if ( class_exists('SitePress', false )) {
  3092. global $sitepress;
  3093. $check = wpml_get_language_information( null, $post->ID );
  3094.  
  3095. if ( ! is_wp_error( $check )) {
  3096. $post_language_code = $check['language_code'];
  3097.  
  3098. if ( $post_language_code != $sitepress->get_current_language() ) {
  3099. continue;
  3100. }
  3101. }
  3102. }
  3103.  
  3104. // tdb_ is used as a prefix to filter it out in theme panel and show it only on post settings
  3105. // why? we have to use the prefix to identify templates even when this plugin is off to load the default theme template in that case
  3106. td_api_single_template::add('tdb_template_' . $post->ID,
  3107. array(
  3108. 'file' => '',
  3109. 'text' => $post->post_title,
  3110. 'img' => TDB_URL . '/assets/images/single_template_placeholder.png',
  3111. 'template_id' => $post->ID, // this key is used only on custom templates
  3112. 'show_featured_image_on_all_pages' => false,
  3113. 'bg_disable_background' => false, // disable the featured image
  3114. 'bg_box_layout_config' => 'auto', // auto | td-boxed-layout | td-full-layout
  3115. 'bg_use_featured_image_as_background' => false, // uses the featured image as a background
  3116. 'exclude_ad_content_top' => false,
  3117. 'is_mobile_template' => get_post_meta( $post->ID, 'tdc_is_mobile_template', true)
  3118. )
  3119. );
  3120. }
  3121. }
  3122. }
  3123. }
  3124. tdb_patch_panel();
  3125.  
  3126.  
  3127. /**
  3128. * remove comment form nonce on composer frame
  3129. * fix for console error on single post comments shortcode addition
  3130. */
  3131. add_action( 'comment_form', function() {
  3132. if ( tdc_state::is_live_editor_iframe() || tdc_state::is_live_editor_ajax() ) {
  3133. remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' ); ;
  3134. }
  3135. }, 9 );
  3136.  
  3137. if ( is_admin() ){
  3138. add_filter( 'query_vars', function( $query_vars ) {
  3139.  
  3140. $query_vars[] = 'meta_key';
  3141. $query_vars[] = 'meta_value';
  3142. $query_vars[] = 'template_type';
  3143. $query_vars[] = 'is_mobile_template';
  3144.  
  3145. return $query_vars;
  3146. });
  3147. }
  3148.  
  3149. /**
  3150. * this removes the wordpress 'hentry' class
  3151. * we're quiting the 'hentry' microformat and rely on the json-ld(json linked data) format
  3152. * @param $classes
  3153. * @return array
  3154. */
  3155. add_filter( 'post_class',function( $classes ){
  3156.  
  3157. $classes = array_diff( $classes, array( 'hentry' ) );
  3158.  
  3159. return $classes;
  3160. });
  3161.  
  3162.  
  3163. if ( td_global::$is_woocommerce_installed && !defined('TD_WOO') ) {
  3164. add_filter('woocommerce_add_to_cart_fragments', 'tdb_woo_get_updated_menu_cart');
  3165. function tdb_woo_get_updated_menu_cart( $fragments ) {
  3166.  
  3167. global $woocommerce;
  3168. $cart_count = $woocommerce->cart->cart_contents_count;
  3169. $cart_subtotal = $woocommerce->cart->subtotal;
  3170. if( $cart_subtotal == 0 ) {
  3171. $cart_subtotal = '0,00 ' . get_woocommerce_currency();
  3172. } else {
  3173. $cart_subtotal .= ' ' . get_woocommerce_currency();
  3174. }
  3175.  
  3176. ob_start();
  3177. woocommerce_mini_cart();
  3178. $cart_contents = ob_get_clean();
  3179.  
  3180.  
  3181. $fragments['.tdb-wmc-count'] = '<span class="tdb-wmc-count">' . $cart_count . '</span>';
  3182. $fragments['.tdb-wmc-txt'] = '<span class="tdb-wmc-txt">' . $cart_subtotal . '</span>';
  3183. $fragments['.tdb-wmc-widget-inner'] = '<div class="tdb-wmc-widget-inner">' . $cart_contents . '</span>';
  3184.  
  3185. return $fragments;
  3186.  
  3187. }
  3188. }
  3189.  
  3190. // add taxonomies tdb term meta support
  3191. add_filter( 'admin_init', function () {
  3192.  
  3193. $fields = array(
  3194. array(
  3195. 'label' => 'Color',
  3196. 'desc' => 'Choose a color',
  3197. 'id' => 'tdb_filter_color',
  3198. 'type' => 'color'
  3199. ),
  3200. //array(
  3201. // 'label' => 'Image',
  3202. // 'desc' => 'You may choose an image for multicolored use cases',
  3203. // 'id' => 'tdb_filter_color_image',
  3204. // 'type' => 'image'
  3205. //),
  3206. array(
  3207. 'label' => 'Image',
  3208. 'desc' => 'Choose an Image',
  3209. 'id' => 'tdb_filter_image',
  3210. 'type' => 'image'
  3211. )
  3212. );
  3213.  
  3214. $cpts = td_util::get_cpts();
  3215.  
  3216. $taxonomies = array();
  3217. foreach ( $cpts as $cpt ) {
  3218.  
  3219. // ignore builtin post types
  3220. if ( $cpt->_builtin )
  3221. continue;
  3222.  
  3223. // get post type taxonomies
  3224. $cpt_taxonomies = get_object_taxonomies( $cpt->name );
  3225.  
  3226. // set ctp taxonomies
  3227. if ( $cpt_taxonomies ) {
  3228. $taxonomies[$cpt->name] = $cpt_taxonomies;
  3229. }
  3230.  
  3231. }
  3232.  
  3233. foreach ( $taxonomies as $post_type => $post_type_taxonomies ) {
  3234.  
  3235. foreach ( $post_type_taxonomies as $taxonomy )
  3236. new tdb_term_meta( $taxonomy, $post_type, $fields );
  3237.  
  3238. }
  3239.  
  3240. });
  3241.  
  3242. /**
  3243. * Class tdb_method - fake callable for auto complete
  3244. */
  3245. class tdb_method {
  3246. /**
  3247. * @param string $p1
  3248. * @param string $p2
  3249. * @param string $p3
  3250. * @param string $p4
  3251. * @return array | string
  3252. */
  3253. function __invoke($p1 = '', $p2 = '', $p3 = '', $p4 = '') {
  3254. return '';
  3255. // TODO: Implement __invoke() method.
  3256. }
  3257. }
  3258.  
Advertisement
Add Comment
Please, Sign In to add comment