Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
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 sharer
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 sharer',
32
		'version' => '4.0.0'
33
	];
34
35
	/**
36
	 * array of the option
37
	 *
38
	 * @var array
39
	 */
40
41
	protected $_optionArray =
42
	[
43
		'className' =>
44
		[
45
			'link' => 'rs-link-social-sharer',
46
			'list' => 'rs-list-social-sharer'
47
		],
48
		'network' =>
49
		[
50
			'facebook' =>
51
			[
52
				'url' => 'https://facebook.com/sharer.php?u=',
53
				'className' => 'rs-link-facebook'
54
			],
55
			'google-plus' =>
56
			[
57
				'url' => 'https://plus.google.com/share?url=',
58
				'className' => 'rs-link-google-plus'
59
			],
60
			'twitter' =>
61
			[
62
				'url' => 'https://twitter.com/intent/tweet?url=',
63
				'className' => 'rs-link-twitter'
64
			],
65
			'whatsapp' =>
66
			[
67
				'url' => 'https://api.whatsapp.com/send?text=',
68
				'className' => 'rs-link-whatsapp'
69
			],
70
			'telegram' =>
71
			[
72
				'url' => 'https://telegram.me/share/url?url=',
73
				'className' => 'rs-link-telegram'
74
			]
75
		]
76
	];
77
78
	/**
79
	 * articleFragmentEnd
80
	 *
81
	 * @since 3.0.0
82
	 *
83
	 * @return string|null
84
	 */
85
86
	public function articleFragmentEnd() : ?string
87
	{
88
		if ($this->_registry->get('lastTable') === 'articles')
89
		{
90
			$url = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . $this->_registry->get('fullRoute');
91
			return $this->render($url);
92
		}
93
		return null;
94
	}
95
96
	/**
97
	 * renderStart
98
	 *
99
	 * @since 3.0.0
100
	 */
101
102
	public function renderStart() : void
103
	{
104
		/* link */
105
106
		$link = Head\Link::getInstance();
107
		$link
108
			->init()
109
			->appendFile('modules/SocialSharer/dist/styles/social-sharer.min.css');
110
	}
111
112
	/**
113
	 * render
114
	 *
115
	 * @since 2.2.0
116
	 *
117
	 * @param string $url
118
	 *
119
	 * @return string|null
120
	 */
121
122
	public function render(string $url = null) : ?string
123
	{
124
		$output = null;
125
		$outputItem = null;
126
127
		/* html element */
128
129
		$element = new Html\Element();
130
		$listElement = $element
131
			->copy()
132
			->init('ul',
133
			[
134
				'class' => $this->_optionArray['className']['list']
135
			]);
136
		$itemElement = $element->copy()->init('li');
137
		$linkElement = $element
138
			->copy()
139
			->init('a',
140
			[
141
				'class' => $this->_optionArray['className']['link'],
142
				'target' => '_blank'
143
			]);
144
145
		/* process network */
146
147
		foreach ($this->_optionArray['network'] as $key => $value)
148
		{
149
			$outputItem .= $itemElement
150
				->html(
151
					$linkElement
152
						->copy()
153
						->addClass($value['className'])
154
						->attr('href', $value['url'] . $url)
155
						->text($key)
156
				);
157
		}
158
159
		/* collect output */
160
161
		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...
162
		{
163
			$output = $listElement->html($outputItem);
164
		}
165
		return $output;
166
	}
167
}
168