| @@ -1,23 +1,49 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Modules\Course\Model; |
| 4 | 4 | |
| 5 | +use FluentCommunity\App\Functions\Utility; | |
| 5 | 6 | use FluentCommunity\App\Models\Comment; |
| 6 | 7 | use FluentCommunity\App\Models\Model; |
| 7 | 8 | use FluentCommunity\App\Models\Reaction; |
| 8 | 9 | use FluentCommunity\App\Models\Term; |
| 9 | 10 | use FluentCommunity\App\Models\User; |
| 11 | +use FluentCommunity\App\Models\Meta; | |
| 10 | 12 | |
| 13 | +/** | |
| 14 | + * @property int $id | |
| 15 | + * @property int|null $user_id | |
| 16 | + * @property string|null $title | |
| 17 | + * @property string|null $slug | |
| 18 | + * @property string|null $message | |
| 19 | + * @property string|null $message_rendered | |
| 20 | + * @property string|null $type | |
| 21 | + * @property int|null $space_id | |
| 22 | + * @property string|null $privacy | |
| 23 | + * @property string|null $status | |
| 24 | + * @property string|null $featured_image | |
| 25 | + * @property int|null $is_sticky | |
| 26 | + * @property string|null $expired_at | |
| 27 | + * @property int $comments_count | |
| 28 | + * @property int $reactions_count | |
| 29 | + * @property array $meta | |
| 30 | + * @property int|null $priority | |
| 31 | + * @property string|null $scheduled_at | |
| 32 | + * @property string|null $created_at | |
| 33 | + * @property string|null $updated_at | |
| 34 | + * @property-read Course|null $course | |
| 35 | + * @property-read mixed $lessons | |
| 36 | + */ | |
| 11 | 37 | class CourseTopic extends Model |
| 12 | 38 | { |
| 13 | 39 | protected $table = 'fcom_posts'; |
| 14 | 40 | |
| 15 | - protected $guarded = ['id']; | |
| 41 | + protected $guarded = [ 'id' ]; | |
| 16 | 42 | |
| 17 | 43 | protected $casts = [ |
| 18 | 44 | 'comments_count' => 'int', |
| 19 | - 'reactions_count' => 'int' | |
| 45 | + 'reactions_count' => 'int', | |
| 20 | 46 | ]; |
| 21 | 47 | |
| 22 | 48 | protected $fillable = [ |
| 23 | 49 | 'user_id', |
| @@ -35,18 +61,18 @@ | ||
| 35 | 61 | 'comments_count', |
| 36 | 62 | 'reactions_count', |
| 37 | 63 | 'meta', |
| 38 | 64 | 'priority', |
| 39 | - 'scheduled_at' | |
| 65 | + 'scheduled_at', | |
| 40 | 66 | ]; |
| 41 | 67 | |
| 42 | 68 | protected $searchable = [ |
| 43 | 69 | 'message', |
| 44 | - 'title' | |
| 70 | + 'title', | |
| 45 | 71 | ]; |
| 46 | 72 | |
| 47 | 73 | public static $publicColumns = [ |
| 48 | - 'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'comments_count', 'reactions_count' | |
| 74 | + 'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'comments_count', 'reactions_count', | |
| 49 | 75 | ]; |
| 50 | 76 | |
| 51 | 77 | protected static $type = 'course_section'; |
| 52 | 78 | |
| @@ -73,14 +99,15 @@ | ||
| 73 | 99 | } |
| 74 | 100 | |
| 75 | 101 | public function lessons() |
| 76 | 102 | { |
| 77 | - return $this->hasMany(CourseLesson::class, 'parent_id')->orderBy('priority', 'ASC'); | |
| 103 | + return $this->hasMany(CourseLesson::class, 'parent_id')->orderBy('priority', 'ASC')->orderBy('id', 'ASC'); | |
| 78 | 104 | } |
| 79 | 105 | |
| 80 | 106 | protected static function generateNewSlug($newModel) |
| 81 | 107 | { |
| 82 | - $slug = sanitize_title($newModel->title); | |
| 108 | + $slug = Utility::slugify($newModel->title, 'topic'); | |
| 109 | + | |
| 83 | 110 | $exist = self::where('slug', $slug) |
| 84 | 111 | ->exists(); |
| 85 | 112 | |
| 86 | 113 | if ($exist) { |
| @@ -92,9 +119,9 @@ | ||
| 92 | 119 | |
| 93 | 120 | protected static function getDefaultMeta() |
| 94 | 121 | { |
| 95 | 122 | return [ |
| 96 | - 'preview_data' => null | |
| 123 | + 'preview_data' => null, | |
| 97 | 124 | ]; |
| 98 | 125 | } |
| 99 | 126 | |
| 100 | 127 | public function setMetaAttribute($value) |
| @@ -103,9 +130,9 @@ | ||
| 103 | 130 | } |
| 104 | 131 | |
| 105 | 132 | public function getMetaAttribute($value) |
| 106 | 133 | { |
| 107 | - $meta = maybe_unserialize($value); | |
| 134 | + $meta = Utility::safeUnserialize($value); | |
| 108 | 135 | |
| 109 | 136 | if (!$meta) { |
| 110 | 137 | $meta = []; |
| 111 | 138 | } |
| @@ -162,9 +189,9 @@ | ||
| 162 | 189 | return false; |
| 163 | 190 | } |
| 164 | 191 | |
| 165 | 192 | return (bool)Reaction::where('object_id', $this->id) |
| 166 | - ->select(['id']) | |
| 193 | + ->select([ 'id' ]) | |
| 167 | 194 | ->where('object_type', 'feed') |
| 168 | 195 | ->where('user_id', $userId) |
| 169 | 196 | ->where('type', $type) |
| 170 | 197 | ->first(); |
| @@ -169,8 +196,44 @@ | ||
| 169 | 196 | ->where('type', $type) |
| 170 | 197 | ->first(); |
| 171 | 198 | } |
| 172 | 199 | |
| 200 | + public function updateCustomMeta($key, $value) | |
| 201 | + { | |
| 202 | + $exist = Meta::where('object_id', $this->id) | |
| 203 | + ->where('object_type', 'course_topic') | |
| 204 | + ->where('meta_key', $key) | |
| 205 | + ->first(); | |
| 206 | + | |
| 207 | + if ($exist) { | |
| 208 | + $exist->value = $value; | |
| 209 | + $exist->save(); | |
| 210 | + } else { | |
| 211 | + Meta::create([ | |
| 212 | + 'object_id' => $this->id, | |
| 213 | + 'object_type' => 'course_topic', | |
| 214 | + 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key | |
| 215 | + 'value' => $value, | |
| 216 | + ]); | |
| 217 | + } | |
| 218 | + | |
| 219 | + return true; | |
| 220 | + } | |
| 221 | + | |
| 222 | + public function getCustomMeta($key, $default = null) | |
| 223 | + { | |
| 224 | + $exist = Meta::where('object_id', $this->id) | |
| 225 | + ->where('object_type', 'course_topic') | |
| 226 | + ->where('meta_key', $key) | |
| 227 | + ->first(); | |
| 228 | + | |
| 229 | + if ($exist) { | |
| 230 | + return $exist->value; | |
| 231 | + } | |
| 232 | + | |
| 233 | + return $default; | |
| 234 | + } | |
| 235 | + | |
| 173 | 236 | public function getHumanExcerpt($length = 40) |
| 174 | 237 | { |
| 175 | 238 | $content = $this->title; |
| 176 | 239 | |
| @@ -179,11 +242,11 @@ | ||
| 179 | 242 | if (!$content) { |
| 180 | 243 | return ''; |
| 181 | 244 | } |
| 182 | 245 | // remove all tags |
| 183 | - $content = strip_tags($content); | |
| 246 | + $content = wp_strip_all_tags($content); | |
| 184 | 247 | // remove new lines and tabs |
| 185 | - $content = str_replace(["\r", "\n", "\t"], ' ', $content); | |
| 248 | + $content = str_replace([ "\r", "\n", "\t" ], ' ', $content); | |
| 186 | 249 | // remove multiple spaces |
| 187 | 250 | $content = preg_replace('/\s+/', ' ', $content); |
| 188 | 251 | |
| 189 | 252 | // trim |
| @@ -192,9 +255,9 @@ | ||
| 192 | 255 | |
| 193 | 256 | if (!$content) { |
| 194 | 257 | return ''; |
| 195 | 258 | } |
| 196 | - | |
| 259 | + | |
| 197 | 260 | // return the first $length chars of the content with ... at the end |
| 198 | 261 | return mb_substr($content, 0, $length) . '...'; |
| 199 | 262 | } |
| 200 | 263 | |