|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelCards; |
|
4
|
|
|
|
|
5
|
|
|
class LaravelCards |
|
6
|
|
|
{ |
|
7
|
|
|
protected $postModelConfig = []; |
|
8
|
|
|
|
|
9
|
|
|
public function __construct() |
|
10
|
|
|
{ |
|
11
|
|
|
$this->postModelConfig = config('laravel-cards.models.post'); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
// ********************************************************************** |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Provide the post data array (post_title, post_body, post_image). |
|
18
|
|
|
* @param int $postId |
|
19
|
|
|
* @return \DavideCasiraghi\LaravelCards\Models\Post $ret |
|
20
|
|
|
**/ |
|
21
|
|
|
public function getPost($postId) |
|
22
|
|
|
{ |
|
23
|
|
|
$postModel = $this->postModelConfig['class']; |
|
24
|
|
|
$ret = $postModel::where('id', $postId)->first(); |
|
25
|
|
|
|
|
26
|
|
|
return $ret; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/************************************************************************/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Find the card snippet occurances in the text. |
|
33
|
|
|
* @param array $text The text where to find the card snippets |
|
34
|
|
|
* @return array $matches The matches |
|
35
|
|
|
**/ |
|
36
|
|
|
public function getCardSnippetOccurrences($text) |
|
37
|
|
|
{ |
|
38
|
|
|
$re = '/{\# |
|
39
|
|
|
\h+card |
|
40
|
|
|
\h+(post_id|img_alignment|img_col_size|bkg_color|text_color|container_wrap)=\[([^]]*)] |
|
41
|
|
|
\h+((?1))=\[([^]]*)] |
|
42
|
|
|
\h+((?1))=\[([^]]*)] |
|
43
|
|
|
\h+((?1))=\[([^]]*)] |
|
44
|
|
|
\h+((?1))=\[([^]]*)] |
|
45
|
|
|
\h+((?1))=\[([^]]*)] |
|
46
|
|
|
\h*\#}/x'; |
|
47
|
|
|
|
|
48
|
|
|
if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) { |
|
|
|
|
|
|
49
|
|
|
return $matches; |
|
50
|
|
|
} else { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// ********************************************************************** |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the plugin parameters. |
|
59
|
|
|
* @param array $matches result from the regular expression on the string from the article |
|
60
|
|
|
* @return array $ret the array containing the parameters |
|
61
|
|
|
**/ |
|
62
|
|
|
public function getParameters($matches) |
|
63
|
|
|
{ |
|
64
|
|
|
$ret = []; |
|
65
|
|
|
|
|
66
|
|
|
// Get activation string parameters (from article) |
|
67
|
|
|
$ret['token'] = $matches[0]; |
|
68
|
|
|
//dump($matches); |
|
69
|
|
|
|
|
70
|
|
|
$ret['post_id'] = $matches[2]; |
|
71
|
|
|
$ret['img_col_size_class'] = 'col-md-'.$matches[6]; |
|
72
|
|
|
$textColSize = 12 - $matches[6]; |
|
73
|
|
|
$ret['text_col_size_class'] = 'col-md-'.$textColSize; |
|
74
|
|
|
$backgroundColor = $matches[8]; |
|
75
|
|
|
$ret['bkg_color'] = 'background-color: '.$backgroundColor.';'; |
|
76
|
|
|
$textColor = $matches[10]; |
|
77
|
|
|
$ret['text_color'] = 'color: '.$textColor.';'; |
|
78
|
|
|
$containerWrap = $matches[12]; |
|
79
|
|
|
$ret['container_wrap'] = ($containerWrap == 'true') ? 1 : 0; |
|
80
|
|
|
|
|
81
|
|
|
//dd($ret['bkg_color']); |
|
82
|
|
|
// Image alignment |
|
83
|
|
|
//$ret['img_alignment'] = $matches[4]; |
|
84
|
|
|
$imageAlignment = $matches[4]; |
|
85
|
|
|
|
|
86
|
|
|
switch ($imageAlignment) { |
|
87
|
|
|
case 'left': |
|
88
|
|
|
$ret['img_col_order_class'] = 'order-md-1'; |
|
89
|
|
|
$ret['text_col_order_class'] = 'order-md-2'; |
|
90
|
|
|
break; |
|
91
|
|
|
case 'right': |
|
92
|
|
|
$ret['img_col_order_class'] = 'order-md-2'; |
|
93
|
|
|
$ret['text_col_order_class'] = 'order-md-1'; |
|
94
|
|
|
break; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
//dump($ret); |
|
98
|
|
|
|
|
99
|
|
|
return $ret; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// ********************************************************************** |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Prepare the card HTML. |
|
106
|
|
|
* @param array $parameters |
|
107
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\Post $postData |
|
108
|
|
|
* |
|
109
|
|
|
* @return string $ret |
|
110
|
|
|
**/ |
|
111
|
|
|
public function prepareCardHtml($parameters, $postData) |
|
112
|
|
|
{ |
|
113
|
|
|
$ret = "<div class='row featurette' style='".$parameters['bkg_color'].$parameters['text_color']."'>"; |
|
114
|
|
|
if ($parameters['container_wrap']) { |
|
115
|
|
|
$ret .= "<div class='container'>"; |
|
116
|
|
|
} |
|
117
|
|
|
$ret .= "<div class='text ".$parameters['text_col_size_class'].' my-auto px-4 '.$parameters['text_col_order_class']."'>"; |
|
118
|
|
|
$ret .= "<h2 class='featurette-heading mt-5'>".$postData['post_title'].'</h2>'; |
|
119
|
|
|
$ret .= "<div class='lead mb-4'>".$postData['post_body'].'</div>'; |
|
120
|
|
|
$ret .= '</div>'; |
|
121
|
|
|
$ret .= "<div class='image ".$parameters['img_col_size_class'].' '.$parameters['img_col_order_class']."'>"; |
|
122
|
|
|
if (! empty($postData['post_image_src'])) { |
|
123
|
|
|
$ret .= "<img class='featurette-image img-fluid mx-auto' src='".$postData['post_image_src']."' alt='".$postData['post_image_alt']."'>"; |
|
124
|
|
|
} |
|
125
|
|
|
$ret .= '</div>'; |
|
126
|
|
|
if ($parameters['container_wrap']) { |
|
127
|
|
|
$ret .= '</div>'; |
|
128
|
|
|
} |
|
129
|
|
|
$ret .= '</div>'; |
|
130
|
|
|
|
|
131
|
|
|
return $ret; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function replace_card_strings_with_template($text) |
|
135
|
|
|
{ |
|
136
|
|
|
$matches = LaravelCards::getCardSnippetOccurrences($text); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
foreach ($matches as $key => $single_gallery_matches) { |
|
139
|
|
|
$parameters = LaravelCards::getParameters($single_gallery_matches); |
|
|
|
|
|
|
140
|
|
|
$post = LaravelCards::getPost($post['id']); |
|
|
|
|
|
|
141
|
|
|
$cardHtml = LaravelCards::prepareCardHtml($parameters, $post); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
// Substitute the card html to the token that has been found |
|
144
|
|
|
$text = str_replace($parameters['token'], $cardHtml, $postBody); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $text; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.