Passed
Push — develop ( 8ebc76...5a0ded )
by Reüel
05:00 queued 17s
created

FormsModule::source_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * Forms Module
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Forms
9
 */
10
11
namespace Pronamic\WordPress\Pay\Forms;
12
13
use Pronamic\WordPress\Pay\Payments\Payment;
14
use Pronamic\WordPress\Pay\Plugin;
15
16
/**
17
 * Forms Module
18
 *
19
 * @author Remco Tolsma
20
 * @version 3.7.0
21
 * @since 3.7.0
22
 */
23
class FormsModule {
24
	/**
25
	 * Plugin.
26
	 *
27
	 * @var Plugin
28
	 */
29
	private $plugin;
30
31
	/**
32
	 * Constructs and initalize a forms module object.
33
	 *
34
	 * @param Plugin $plugin Plugin.
35
	 */
36
	public function __construct( $plugin ) {
37
		$this->plugin = $plugin;
38
39
		// Form Post Type.
40
		$this->form_post_type = new FormPostType( $plugin );
0 ignored issues
show
Bug Best Practice introduced by
The property form_post_type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
42
		// Processor.
43
		$this->processor = new FormProcessor( $plugin );
0 ignored issues
show
Bug Best Practice introduced by
The property processor does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
45
		// Scripts.
46
		$this->scripts = new FormScripts( $plugin );
0 ignored issues
show
Bug Best Practice introduced by
The property scripts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
48
		// Shortcode.
49
		$this->shortcode = new FormShortcode( $this );
0 ignored issues
show
Bug Best Practice introduced by
The property shortcode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
51
		// Actions.
52
		add_filter( 'the_content', array( $this, 'maybe_add_form_to_content' ) );
53
54
		add_filter( 'pronamic_payment_source_text_payment_form', array( $this, 'source_text' ), 10, 2 );
55
		add_filter( 'pronamic_payment_source_description_payment_form', array( $this, 'source_description' ), 10, 2 );
56
	}
57
58
	/**
59
	 * Maybe add form to content.
60
	 *
61
	 * @see https://developer.wordpress.org/reference/hooks/the_content/
62
	 * @param string $content Post content to maybe extend with a payment form.
63
	 * @return string
64
	 */
65
	public function maybe_add_form_to_content( $content ) {
66
		if ( is_singular( 'pronamic_pay_form' ) && 'pronamic_pay_form' === get_post_type() ) {
67
			$content .= $this->get_form_output( get_the_ID() );
0 ignored issues
show
Bug introduced by
It seems like get_the_ID() can also be of type false; however, parameter $id of Pronamic\WordPress\Pay\F...dule::get_form_output() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
			$content .= $this->get_form_output( /** @scrutinizer ignore-type */ get_the_ID() );
Loading history...
68
		}
69
70
		return $content;
71
	}
72
73
	/**
74
	 * Get form output.
75
	 *
76
	 * @param string $id Form ID.
77
	 * @return string
78
	 */
79
	public function get_form_output( $id ) {
80
		$file = plugin_dir_path( $this->plugin->get_file() ) . 'templates/form.php';
81
82
		ob_start();
83
84
		include $file;
85
86
		$output = ob_get_clean();
87
88
		return $output;
89
	}
90
91
	/**
92
	 * Source text filter.
93
	 *
94
	 * @param string  $text    The source text to filter.
95
	 * @param Payment $payment The payment for the specified source text.
96
	 * @return string
97
	 */
98
	public function source_text( $text, Payment $payment ) {
99
		$text = __( 'Payment Form', 'pronamic_ideal' ) . '<br />';
100
101
		$text .= sprintf(
102
			'<a href="%s">%s</a>',
103
			get_edit_post_link( $payment->source_id ),
0 ignored issues
show
Bug introduced by
$payment->source_id of type string is incompatible with the type integer|WP_Post expected by parameter $id of get_edit_post_link(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
			get_edit_post_link( /** @scrutinizer ignore-type */ $payment->source_id ),
Loading history...
104
			$payment->source_id
105
		);
106
107
		return $text;
108
	}
109
110
	/**
111
	 * Source description filter.
112
	 *
113
	 * @param string  $text    The source text to filter.
114
	 * @param Payment $payment The payment for the specified source text.
115
	 * @return string
116
	 */
117
	public function source_description( $text, Payment $payment ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

117
	public function source_description( $text, /** @scrutinizer ignore-unused */ Payment $payment ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $text is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

117
	public function source_description( /** @scrutinizer ignore-unused */ $text, Payment $payment ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
		$text = __( 'Payment Form', 'pronamic_ideal' ) . '<br />';
119
120
		return $text;
121
	}
122
}
123