Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsMain::is_autosave()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsMain
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Abstraction for the hook classes
12
 *
13
 * @package Msls
14
 */
15
class MslsMain {
16
17
	/**
18
	 * @var MslsOptions
19
	 */
20
	protected $options;
21
22
	/**
23
	 * @var MslsBlogCollection
24
	 */
25
	protected $collection;
26
27
	/**
28
	 * @param MslsOptions $options
29
	 * @param MslsBlogCollection $collection
30
	 */
31
	public function __construct( MslsOptions $options, MslsBlogCollection $collection ) {
32
		$this->options    = $options;
33
		$this->collection = $collection;
34
	}
35
36
	/**
37
	 * Factory
38
	 *
39
	 * @codeCoverageIgnore
40
	 *
41
	 * @return MslsMain
42
	 */
43
	public static function init() {
44
		$options    = MslsOptions::instance();
45
		$collection = MslsBlogCollection::instance();
46
47
		return new static( $options, $collection );
48
	}
49
50
	/**
51
	 * Prints a message in the error log if WP_DEBUG is true
52
	 *
53
	 * @param mixed $message
54
	 */
55
	public function debugger( $message ) {
56
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
0 ignored issues
show
introduced by
Found "=== true". Use Yoda Condition checks, you must
Loading history...
57
			if ( is_array( $message ) || is_object( $message ) ) {
58
				$message = print_r( $message, true );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
59
			}
60
			error_log( 'MSLS Debug: ' . $message );
61
		}
62
	}
63
64
	/**
65
	 * Get the input array
66
	 * @param int $object_id
67
	 * @return array
68
	 */
69
	public function get_input_array( $object_id ) {
70
		$arr = array();
71
72
		$current_blog = $this->collection->get_current_blog();
73
		if ( ! is_null( $current_blog ) ) {
74
			$arr[ $current_blog->get_language() ] = (int) $object_id;
75
		}
76
77
		$input_post = filter_input_array( INPUT_POST );
78
		if ( is_array( $input_post ) ) {
79
			foreach ( $input_post as $key => $value ) {
80
				if ( false !== strpos( $key, 'msls_input_' ) && ! empty( $value ) ) {
81
					$arr[ substr( $key, 11 ) ] = (int) $value;
82
				}
83
			}
84
		}
85
86
		return $arr;
87
	}
88
89
	/**
90
	 * Checks if the current input comes from the autosave-functionality
91
	 * @param int $post_id
92
	 * @return bool
93
	 */
94
	public function is_autosave( $post_id ) {
95
		return( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id );
96
	}
97
98
	/**
99
	 * Checks for the nonce in the INPUT_POST
100
	 * @return boolean
101
	 */
102
	public function verify_nonce() {
103
		return(
104
			filter_has_var( INPUT_POST, 'msls_noncename' ) &&
105
			wp_verify_nonce( filter_input( INPUT_POST, 'msls_noncename' ), MSLS_PLUGIN_PATH )
106
		);
107
	}
108
109
	/**
110
	 * Delete
111
	 * @param int $object_id
112
	 * @codeCoverageIgnore
113
	 */
114
	public function delete( $object_id ) {
115
		$this->save( $object_id, MslsOptionsPost::class );
116
	}
117
118
	/**
119
	 * Save
120
	 * @param int $object_id
121
	 * @param string $class
122
	 * @codeCoverageIgnore
123
	 */
124
	protected function save( $object_id, $class ) {
125
		if ( has_action( 'msls_main_save' ) ) {
126
			/**
127
			 * Calls completely customized save-routine
128
			 * @since 0.9.9
129
			 * @param int $object_id
130
			 * @param string Classname
131
			 */
132
			do_action( 'msls_main_save', $object_id, $class );
133
			return;
134
		}
135
136
		if ( ! $this->collection->has_current_blog() ) {
137
			$this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' );
138
			return;
139
		}
140
141
		$language = $this->collection->get_current_blog()->get_language();
142
		$msla     = new MslsLanguageArray( $this->get_input_array( $object_id ) );
143
		$options  = new $class( $object_id );
144
		$temp     = $options->get_arr();
145
146
		if ( 0 != $msla->get_val( $language ) ) {
147
			$options->save( $msla->get_arr( $language ) );
148
		}
149
		else {
150
			$options->delete();
151
		}
152
153
		foreach ( $this->collection->get() as $blog ) {
154
			switch_to_blog( $blog->userblog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
155
156
			$language = $blog->get_language();
157
			$larr_id  = $msla->get_val( $language );
158
159
			if ( 0 != $larr_id ) {
160
				$options = new $class( $larr_id );
161
				$options->save( $msla->get_arr( $language ) );
162
			}
163
			elseif ( isset( $temp[ $language ] ) ) {
164
				$options = new $class( $temp[ $language ] );
165
				$options->delete();
166
			}
167
168
			restore_current_blog();
169
		}
170
	}
171
172
}
173