class-fl-builder-wpml.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Logic to support various parts of WPML.
  4. *
  5. * @since 2.1
  6. */
  7. final class FLBuilderWPML {
  8. /**
  9. * @since 2.1
  10. * @return void
  11. */
  12. static public function init() {
  13. if ( ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
  14. return;
  15. }
  16. add_filter( 'fl_get_wp_widgets_exclude', __CLASS__ . '::filter_wp_widgets_exclude' );
  17. add_filter( 'fl_builder_node_template_post_id', __CLASS__ . '::filter_node_template_post_id' );
  18. add_filter( 'fl_builder_parent_template_node_id', __CLASS__ . '::filter_parent_template_node_id', 10, 3 );
  19. }
  20. /**
  21. * Filter out the language switcher from the BB content panel
  22. * as it must be added to a sidebar to work.
  23. *
  24. * @since 2.1
  25. * @param array $exclude
  26. * @return array
  27. */
  28. static public function filter_wp_widgets_exclude( $exclude ) {
  29. $exclude[] = 'WPML_LS_Widget';
  30. return $exclude;
  31. }
  32. /**
  33. * Returns the translated post ID for a node template. This makes
  34. * it so the translated version of a global node will render.
  35. *
  36. * @since 2.1
  37. * @param int $post_id
  38. * @return int
  39. */
  40. static public function filter_node_template_post_id( $post_id ) {
  41. global $sitepress;
  42. $post_type = get_post_type( $post_id );
  43. $lang = $sitepress->get_current_language();
  44. $wpml_post = new WPML_Post_Element( $post_id, $sitepress );
  45. $trid = $sitepress->get_element_trid( $post_id, "post_$post_type" );
  46. $translations = $sitepress->get_element_translations( $trid, "post_$post_type" );
  47. if ( is_array( $translations ) && isset( $translations[ $lang ] ) ) {
  48. $post_id = $translations[ $lang ]->element_id;
  49. }
  50. return $post_id;
  51. }
  52. /**
  53. * Returns the translated root node ID for a node template. This makes
  54. * it so the translated version of a global node will render.
  55. *
  56. * @since 2.1.3
  57. * @param string template_node_id
  58. * @param object $parent
  59. * @param array $layout_data
  60. * @return string
  61. */
  62. static public function filter_parent_template_node_id( $template_node_id, $parent, $layout_data ) {
  63. $root = FLBuilderModel::get_node_template_root( $parent->type, $layout_data );
  64. if ( $root && isset( $root->template_root_node ) && isset( $root->template_node_id ) && ! empty( $root->template_node_id ) ) {
  65. $template_node_id = $root->template_node_id;
  66. }
  67. return $template_node_id;
  68. }
  69. }
  70. FLBuilderWPML::init();