gravityview /
GravityView
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Add WPML compatibility to GravityView, including registering scripts and styles to GravityView no-conflict list |
||
| 4 | * |
||
| 5 | * @file class-gravityview-theme-hooks-wpml.php |
||
| 6 | * @package GravityView |
||
| 7 | * @license GPL2+ |
||
| 8 | * @author Katz Web Services, Inc. |
||
| 9 | * @link http://gravityview.co |
||
| 10 | * @copyright Copyright 2015, Katz Web Services, Inc. |
||
| 11 | * |
||
| 12 | * @since 1.19.2 |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @inheritDoc |
||
| 17 | */ |
||
| 18 | class GravityView_Theme_Hooks_WPML extends GravityView_Plugin_and_Theme_Hooks { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @inheritDoc |
||
| 22 | * @since 1.19.2 |
||
| 23 | */ |
||
| 24 | protected $script_handles = array( |
||
| 25 | 'wpml-cpi-scripts', |
||
| 26 | 'sitepress-scripts', |
||
| 27 | 'sitepress-post-edit', |
||
| 28 | 'sitepress-post-list-quickedit', |
||
| 29 | 'sitepress-languages', |
||
| 30 | 'sitepress-troubleshooting', |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @inheritDoc |
||
| 35 | * @since 1.19.2 |
||
| 36 | */ |
||
| 37 | protected $style_handles = array( |
||
| 38 | 'wpml-select-2', |
||
| 39 | 'wpml-tm-styles', |
||
| 40 | 'wpml-tm-queue', |
||
| 41 | 'wpml-dialog', |
||
| 42 | 'wpml-tm-editor-css', |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritDoc |
||
| 47 | * @since 1.19.2 |
||
| 48 | */ |
||
| 49 | protected $constant_name = 'ICL_SITEPRESS_VERSION'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Add filters for WPML links |
||
| 53 | * |
||
| 54 | * @since 1.19.4 |
||
| 55 | */ |
||
| 56 | public function add_hooks() { |
||
| 57 | |||
| 58 | parent::add_hooks(); |
||
| 59 | |||
| 60 | add_filter( 'icl_ls_languages', array( $this, 'wpml_ls_filter' ) ); |
||
| 61 | |||
| 62 | add_filter( 'gravityview_directory_link', array( $this, 'filter_gravityview_back_link') ); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Add WPML filters to GravityView directory link |
||
| 67 | * |
||
| 68 | * This also modifies all the Edit Entry, Cancel Edit, Go Back links |
||
| 69 | * |
||
| 70 | * @since 1.19.4 |
||
| 71 | * |
||
| 72 | * @see GravityView_API::directory_link |
||
| 73 | * @uses WPML_URL_Filters::permalink_filter |
||
| 74 | * |
||
| 75 | * @param string $link Permalink to the GravityView directory, without language params |
||
| 76 | * |
||
| 77 | * @return string $link, with language params added by WPML |
||
| 78 | */ |
||
| 79 | function filter_gravityview_back_link( $link ) { |
||
|
0 ignored issues
–
show
|
|||
| 80 | global $wpml_url_filters; |
||
|
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
Loading history...
|
|||
| 81 | |||
| 82 | $link = $wpml_url_filters->permalink_filter( $link, GravityView_frontend::getInstance()->getPostId() ); |
||
| 83 | |||
| 84 | return $link; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Remove WPML permalink filters |
||
| 89 | * |
||
| 90 | * @since 1.19.4 |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | private function remove_url_hooks() { |
||
| 95 | global $wpml_url_filters; |
||
|
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
Loading history...
|
|||
| 96 | |||
| 97 | $wpml_url_filters->remove_global_hooks(); |
||
| 98 | |||
| 99 | if ( $wpml_url_filters->frontend_uses_root() === true ) { |
||
|
0 ignored issues
–
show
|
|||
| 100 | remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1 ); |
||
| 101 | } else { |
||
| 102 | remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1 ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Add the WPML permalink filters back in |
||
| 108 | * |
||
| 109 | * @since 1.19.4 |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | private function add_url_hooks() { |
||
| 114 | global $wpml_url_filters; |
||
|
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
Loading history...
|
|||
| 115 | |||
| 116 | $wpml_url_filters->add_global_hooks(); |
||
| 117 | |||
| 118 | if ( $wpml_url_filters->frontend_uses_root() === true ) { |
||
|
0 ignored issues
–
show
|
|||
| 119 | add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1, 2 ); |
||
| 120 | } else { |
||
| 121 | add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1, 2 ); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Modify the language links to fix /entry/ var from being stripped |
||
| 127 | * |
||
| 128 | * @since 1.19.4 |
||
| 129 | * |
||
| 130 | * @param array $languages Array of active languages with their details |
||
| 131 | * |
||
| 132 | * @return array If currently a single entry screen, re-generate URL after removing WPML filters |
||
| 133 | */ |
||
| 134 | public function wpml_ls_filter( $languages ) { |
||
| 135 | global $sitepress, $post; |
||
|
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
Use of
global functionality is not recommended; it makes your code harder to test, and less reusable.
Instead of relying on 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass MyClass {
private $a;
private $b;
public function __construct($a, $b) {
$this->a = $a;
$this->b = $b;
}
public function myFunction() {
// Do something
}
}
Loading history...
|
|||
| 136 | |||
| 137 | if ( $entry_slug = GravityView_frontend::getInstance()->getSingleEntry() ) { |
||
| 138 | |||
| 139 | $trid = $sitepress->get_element_trid( $post->ID ); |
||
| 140 | $translations = $sitepress->get_element_translations( $trid ); |
||
| 141 | |||
| 142 | $this->remove_url_hooks(); |
||
| 143 | |||
| 144 | if( $translations ) { |
||
| 145 | foreach ( $languages as $lang_code => $language ) { |
||
| 146 | |||
| 147 | $lang_post_id = $translations[ $lang_code ]->element_id; |
||
| 148 | |||
| 149 | $entry_link = GravityView_API::entry_link( $entry_slug, $lang_post_id ); |
||
| 150 | |||
| 151 | if ( ! empty( $translations[ $lang_code ]->original ) ) { |
||
| 152 | |||
| 153 | // The original doesn't need a language parameter |
||
| 154 | $languages[ $lang_code ]['url'] = remove_query_arg( 'lang', $entry_link ); |
||
| 155 | |||
| 156 | } elseif ( $entry_link ) { |
||
| 157 | |||
| 158 | // Every other language does |
||
| 159 | $languages[ $lang_code ]['url'] = add_query_arg( array( 'lang' => $lang_code ), $entry_link ); |
||
| 160 | |||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->add_url_hooks(); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $languages; |
||
| 169 | } |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | new GravityView_Theme_Hooks_WPML; |