Completed
Push — master ( 47a4bc...b3d566 )
by
unknown
03:07
created

save::save_to_post_disables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Main class responsible for saving the post object
4
 *
5
 * @since 1.0
6
 */
7
namespace lasso\process;
8
9
use lasso\internal_api\api_action;
10
11
class save implements api_action {
12
	
13
	/**
14
	 * Process the post save
15
	 *
16
	 * @since 0.9.2
17
	 *
18
	 * @param array $data Sanitized data to use for saving.
19
	 *
20
	 * @return bool Always returns true.
21
	 */
22 View Code Duplication
	public function content( $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
24
		$save_to_post_disabled = $this->save_to_post_disables();
25
26
		$postid = (int) $data[ 'post_id' ];
27
		$content = $this->replace_rendered_shortcodes( $data[ 'content' ] );
28
29
		if ( 'off' == $save_to_post_disabled || empty( $save_to_post_disabled ) ) {
30
31
			$args = array(
32
				'ID'           => (int) $postid,
33
				'post_content' => $content
34
			);
35
36
			wp_update_post( apply_filters( 'lasso_object_save_args', $args ) );
37
38
		}
39
40
		// run save action
41
		do_action( 'lasso_post_saved', $postid, $content, get_current_user_ID() );
42
43
		return true;
44
45
	}
46
47
	/**
48
	 * Process the post save
49
	 *
50
	 * @since 0.9.2
51
	 *
52
	 * @param array $data Sanitized data to use for saving.
53
	 *
54
	 * @return bool Always returns true.
55
	 */
56 View Code Duplication
	public function publish_content( $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
		$save_to_post_disabled = $this->save_to_post_disables();
58
59
		$postid = (int) $data[ 'post_id' ];
60
		$content = $this->replace_rendered_shortcodes( $data[ 'content' ] );
61
62
		if ( 'off' == $save_to_post_disabled || empty( $save_to_post_disabled ) ) {
63
64
			$args = array (
65
				'ID'           	=> $postid,
66
				'post_content' 	=> $content,
67
				'post_status' 	=> 'publish'
68
			);
69
			wp_update_post( apply_filters( 'lasso_object_publish_args', $args ) );
70
71
		}
72
73
		do_action( 'lasso_post_published', $postid, $content, get_current_user_ID() );
74
75
		return true;
76
77
	}
78
79
	/**
80
	 * The keys required for the actions of this class.
81
	 *
82
	 * @since     0.9.2
83
	 *
84
	 * @return array Array of keys to pull from $_POST per action and their sanitization callback
85
	 */
86
	public static function params(){
87
		$params[ 'process_save_content' ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
88
			'post_id' => 'absint',
89
			'content' => 'wp_kses_post'
90
		);
91
92
		$params[ 'process_save_publish_content' ] = array(
93
			'post_id' => 'absint',
94
			'content' => 'wp_kses_post'
95
		);
96
97
		return $params;
98
	}
99
100
	/**
101
	 * Additional auth callbacks to check.
102
	 *
103
	 * @since     0.9.2
104
	 *
105
	 * @return array Array of additional functions to use to authorize action.
106
	 */
107
	public static function auth_callbacks() {
108
		$params[ 'process_save_content' ] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
109
			'lasso_user_can'
110
		);
111
112
		$params[ 'process_save_publish_content' ] = array();
113
114
		return $params;
115
116
	}
117
118
	/**
119
	 * Check if saving post is disabled.
120
	 *
121
	 * @since 0.9.2
122
	 *
123
	 * @access protected
124
	 *
125
	 * @return bool
126
	 */
127
	protected function save_to_post_disables() {
128
		$save_to_post_disabled = lasso_editor_get_option( 'post_save_disabled', 'lasso_editor' );
0 ignored issues
show
Documentation introduced by
'post_save_disabled' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'lasso_editor' is of type string, but the function expects a object<unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
130
		return $save_to_post_disabled;
131
132
	}
133
134
	/**
135
	 * Replace shortcodes from other plugins with shortcode tags.
136
	 *
137
	 * @since 0.9.9
138
	 *
139
	 * @access protected
140
	 *
141
	 * @param string $content
142
	 *
143
	 * @return string
144
	 */
145
	protected function replace_rendered_shortcodes( $content ) {
146
		if ( false === strpos( $content, '<!--EDITUS_OTHER_SHORTCODE_START|' ) ) {
147
			return $content;
148
		}
149
150
		$content = preg_replace(
151
			'/<!--EDITUS_OTHER_SHORTCODE_START\|\[(.*?)\]-->(.*?)<!--EDITUS_OTHER_SHORTCODE_END-->/s',
152
			'$1',
153
			$content
154
		);
155
156
		return $content;
157
	}
158
159
}
160