@@ -59,6 +59,13 @@ class PlgSystemStats extends JPlugin
5959 */
6060 protected $ db ;
6161
62+ /**
63+ * Url to send the statistics.
64+ *
65+ * @var string
66+ */
67+ protected $ serverUrl = 'https://developer.joomla.org/stats/submit ' ;
68+
6269 /**
6370 * Unique identifier for this site
6471 *
@@ -76,25 +83,13 @@ class PlgSystemStats extends JPlugin
7683 */
7784 public function onAfterInitialise ()
7885 {
79- if (!$ this ->app ->isAdmin () || !$ this ->isAllowedUser () || ! $ this -> isUpdateRequired () )
86+ if (!$ this ->app ->isAdmin () || !$ this ->isAllowedUser ())
8087 {
8188 return ;
8289 }
8390
84- $ mode = (int ) $ this ->params ->get ('mode ' );
85-
86- // Plugin parameters are saved and send always enabled
87- if ($ mode === static ::MODE_ALLOW_ALWAYS )
91+ if (!$ this ->isDebugEnabled () && !$ this ->isUpdateRequired ())
8892 {
89- try
90- {
91- $ this ->sendStats ();
92- }
93- catch (Exception $ e )
94- {
95- JLog::add ($ e ->getMessage (), JLog::WARNING , 'stats ' );
96- }
97-
9893 return ;
9994 }
10095
@@ -103,92 +98,129 @@ public function onAfterInitialise()
10398 }
10499
105100 /**
106- * Get the user message rendered
101+ * User selected to always send data
107102 *
108- * @return array
103+ * @return void
109104 *
110105 * @since 3.5
106+ *
107+ * @throws Exception If user is not allowed.
108+ * @throws RuntimeException If there is an error saving the params or sending the data.
111109 */
112- public function onAjaxRenderStatsMessage ()
110+ public function onAjaxSendAlways ()
113111 {
114112 if (!$ this ->isAllowedUser () || !$ this ->isAjaxRequest ())
115113 {
116114 throw new Exception (JText::_ ('JGLOBAL_AUTH_ACCESS_DENIED ' ), 403 );
117115 }
118116
119- return array (
120- 'html ' => $ this ->getRenderer ('message ' )->render ($ this ->getLayoutData ())
121- );
117+ $ this ->params ->set ('mode ' , static ::MODE_ALLOW_ALWAYS );
118+
119+ if (!$ this ->saveParams ())
120+ {
121+ throw new RuntimeException ('Unable to save plugin settings ' , 500 );
122+ }
123+
124+ $ this ->sendStats ();
125+
126+ echo json_encode (array ('sent ' => 1 ));
122127 }
123128
124129 /**
125- * User selected to always send data
130+ * User selected to never send data.
126131 *
127132 * @return void
128133 *
129134 * @since 3.5
135+ *
136+ * @throws Exception If user is not allowed.
137+ * @throws RuntimeException If there is an error saving the params.
130138 */
131- public function onAjaxSendAlways ()
139+ public function onAjaxSendNever ()
132140 {
133141 if (!$ this ->isAllowedUser () || !$ this ->isAjaxRequest ())
134142 {
135143 throw new Exception (JText::_ ('JGLOBAL_AUTH_ACCESS_DENIED ' ), 403 );
136144 }
137145
138- $ this ->params ->set ('mode ' , static ::MODE_ALLOW_ALWAYS );
146+ $ this ->params ->set ('mode ' , static ::MODE_ALLOW_NEVER );
139147
140148 if (!$ this ->saveParams ())
141149 {
142150 throw new RuntimeException ('Unable to save plugin settings ' , 500 );
143151 }
144152
145- $ this -> sendStats ( );
153+ echo json_encode ( array ( ' sent ' => 0 ) );
146154 }
147155
148156 /**
149- * User selected to never send data.
157+ * User selected to send data once .
150158 *
151159 * @return void
152160 *
153161 * @since 3.5
162+ *
163+ * @throws Exception If user is not allowed.
164+ * @throws RuntimeException If there is an error saving the params or sending the data.
154165 */
155- public function onAjaxSendNever ()
166+ public function onAjaxSendOnce ()
156167 {
157168 if (!$ this ->isAllowedUser () || !$ this ->isAjaxRequest ())
158169 {
159170 throw new Exception (JText::_ ('JGLOBAL_AUTH_ACCESS_DENIED ' ), 403 );
160171 }
161172
162- $ this ->params ->set ('mode ' , static ::MODE_ALLOW_NEVER );
173+ $ this ->params ->set ('mode ' , static ::MODE_ALLOW_ONCE );
163174
164175 if (!$ this ->saveParams ())
165176 {
166177 throw new RuntimeException ('Unable to save plugin settings ' , 500 );
167178 }
179+
180+ $ this ->sendStats ();
181+
182+ echo json_encode (array ('sent ' => 1 ));
168183 }
169184
170185 /**
171- * User selected to send data once.
186+ * Send the stats to the server.
187+ * On first load | on demand mode it will show a message asking users to select mode.
172188 *
173189 * @return void
174190 *
175191 * @since 3.5
192+ *
193+ * @throws Exception If user is not allowed.
194+ * @throws RuntimeException If there is an error saving the params or sending the data.
176195 */
177- public function onAjaxSendOnce ()
196+ public function onAjaxSendStats ()
178197 {
179198 if (!$ this ->isAllowedUser () || !$ this ->isAjaxRequest ())
180199 {
181200 throw new Exception (JText::_ ('JGLOBAL_AUTH_ACCESS_DENIED ' ), 403 );
182201 }
183202
184- $ this ->params ->set ('mode ' , static ::MODE_ALLOW_ONCE );
203+ // User has not selected the mode. Show message.
204+ if ((int ) $ this ->params ->get ('mode ' ) !== static ::MODE_ALLOW_ALWAYS )
205+ {
206+ $ data = array (
207+ 'sent ' => 0 ,
208+ 'html ' => $ this ->getRenderer ('message ' )->render ($ this ->getLayoutData ())
209+ );
210+
211+ echo json_encode ($ data );
212+
213+ return ;
214+ }
185215
186216 if (!$ this ->saveParams ())
187217 {
188218 throw new RuntimeException ('Unable to save plugin settings ' , 500 );
189219 }
190220
191221 $ this ->sendStats ();
222+
223+ echo json_encode (array ('sent ' => 1 ));
192224 }
193225
194226 /**
@@ -321,7 +353,7 @@ private function isUpdateRequired()
321353 }
322354
323355 // Never updated or debug enabled
324- if (!$ last || ! $ interval || $ this ->isDebugEnabled ())
356+ if (!$ last || $ this ->isDebugEnabled ())
325357 {
326358 return true ;
327359 }
@@ -338,7 +370,7 @@ private function isUpdateRequired()
338370 */
339371 private function isAjaxRequest ()
340372 {
341- return ( isset ( $ _SERVER [ 'HTTP_X_REQUESTED_WITH ' ]) && strtolower ( $ _SERVER [ ' HTTP_X_REQUESTED_WITH ' ]) == 'xmlhttprequest ' ) ;
373+ return strtolower ( $ this -> app -> input -> server -> get ( 'HTTP_X_REQUESTED_WITH ' , '' )) == 'xmlhttprequest ' ;
342374 }
343375
344376 /**
@@ -408,13 +440,15 @@ private function saveParams()
408440 * @return boolean
409441 *
410442 * @since 3.5
443+ *
444+ * @throws RuntimeException If there is an error sending the data.
411445 */
412446 private function sendStats ()
413447 {
414448 try
415449 {
416450 // Don't let the request take longer than 2 seconds to avoid page timeout issues
417- $ response = JHttpFactory::getHttp ()->post ($ this ->params -> get ( ' url ' , ' https://developer.joomla.org/stats/submit ' ) , $ this ->getStatsData (), null , 2 );
451+ JHttpFactory::getHttp ()->post ($ this ->serverUrl , $ this ->getStatsData (), null , 2 );
418452 }
419453 catch (UnexpectedValueException $ e )
420454 {
0 commit comments