Completed
Push — mobile-api-entity-checking ( 8bb70a )
by
unknown
23:08
created

blog.php ➔ get_blogpost()   C

Complexity

Conditions 10
Paths 36

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 36
nop 3
dl 0
loc 65
rs 6.8969
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Exposes API endpoints for Blog entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.blogpost",
8
	"get_blogpost",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
13
	),
14
	'Retrieves a blog post & all replies based on user id and blog post id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.blogposts",
22
	"get_blogposts",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
26
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
27
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
28
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
29
	),
30
	'Retrieves blog posts & all replies based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
elgg_ws_expose_function(
37
	"get.blogpostsbyowner",
38
	"get_blogposts_by_owner",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
42
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
43
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
44
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en"),
45
		"target" => array('type' => 'string', 'required'=> false, 'default' => '')
46
	),
47
	'Retrieves blog posts & all replies based on user id',
48
	'POST',
49
	true,
50
	false
51
);
52
53
elgg_ws_expose_function(
54
 "get.blogpostsbycolleague",
55
 "get_blogposts_by_colleague",
56
 array(
57
	"user" => array('type' => 'string', 'required' => true),
58
	"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
59
	"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
60
	"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
61
	"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
62
 ),
63
 'Retrieves a container\'s blogs based on user id and container guid. Used for groups, as a group\'s blogs have container_id of the group.',
64
 'POST',
65
 true,
66
 false
67
);
68
69
elgg_ws_expose_function(
70
 "get.blogpostsbycontainer",
71
 "get_blogposts_by_container",
72
 array(
73
	 "user" => array('type' => 'string', 'required' => true),
74
	 "guid" => array('type' => 'int', 'required' => true),
75
	 "limit" => array('type' => 'int', 'required' => false, 'default' => 10),
76
	 "offset" => array('type' => 'int', 'required' => false, 'default' => 0),
77
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
78
 ),
79
 'Retrieves a container\'s blogs based on user id and container guid. Used for groups, as a group\'s blogs have container_id of the group.',
80
 'POST',
81
 true,
82
 false
83
);
84
85
elgg_ws_expose_function(
86
 "get.blogedit",
87
 "get_blog_edit",
88
 array(
89
	 "user" => array('type' => 'string', 'required' => true),
90
	 "guid" => array('type' => 'int', 'required' => true),
91
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
92
 ),
93
 'Retrieves a blog post based on user id and blog post id, with only info needed for edit form',
94
 'POST',
95
 true,
96
 false
97
);
98
99
elgg_ws_expose_function(
100
 "save.blog",
101
 "save_blog",
102
 array(
103
	 "user" => array('type' => 'string', 'required' => true),
104
	 "title" => array('type' => 'string', 'required' => true),
105
	 "excerpt" => array('type' =>'string', 'required' => false, 'default' => ''),
106
	 "body" => array('type' =>'string', 'required' => true),
107
	 "container_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
108
	 "blog_guid" => array('type' =>'string', 'required' => false, 'default' => ''),
109
	 "comments" => array('type' =>'int', 'required' => false, 'default' => 1),
110
	 "access" => array('type' =>'int', 'required' => false, 'default' => 1),
111
	 "status" => array('type' =>'int', 'required' => false, 'default' => 0),
112
	 "lang" => array('type' => 'string', 'required' => false, 'default' => "en")
113
 ),
114
 'Posts/Saves a blog post',
115
 'POST',
116
 true,
117
 false
118
);
119
120
function foreach_blogs($blogs, $user_entity, $lang)
121
{
122
	foreach ($blogs as $blog_post) {
123
		$blog_post->title = gc_explode_translation($blog_post->title, $lang);
124
		$blog_post->description = gc_explode_translation($blog_post->description, $lang);
125
126
		$likes = elgg_get_annotations(array(
127
			'guid' => $blog_post->guid,
128
			'annotation_name' => 'likes'
129
		));
130
		$blog_post->likes = count($likes);
131
132
		$liked = elgg_get_annotations(array(
133
			'guid' => $blog_post->guid,
134
			'annotation_owner_guid' => $user_entity->guid,
135
			'annotation_name' => 'likes'
136
		));
137
		$blog_post->liked = count($liked) > 0;
138
139
		$blog_post->comment_count = elgg_get_entities(array(
140
			'container_guid' => $guid,
0 ignored issues
show
Bug introduced by
The variable $guid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
141
			'count' => true,
142
			'distinct' => false,
143
		));
144
145
		$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
146
147
		$group = get_entity($blog_post->container_guid);
148
		$blog_post->group = gc_explode_translation($group->name, $lang);
149
150
		if (is_callable(array($group, 'getURL'))) {
151
			$blog_post->groupURL = $group->getURL();
152
		}
153
	}
154
	return $blogs;
155
}
156
157
function get_blogpost($user, $guid, $lang)
158
{
159
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
160
	if (!$user_entity) {
161
		return "User was not found. Please try a different GUID, username, or email address";
162
	}
163
	if (!$user_entity instanceof ElggUser) {
164
		return "Invalid user. Please try a different GUID, username, or email address";
165
	}
166
167
	$entity = get_entity($guid);
168
	if (!isset($entity)) {
169
		return "Blog was not found. Please try a different GUID";
170
	}
171
	if (!$entity) {
172
    return "Blog was not found.";
173
  }
174
	if (!elgg_instanceof($entity, 'object', 'blog')) {
175
		return "Invalid blog.";
176
	}
177
178
	if (!elgg_is_logged_in()) {
179
		login($user_entity);
180
	}
181
182
	$blog_posts = elgg_list_entities(array(
183
		'type' => 'object',
184
		'subtype' => 'blog',
185
		'guid' => $guid
186
	));
187
	$blog_post = json_decode($blog_posts)[0];
188
189
	$blog_post->title = gc_explode_translation($blog_post->title, $lang);
190
	$blog_post->description = gc_explode_translation($blog_post->description, $lang);
191
192
	$likes = elgg_get_annotations(array(
193
		'guid' => $blog_post->guid,
194
		'annotation_name' => 'likes'
195
	));
196
	$blog_post->likes = count($likes);
197
198
	$liked = elgg_get_annotations(array(
199
		'guid' => $blog_post->guid,
200
		'annotation_owner_guid' => $user_entity->guid,
201
		'annotation_name' => 'likes'
202
	));
203
	$blog_post->liked = count($liked) > 0;
204
205
	$blog_post->comment_count = elgg_get_entities(array(
206
		'container_guid' => $guid,
207
		'count' => true,
208
		'distinct' => false,
209
	));
210
211
	$blog_post->userDetails = get_user_block($blog_post->owner_guid, $lang);
212
213
	$group = get_entity($blog_post->container_guid);
214
	$blog_post->group = gc_explode_translation($group->name, $lang);
215
216
	if (is_callable(array($group, 'getURL'))) {
217
		$blog_post->groupURL = $group->getURL();
218
	}
219
220
	return $blog_post;
221
}
222
223
function get_blogposts($user, $limit, $offset, $filters, $lang)
224
{
225
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
226
	if (!$user_entity) {
227
		return "User was not found. Please try a different GUID, username, or email address";
228
	}
229
	if (!$user_entity instanceof ElggUser) {
230
		return "Invalid user. Please try a different GUID, username, or email address";
231
	}
232
233
	if (!elgg_is_logged_in()) {
234
		login($user_entity);
235
	}
236
237
	$filter_data = json_decode($filters);
238
	if (!empty($filter_data)) {
239
		$params = array(
240
			'type' => 'object',
241
			'subtype' => 'blog',
242
			'limit' => $limit,
243
			'offset' => $offset
244
		);
245
246
		if ($filter_data->name) {
247
			$db_prefix = elgg_get_config('dbprefix');
248
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
249
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
250
		}
251
252
		$all_blog_posts = elgg_list_entities_from_metadata($params);
253
	} else {
254
		$all_blog_posts = elgg_list_entities(array(
255
			'type' => 'object',
256
			'subtype' => 'blog',
257
			'limit' => $limit,
258
			'offset' => $offset
259
		));
260
	}
261
262
	$blog_posts = json_decode($all_blog_posts);
263
264
	$blogs = foreach_blogs($blog_posts, $user_entity, $lang);
265
266
	return $blogs;
267
}
268
269
function get_blogposts_by_owner($user, $limit, $offset, $filters, $lang, $target)
270
{
271
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
272
	if (!$user_entity) {
273
		return "User was not found. Please try a different GUID, username, or email address";
274
	}
275
	if (!$user_entity instanceof ElggUser) {
276
		return "Invalid user. Please try a different GUID, username, or email address";
277
	}
278
279
	$target_entity = $user_entity;
280
	if (!empty($target)){
281
		$target_entity =  is_numeric($target) ? get_user($target) : (strpos($target, '@') !== false ? get_user_by_email($target)[0] : get_user_by_username($target));
282
		if (!$target_entity) {
283
			return "Target user was not found. Please try a different GUID, username, or email address";
284
		}
285
		if (!$target_entity instanceof ElggUser) {
286
			return "Invalid target user. Please try a different GUID, username, or email address";
287
		}
288
	}
289
290
	if (!elgg_is_logged_in()) {
291
		login($user_entity);
292
	}
293
	$filter_data = json_decode($filters);
294
	if (!empty($filter_data)) {
295
		$params = array(
296
			'type' => 'object',
297
			'subtype' => 'blog',
298
			'owner_guid' => $target_entity->guid,
299
			'limit' => $limit,
300
			'offset' => $offset
301
		);
302
303
		if ($filter_data->name) {
304
			$db_prefix = elgg_get_config('dbprefix');
305
			$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
306
			$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
307
		}
308
309
		$all_blog_posts = elgg_list_entities_from_metadata($params);
310 View Code Duplication
	} else {
311
		$all_blog_posts = elgg_list_entities(array(
312
			'type' => 'object',
313
			'subtype' => 'blog',
314
			'owner_guid' => $target_entity->guid,
315
			'limit' => $limit,
316
			'offset' => $offset
317
		));
318
	}
319
320
	$blog_posts = json_decode($all_blog_posts);
321
322
	$blogs = foreach_blogs($blog_posts, $user_entity, $lang);
323
324
	return $blogs;
325
}
326
327
function get_blogposts_by_colleague($user, $limit, $offset, $filters, $lang, $target)
328
{
329
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
330
 if (!$user_entity) {
331
	 return "User was not found. Please try a different GUID, username, or email address";
332
 }
333
 if (!$user_entity instanceof ElggUser) {
334
	 return "Invalid user. Please try a different GUID, username, or email address";
335
 }
336
337
 if (!elgg_is_logged_in()) {
338
	 login($user_entity);
339
 }
340
341
 $filter_data = json_decode($filters);
342
 if (!empty($filter_data)) {
343
 	$params = array(
344
		'type' => 'object',
345
		'subtype' => 'blog',
346
		'relationship' => 'friend',
347
		'relationship_guid' => $user_entity->guid,
348
		'relationship_join_on' => 'container_guid',
349
		'limit' => $limit,
350
		'offset' => $offset
351
 	);
352
353
 	if ($filter_data->name) {
354
 		$db_prefix = elgg_get_config('dbprefix');
355
 		$params['joins'] = array("INNER JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid");
356
 		$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')");
357
 	}
358
359
 	$all_blog_posts = elgg_list_entities_from_relationship($params);
360
 } else {
361
	 $all_blog_posts = elgg_list_entities_from_relationship(array(
362
		 'type' => 'object',
363
		 'subtype' => 'blog',
364
		 'relationship' => 'friend',
365
		 'relationship_guid' => $user_entity->guid,
366
		 'relationship_join_on' => 'container_guid',
367
		 'limit' => $limit,
368
		 'offset' => $offset
369
	 ));
370
	}
371
 $blog_posts = json_decode($all_blog_posts);
372
373
 $blogs = foreach_blogs($blog_posts, $user_entity, $lang);
374
375
 return $blogs;
376
}
377
378
379
function get_blogposts_by_container($user, $guid, $limit, $offset, $lang)
380
{
381
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
382
 if (!$user_entity) {
383
	 return "User was not found. Please try a different GUID, username, or email address";
384
 }
385
 if (!$user_entity instanceof ElggUser) {
386
	 return "Invalid user. Please try a different GUID, username, or email address";
387
 }
388
 if (!elgg_is_logged_in()) {
389
	 login($user_entity);
390
 }
391
392
 $group = get_entity($guid);
393
 if (!$group) {
394
	 return "Group was not found. Please try a different GUID";
395
 }
396
 if (!$group instanceof ElggGroup) {
397
	 return "Invalid group. Please try a different GUID";
398
 }
399
400
 $all_blog_posts = elgg_list_entities(array(
401
	 'type' => 'object',
402
	 'subtype' => 'blog',
403
	 'container_guid' => $guid,
404
	 'limit' => $limit,
405
	 'offset' => $offset,
406
	 'order_by' => 'e.last_action desc'
407
 ));
408
409
 $blog_posts = json_decode($all_blog_posts);
410
411
 $blogs_final = foreach_blogs($blog_posts, $user_entity, $lang);
412
413
 return $blogs_final;
414
}
415
416
function get_blog_edit($user, $guid, $lang)
417
{
418
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
419
 if (!$user_entity) {
420
	 return "User was not found. Please try a different GUID, username, or email address";
421
 }
422
 if (!$user_entity instanceof ElggUser) {
423
	 return "Invalid user. Please try a different GUID, username, or email address";
424
 }
425
426
 $entity = get_entity($guid);
427
 if (!isset($entity)) {
428
	 return "Blog was not found. Please try a different GUID";
429
 }
430
431
 if (!elgg_is_logged_in()) {
432
	 login($user_entity);
433
 }
434
435
 $blog_posts = elgg_list_entities(array(
436
	 'type' => 'object',
437
	 'subtype' => 'blog',
438
	 'guid' => $guid
439
 ));
440
 $blog_post = json_decode($blog_posts)[0];
441
442
 $blog_post->title = json_decode($blog_post->title);
443
 //$blog_post->excerpt = json_decode($blog_post->excerpt); //not correct
444
 $blog_post->description = json_decode($blog_post->description);
445
446
 $container = get_entity($blog_post->container_guid);
447
 if ($container instanceof ElggGroup){
448
	 $blog_post->group->public = $container->isPublicMembership();
449
	 if (!$blog_post->group->public && !$container->isMember($user_entity)){
450
		return elgg_echo('discussion:error:permissions');
451
	 }
452
 }
453
454
 if (is_callable(array($group, 'getURL'))) {
455
	 $blog_post->groupURL = $group->getURL();
0 ignored issues
show
Bug introduced by
The variable $group does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
456
 }
457
458
 return $blog_post;
459
}
460
461
function save_blog($user, $title, $excerpt, $body, $container_guid, $blog_guid, $comments, $access, $status, $lang)
462
{
463
 $user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
464
	 if (!$user_entity) {
465
		 return "User was not found. Please try a different GUID, username, or email address";
466
	 }
467
	 if (!$user_entity instanceof ElggUser) {
468
		 return "Invalid user. Please try a different GUID, username, or email address";
469
	 }
470
	 if (!elgg_is_logged_in()) {
471
		 login($user_entity);
472
	 }
473
	 $error = FALSE;
474
	 //check required fields being not empty
475
	 $titles = json_decode($title);
476
	 $bodies = json_decode($body);
477
	 $excerpts = json_decode($excerpt);
478
	 //Check Required
479
	 if (!$titles->en && !$titles->fr) { return elgg_echo("blog:error:missing:title"); }
480
	 if (!$bodies->en && !$bodies->fr) { return elgg_echo("blog:error:missing:description");  }
481 View Code Duplication
	 if (!($titles->en && $bodies->en) && !($titles->fr && $bodies->fr)) { return "require-same-lang"; }
482
	 //Default any Missing or faulty
483
	 if (!$titles->en) { $titles->en = ''; }
484
	 if (!$titles->fr) { $titles->fr = ''; }
485
	 if (!$bodies->en) { $bodies->en = ''; }
486
	 if (!$bodies->fr) { $bodies->fr = ''; }
487
	 if (!$excerpts->en) { $excerpts->en = ''; }
488
	 if (!$excerpts->fr) { $excerpts->fr = ''; }
489
	 if ($comments != 0 && $comments != 1) { $comments = 1; }
490
	 if ($access != 0 && $access != 1 && $access != -2 && $access !=2 ) { $access = 1; }
491
	 if ($status != 0 && $status != 1) { $status = 0; }
492
493
	 // if there is a container_guid, .: group, and access is set to group only, set access to proper group only
494
	 if (!empty($container_guid) && $access == 2){
495
		 $container = get_entity($container_guid);
496
		 //validate container and ability to write to it
497
		 if (!$container || !$container->canWriteToContainer(0, 'object', 'blog')) {
498
 			return elgg_echo('blog:error:cannot_write_to_container');
499
 		} else {
500
			$access = $container->group_acl;
501
		}
502
		 //If no group container, use user guid.
503
	 } else if ($container_guid=='') { $container_guid = $user_entity->guid; }
504
505
	 //Set int variables to correct
506
	 if ($status == 1) { $status = 'published'; } else { $status = 'draft'; }
507
	 if ($comments == 1) { $comments = 'On'; } else { $comments = 'Off'; }
508
	 if ($status == 'draft') { $access = 0; }
509
	 $titles->en = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8');
510
	 $titles->fr = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8');
511
	 $excerpts->en = elgg_get_excerpt($excerpts->en);
512
	 $excerpts->fr = elgg_get_excerpt($excerpts->fr);
513
514
	 $values = array(
515
		 'title' => JSON_encode($titles),
516
		 'title2' => '',
517
		 //'title3' => '',
518
		 'description' => JSON_encode($bodies),
519
		 'description2' => '',
520
		 'description3' => '',
521
		 'status' => $status,
522
		 'access_id' => $access,
523
		 'comments_on' => $comments,
524
		 'excerpt' => JSON_encode($excerpts),
525
		 'excerpt2' => '',
526
		 'excerpt3' => '',
527
		 'tags' => '',
528
		 'publication_date' => '',
529
		 'expiration_date' => '',
530
		 'show_owner' => 'no'
531
	 );
532
533
	 $blog = new stdClass();
534
	 $revision_text = '';
535
	 if ($blog_guid){
536
		 $entity = get_entity($blog_guid);
537 View Code Duplication
		 if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) {
538
			 	$blog = $entity;
539
		 } else {
540
			 return elgg_echo('blog:error:post_not_found');
541
		 }
542
		 $revision_text = $blog->description;
543
		 $new_post = $blog->new_post; //what?
544
	 } else {
545
		 //Create blog
546
		 $blog = new ElggBlog();
547
		 $blog->subtype = 'blog';
548
		 $blog->container_guid = $container_guid;
549
		 $new_post = TRUE;
550
	 }
551
552
	 $old_status = $blog->status;
553
554
	 // assign values to the entity, stopping on error.
555 View Code Duplication
	 if (!$error) {
556
		 foreach ($values as $name => $value) {
557
			 if (($name != 'title2') && ($name != 'description2') &&  ($name != 'excerpt2')){ // remove input 2 in metastring table
558
			 $blog->$name = $value;
559
			 }
560
		 }
561
	 }
562
563
	 if (!$error){
564
		 if ($blog->save()){
565
566
				 $icon_file = get_resized_image_from_uploaded_file("icon", 100, 100);
567
				 $icon_sizes = elgg_get_config("icon_sizes");
568
569 View Code Duplication
				 if (!empty($icon_file) && !empty($icon_sizes)) {
570
					 // create icon
571
					 $prefix = "blogs/" . $blog->getGUID();
572
573
					 $fh = new ElggFile();
574
					 $fh->owner_guid = $blog->getOwnerGUID();
575
576
					 foreach ($icon_sizes as $icon_name => $icon_info) {
577
						 $icon_file = get_resized_image_from_uploaded_file("icon", $icon_info["w"], $icon_info["h"], $icon_info["square"], $icon_info["upscale"]);
578
						 if (!empty($icon_file)) {
579
							 $fh->setFilename($prefix . $icon_name . ".jpg");
580
581
							 if ($fh->open("write")) {
582
								 $fh->write($icon_file);
583
								 $fh->close();
584
							 }
585
						 }
586
					 }
587
588
					 $blog->icontime = time();
589
			 }
590
591
			 // remove autosave draft if exists
592
			 $blog->deleteAnnotations('blog_auto_save');
593
			 // no longer a brand new post.
594
			 $blog->deleteMetadata('new_post');
595
			 if (!$new_post && $revision_text) {
596
				 $blog->annotate('blog_revision', $revision_text);
597
			 }
598
599
			 $status = $blog->status;
600
				 // add to river if changing status or published, regardless of new post
601
				 // because we remove it for drafts.
602
				 if (($new_post || $old_status == 'draft' ||  $old_status == 'published') && $status == 'published') {
603
					 elgg_create_river_item(array(
604
						 'view' => 'river/object/blog/create',
605
						 'action_type' => 'create',
606
						 'subject_guid' => $blog->owner_guid,
607
						 'object_guid' => $blog->getGUID(),
608
					 ));
609
					 // we only want notifications sent when post published
610
					 elgg_trigger_event('publish', 'object', $blog);
0 ignored issues
show
Documentation introduced by
$blog is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
611
612
					 // reset the creation time for posts that move from draft to published
613
					 if ($guid) {
0 ignored issues
show
Bug introduced by
The variable $guid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
614
						 $blog->time_created = time();
615
						 $blog->save();
616
					 }
617
				 } elseif ($old_status == 'published' && $status == 'draft') {
618
					 elgg_delete_river(array(
619
						 'object_guid' => $blog->guid,
620
						 'action_type' => 'create',
621
					 ));
622
				 }
623
				 if ($blog->status == 'published' || $save == false) {
0 ignored issues
show
Bug introduced by
The variable $save does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
624
					 return ($blog->getURL());
625
				 } else {
626
					 return ("blog/edit/$blog->guid");
627
				 }
628
629
		 } else {
630
			 return elgg_echo('blog:error:cannot_save');
631
		 }
632
	 }
633
}
634