Completed
Push — master ( 39734d...9bfb60 )
by Zack
05:51 queued 02:37
created

GravityView_Theme_Hooks_WPML   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add_hooks() 0 8 1
A filter_gravityview_back_link() 0 7 1
A remove_url_hooks() 0 11 2
A add_url_hooks() 0 11 2
B wpml_ls_filter() 0 36 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 173.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
No space before closing parenthesis of array is bad style
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
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class 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 global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class 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
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
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 global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class 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
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
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 global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class 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;