Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

modules/SocialSharer/SocialSharer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\SocialSharer;
3
4
use Redaxscript\Head;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
8
/**
9
 * integrate social network buttons
10
 *
11
 * @since 3.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Modules
15
 * @author Henry Ruhs
16
 */
17
18
class SocialSharer extends Module\Module
19
{
20
	/**
21
	 * array of the module
22
	 *
23
	 * @var array
24
	 */
25
26
	protected static $_moduleArray =
27
	[
28
		'name' => 'Social Sharer',
29
		'alias' => 'SocialSharer',
30
		'author' => 'Redaxmedia',
31
		'description' => 'Integrate social network buttons',
32
		'version' => '4.5.0',
33
		'license' => 'MIT'
34
	];
35
36
	/**
37
	 * array of the option
38
	 *
39
	 * @var array
40
	 */
41
42
	protected $_optionArray =
43
	[
44
		'className' =>
45
		[
46
			'link' => 'rs-link-social-sharer',
47
			'list' => 'rs-list-social-sharer'
48
		],
49
		'network' =>
50
		[
51
			'facebook' =>
52
			[
53
				'url' => 'https://facebook.com/sharer.php?u=',
54
				'className' => 'rs-link-facebook'
55
			],
56
			'twitter' =>
57
			[
58
				'url' => 'https://twitter.com/intent/tweet?url=',
59
				'className' => 'rs-link-twitter'
60
			],
61
			'whatsapp' =>
62
			[
63
				'url' => 'https://api.whatsapp.com/send?text=',
64
				'className' => 'rs-link-whatsapp'
65
			],
66
			'telegram' =>
67
			[
68
				'url' => 'https://telegram.me/share/url?url=',
69
				'className' => 'rs-link-telegram'
70
			]
71
		]
72
	];
73
74
	/**
75
	 * articleFragmentEnd
76
	 *
77
	 * @since 3.0.0
78
	 *
79
	 * @return string|null
80
	 */
81
82
	public function articleFragmentEnd() : ?string
83
	{
84
		if ($this->_registry->get('lastTable') === 'articles')
85
		{
86
			$url = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . $this->_registry->get('fullRoute');
87
			return $this->render($url);
88
		}
89
		return null;
90
	}
91
92
	/**
93
	 * renderStart
94
	 *
95
	 * @since 3.0.0
96
	 */
97
98
	public function renderStart() : void
99
	{
100
		/* link */
101
102
		$link = Head\Link::getInstance();
103
		$link
104
			->init()
105
			->appendFile('modules/SocialSharer/dist/styles/social-sharer.min.css');
106
	}
107
108
	/**
109
	 * render
110
	 *
111
	 * @since 2.2.0
112
	 *
113
	 * @param string $url
114
	 *
115
	 * @return string|null
116
	 */
117
118
	public function render(string $url = null) : ?string
119
	{
120
		$output = null;
121
		$outputItem = null;
122
123
		/* html element */
124
125
		$element = new Html\Element();
126
		$listElement = $element
127
			->copy()
128
			->init('ul',
129
			[
130
				'class' => $this->_optionArray['className']['list']
131
			]);
132
		$itemElement = $element->copy()->init('li');
133
		$linkElement = $element
134
			->copy()
135
			->init('a',
136
			[
137
				'class' => $this->_optionArray['className']['link'],
138
				'target' => '_blank'
139
			]);
140
141
		/* process network */
142
143
		foreach ($this->_optionArray['network'] as $key => $value)
144
		{
145
			$outputItem .= $itemElement
146
				->html(
147
					$linkElement
148
						->copy()
149
						->addClass($value['className'])
150
						->attr('href', $value['url'] . $url)
151
						->text($key)
152
				);
153
		}
154
155
		/* collect output */
156
157
		if ($outputItem)
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputItem of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
		{
159
			$output = $listElement->html($outputItem);
160
		}
161
		return $output;
162
	}
163
}
164