Completed
Push — master ( c43eec...2d99df )
by
unknown
02:10
created

save::remove_comments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 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
		$content = $this->remove_comments( $content );
29
30
		if ( 'off' == $save_to_post_disabled || empty( $save_to_post_disabled ) ) {
31
32
			$args = array(
33
				'ID'           => (int) $postid,
34
				'post_content' => $content
35
			);
36
37
			wp_update_post( apply_filters( 'lasso_object_save_args', $args ) );
38
39
		}
40
41
		// run save action
42
		do_action( 'lasso_post_saved', $postid, $content, get_current_user_ID() );
43
44
		return true;
45
46
	}
47
48
	/**
49
	 * Process the post save
50
	 *
51
	 * @since 0.9.2
52
	 *
53
	 * @param array $data Sanitized data to use for saving.
54
	 *
55
	 * @return bool Always returns true.
56
	 */
57 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...
58
		$save_to_post_disabled = $this->save_to_post_disables();
59
60
		$postid = (int) $data[ 'post_id' ];
61
		$content = $this->replace_rendered_shortcodes( $data[ 'content' ] );
62
		$content = $this->remove_comments( $content );
63
64
		if ( 'off' == $save_to_post_disabled || empty( $save_to_post_disabled ) ) {
65
66
			$args = array (
67
				'ID'           	=> $postid,
68
				'post_content' 	=> $content,
69
				'post_status' 	=> 'publish'
70
			);
71
			wp_update_post( apply_filters( 'lasso_object_publish_args', $args ) );
72
73
		}
74
75
		do_action( 'lasso_post_published', $postid, $content, get_current_user_ID() );
76
77
		return true;
78
79
	}
80
81
	/**
82
	 * The keys required for the actions of this class.
83
	 *
84
	 * @since     0.9.2
85
	 *
86
	 * @return array Array of keys to pull from $_POST per action and their sanitization callback
87
	 */
88
	public static function params(){
89
		$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...
90
			'post_id' => 'absint',
91
			'content' => 'wp_kses_post'
92
		);
93
94
		$params[ 'process_save_publish_content' ] = array(
95
			'post_id' => 'absint',
96
			'content' => 'wp_kses_post'
97
		);
98
99
		return $params;
100
	}
101
102
	/**
103
	 * Additional auth callbacks to check.
104
	 *
105
	 * @since     0.9.2
106
	 *
107
	 * @return array Array of additional functions to use to authorize action.
108
	 */
109
	public static function auth_callbacks() {
110
		$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...
111
			'lasso_user_can'
112
		);
113
114
		$params[ 'process_save_publish_content' ] = array();
115
116
		return $params;
117
118
	}
119
120
	/**
121
	 * Check if saving post is disabled.
122
	 *
123
	 * @since 0.9.2
124
	 *
125
	 * @access protected
126
	 *
127
	 * @return bool
128
	 */
129
	protected function save_to_post_disables() {
130
		$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...
131
132
		return $save_to_post_disabled;
133
134
	}
135
136
	/**
137
	 * Replace shortcodes from other plugins with shortcode tags.
138
	 *
139
	 * @since 0.9.9
140
	 *
141
	 * @access protected
142
	 *
143
	 * @param string $content
144
	 *
145
	 * @return string
146
	 */
147
	protected function replace_rendered_shortcodes( $content ) {
148
		//debug line
149
        //file_put_contents(WP_PLUGIN_DIR."/file1.txt", $content);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
		
151
152
		if ( false === strpos( $content, '--EDITUS_OTHER_SHORTCODE_START|' ) ) {
153
			return $content;
154
		}
155
		$content = htmlspecialchars_decode ($content);
156
157
		$content = preg_replace(
158
			'/<!--EDITUS_OTHER_SHORTCODE_START\|\[(.*?)\]-->(.*?)<!--EDITUS_OTHER_SHORTCODE_END-->/s',
159
			'$1',
160
			$content
161
		);
162
163
		return $content;
164
	}
165
	
166
	protected function remove_comments($content) {
167
		return preg_replace('/<!--(.*)-->/Uis', '', $content);
168
	}
169
170
}
171