GIF89a=( õ' 7IAXKgNgYvYx\%wh…hŽth%ˆs%—x¨}9®Œ©€&©‰%¶†(¹–.¹5·œD¹&Çš)ÇŸ5ǘ;Í£*È¡&Õ²)ׯ7×µ<Ñ»4ï°3ø‘HÖ§KͯT÷¨Yÿšqÿ»qÿÔFØ !ù ' !ÿ NETSCAPE2.0 , =( þÀ“pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§gª«ªE¯°¨¬ª±²Œ¹º¹E¾­”´ÂB¶¯ §Åȸ»ÑD¾¿Á•ÄÅ®° ÝH¾ÒLÀÆDÙ«D¶BÝïðÀ¾DÑÑÔTÌÍíH òGö¨A RÎڐ |¥ ٭&ºìE8œ¹kGÔAÞpx­a¶­ã R2XB®åE8I€Õ6Xî:vT)äžþÀq¦è³¥ì仕F~%xñ  4#ZÔ‰O|-4Bs‘X:= QÉ œš lºÒyXJŠGȦ|s hÏíK–3l7·B|¥$'7Jީܪ‰‡àá”Dæn=Pƒ ¤Òëí‰`䌨ljóá¯Éüv>á–Á¼5 ½.69ûϸd«­ºÀûnlv©‹ªîf{¬ÜãPbŸ  l5‘ޝpß ´ ˜3aÅùäI«O’ý·‘áÞ‡˜¾Æ‚ÙÏiÇÿ‹Àƒ #öó)pâš Þ½ ‘Ý{ó)vmÞü%D~ 6f s}ŃƒDØW Eþ`‡þ À…L8xá†ç˜{)x`X/> Ì}mø‚–RØ‘*|`D=‚Ø_ ^ð5 !_…'aä“OÚ—7âcð`D”Cx`ÝÂ¥ä‹éY¹—F¼¤¥Š?¡Õ™ n@`} lď’ÄÉ@4>ñd œ à‘vÒxNÃ×™@žd=ˆgsžG±æ ´²æud &p8Qñ)ˆ«lXD©øÜéAžHìySun jª×k*D¤LH] †¦§C™Jä–´Xb~ʪwStŽ6K,°£qÁœ:9ت:¨þªl¨@¡`‚ûÚ ».Û¬¯t‹ÆSÉ[:°=Š‹„‘Nåû”Ìî{¿ÂA ‡Rà›ÀÙ6úë°Ÿð0Ä_ ½;ÃϱîÉì^ÇÛÇ#Ëë¼ôº!±Ä˜íUîÅÇ;0L1óÁµö«p% AÀºU̬ݵ¼á%霼€‡¯Á~`ÏG¯»À× ­²± =4ªnpð3¾¤³¯­ü¾¦îuÙuµÙ®|%2ÊIÿür¦#0·ÔJ``8È@S@5ê¢ ö×Þ^`8EÜ]ý.뜃Âç 7 ú ȉÞj œ½Dç zý¸iþœÑÙûÄë!ˆÞÀl§Ïw‹*DçI€nEX¯¬¼ &A¬Go¼QföõFç°¯;é¦÷îŽêJ°îúôF5¡ÌQ|îúöXªæ»TÁÏyñêï]ê² o óÎC=öõ›ÒÓPB@ D×½œä(>èCÂxŽ`±«Ÿ–JЀ»Û á¤±p+eE0`ëŽ`A Ú/NE€Ø†À9‚@¤à H½7”à‡%B‰`Àl*ƒó‘–‡8 2ñ%¸ —€:Ù1Á‰E¸àux%nP1ð!‘ðC)¾P81lÑɸF#ˆ€{´âé°ÈB„0>±û °b¡Š´±O‚3È–Ù()yRpbµ¨E.Z‘D8ÊH@% òŒx+%Ù˜Æcü »¸˜fõ¬b·d`Fê™8èXH"ÉÈ-±|1Ô6iI, 2““¬$+](A*jÐ QTÂo‰.ÛU슬Œã„Ž`¯SN¡–¶Äåyše¯ª’­¬‚´b¦Éož œ)åyâ@Ì®3 ÎtT̉°&Ø+žLÀf"Ø-|žçÔ>‡Ðv¦Ðžì\‚ Q1)Ž@Žh#aP72”ˆ™¨$‚ !ù " , =( …7IAXG]KgNgYvYxR"k\%w]'}hŽth%ˆg+ˆs%—r.—m3šx3˜x¨}9®€&©€+¨‡7§‰%¶†(¹–.¹œD¹&ǘ;Í•&ײ)×»4ïÌ6ò§KÍ þ@‘pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§g «¬ E ±± ¨­¶°ººE Á´”·®C¬²§Ç¶Œ»ÓDÃÕƷ¯Ê±H½ºM×ÁGÚ¬D¶BËÁ½î½DÓôTÏÛßîG»ôõC×CÌ l&âž:'òtU³6ɹ#·Ø)€'Ü.6±&ëÍÈ» K(8p0N?!æ2"ÛˆNIJX>R¼ÐO‚M '¡¨2¸*Ÿþ>#n↠å@‚<[:¡Iïf’ ¤TÚ˘CdbÜÙ“[«ŽEú5MBo¤×@€`@„€Êt W-3 ¶Ÿ¡BíêäjIÝ…Eò9[T…$íêﯧ„…•s»Óȳ¹€ÅÚdc®UUρ#±Ùïldj?´í¼²`\ŽÁðÞu|3'ÖŒ]ë6 ¶S#²‡˜FKLÈ *N E´‘áäŠ$˜›eÄYD„ºq«.è촁ƒs \-ÔjA 9²õ÷å- üúM[Âx(ís÷ì®x€|í¡Ù’p¦‚ ŽkÛTÇDpE@WÜ ²Ç]kŠ1¨ þ€·Yb ÓÁ‰l°*n0 ç™—žzBdОu¾7ĉBl€â‰-ºx~|UåU‰  h*Hœ|e"#"?vpÄiŠe6^ˆ„+qâŠm8 #VÇá ‘å–ÄV„œ|Аè•m"сœn|@›U¶ÆÎž—Špb¥G¨ED”€±Úê2FÌIç? >Éxå Œ± ¡¤„%‘žjŸ‘ꄯ<Ìaà9ijÐ2˜D¦È&›†Z`‚å]wþ¼Â:ç6àB¤7eFJ|õÒ§Õ,¨äàFÇ®cS·Ê¶+B°,‘Þ˜ºNûãØ>PADÌHD¹æž«ÄÀnÌ¥}­#Ë’ë QÀÉSÌÂÇ2ÌXÀ{æk²lQÁ2«ÊðÀ¯w|2Í h‹ÄÂG€,m¾¶ë3ÐÙ6-´ÅE¬L°ÆIij*K½ÀÇqï`DwVÍQXœÚÔpeœ±¬Ñ q˜§Tœ½µƒ°Œìu Â<¶aØ*At¯lmEØ ü ôÛN[P1ÔÛ¦­±$ÜÆ@`ùåDpy¶yXvCAyåB`ŽD¶ 0QwG#¯ æš[^Äþ $ÀÓÝǦ{„L™[±úKÄgÌ;ï£S~¹ìGX.ôgoT.»åˆ°ùŸûù¡?1zö¦Ÿž:ÅgÁ|ìL¹ „®£œŠ‚à0œ]PÁ^p F<"•ç?!,ñ‡N4—…PÄ Á„ö¨Û:Tè@hÀ‹%táÿ:ø-žI<`þ‹p I….)^ 40D#p@ƒj4–؀:²‰1Øâr˜¼F2oW¼#Z†;$Q q” ‘ ÂK¦ñNl#29 !’F@¥Bh·ᏀL!—XFóLH‘Kh¤.«hE&JòG¨¥<™WN!€ÑÙÚˆY„@†>Œž19J" 2,/ &.GXB%ÌRÈ9B6¹W]’î×ÔW¥’IÎ$ ñ‹ÓŒE8YÆ ¼³™ñA5“à®Q.aŸB€&Ø©³ JÁ—! ¦t)K%tœ-¦JF bòNMxLôþ)ÐR¸Ð™‘ èÝ6‘O!THÌ„HÛ ‰ !ù ) , =( …AXKgNgYvYxR"k\%wh…hŽh%ˆg+ˆs%—r.—x3˜x¨}9®€&©€+¨Œ,©‡7§‰%¶†(¹–.¹5·&Çš)ǘ;Í•&×£*Ȳ)ׯ7×»4ï°3øÌ6ò‘HÖ§KÍ»Hó¯T÷¨Yÿ»qÿÇhÿ þÀ”pH,È¤rÉl:ŸÐ¨tJ­Z¯Ø¬vËíz¿à°xL.›Ïè´zÍn»ßð¸|N¯Ûïø¼~Ïïûÿ€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§g ª« E$±²¨ª­ · °²½$E$ÂÕ««D· Í ¿¦Ç¶¸ÌŒ¾³CÃÅÆ E ééH½MÛÂGâªD­ çBêêϾD²ÒaÀà€Š1r­ðÓ¤ ÔožzU!L˜C'¾yW½UGtäÇïÙllê0×àÂuGþ)AÀs[þ·xì ÁxO%ƒûX2ó—  P£n›R/¡ÑšHše+êDm?# —‘Ç£6¡8íJ¡ŸâDiäªM¥Ö„ôj“¬¹£5oQ7°- <‡ *´lãÓŒ2r/a!l)dÈ A™ÈE¢ôÔ͆…ð ;Ö˜c ¡%ß‚’Ùˆâ¸b½—pe~C"BíëÚHïeF2§æŠ8qb t_`urŠeü wÅu3êæPv§h•"ß`íÍxçLĹÜÖ3á  ~Öº“®›¸ÏMDfJÙ °„ÛµáWõ%§œ‚à©–‚X ÓØ)@®Ñ›Eþ´wëuÅSxb8y\mÖzœ¥§ZbºE—ÂLªÌw!y(>¡™wú=Ç|ÅÝs¢d €CÁW)HÜcC$€L Ä7„r.á\{)@ð` @ äXÈ$PD” `šaG:§æˆOˆ72EÐamn]ù"ŒcÊxÑŒ° &dR8`g«iÙŸLR!¦P …d’ä¡“¦ðÎTƒ¦ià|À _ ¥ Qi#¦Šg›Æ ›noMµ ›V ã£)p ç£ÎW…š=Âeªk§†j„ ´®1ß²sÉxéW«jšl|0¯B0Û, \jÛ´›6±¬¶C ÛíWþï|ëÙ‹¸ñzĸV {ì;Ýñn¼òVˆm³I¼³.Ðã¤PN¥ ²µ¼„µCã+¹ÍByî£Ñ¾HŸ›ëê 7ìYÆFTk¨SaoaY$Dµœìï¿Ã29RÈkt Çïfñ ÇÒ:ÀÐSp¹3ÇI¨â¥DZÄ ü9Ïýögñ½­uÔ*3)O‘˜Ö[_hv ,àî×Et Ÿé¶BH€ Õ[ü±64M@ÔSÌM7dÐl5-ÄÙU܍´©zߌ3Ô€3ž„ „ ¶ÛPô½5×g› êÚ˜kN„Ý…0Îj4€Ìë°“#{þÕ3S2çKÜ'ợlø¼Ú2K{° {Û¶?žm𸧠ËI¼nEò='êüóºè^üæÃ_Û=°óž‚ì#Oý¿Í'¡½áo..ÏYìnüñCœO±Áa¿¢Kô½o,üÄËbö²çºíï{ËC Ú— "”Ï{ËK ÍÒw„õ±Oz dÕ¨à:$ ƒô—«v»] A#ð «€¿šéz)Rx׿ˆ¥‚d``èw-îyÏf×K!ð€þ­Ð|ìPľ„=Ì`ý(f” 'Pa ¥ÐBJa%Ðâf§„%Š¡}FàáÝ×6>ÉäŠG"éŽè=ø!oа^FP¼Ø©Q„ÀCÙÁ`(Ž\ÄÝ® ©Â$<n@dÄ E#ììUÒI! ‚#lù‹`k¦ÐÇ'Rró’ZýNBÈMF Í[¤+‹ðɈ-áwj¨¥þ8¾rá ,VÂh„"|½œ=×G_¦Ñ™EØ 0i*%̲˜Æda0mV‚k¾)›;„&6 p>ÓjK “¦Ç# âDÂ:ûc?:R Ó¬fÞéI-Ì“•Ã<ä=™Ï7˜3œ¨˜c2ŒW ,ˆ”8(T™P‰F¡Jhç"‚ ; 403WebShell
403Webshell
Server IP : 172.67.177.218  /  Your IP : 216.73.216.142
Web Server : LiteSpeed
System : Linux premium229.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64
User : akhalid ( 749)
PHP Version : 8.3.22
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home/akhalid/dangroup.sa/wp-content/plugins/elementor-pro/modules/notes/data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/akhalid/dangroup.sa/wp-content/plugins/elementor-pro/modules/notes/data/controller.php
<?php
namespace ElementorPro\Modules\Notes\Data;

use Elementor\Core\Utils\Collection;
use ElementorPro\Modules\Notes\Data\Endpoints\Users_Endpoint;
use ElementorPro\Modules\Notes\Database\Transformers\User_Transformer;
use ElementorPro\Modules\Notes\Utils;
use Elementor\Data\V2\Base\Controller as Base_Controller;
use Elementor\Data\V2\Base\Exceptions\Data_Exception;
use Elementor\Data\V2\Base\Exceptions\Error_404;
use ElementorPro\Data\Http_Status;
use ElementorPro\Modules\Notes\Data\Endpoints\Read_Status_Endpoint;
use ElementorPro\Modules\Notes\Data\Endpoints\Summary_Endpoint;
use ElementorPro\Modules\Notes\Database\Models\Note;
use ElementorPro\Modules\Notes\Database\Models\User;
use ElementorPro\Modules\Notes\Database\Query\Note_Query_Builder;
use ElementorPro\Modules\Notes\Notifications\User_Mentioned_Notification;
use ElementorPro\Modules\Notes\Notifications\User_Replied_Notification;
use ElementorPro\Modules\Notes\Notifications\User_Resolved_Notification;
use ElementorPro\Modules\Notes\User\Capabilities;
use ElementorPro\Plugin;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Controller extends Base_Controller {
	public function get_name() {
		return 'notes';
	}

	public function __construct() {
		parent::__construct();

		$this->user_transformer = new User_Transformer();
	}

	public function register_endpoints() {
		$this->register_endpoint( new Read_Status_Endpoint( $this ) );
		$this->register_endpoint( new Summary_Endpoint( $this ) );
		$this->register_endpoint( new Users_Endpoint( $this ) );

		$this->index_endpoint->register_item_route( \WP_REST_Server::READABLE, [
			'id' => [
				'type' => 'integer',
				'description' => 'Note ID to find.',
				'required' => true,
			],
		] );

		$this->index_endpoint->register_items_route( \WP_REST_Server::CREATABLE, [
			'post_id' => [
				'type' => 'integer',
				'description' => 'The id of the post where the note was created at (can be template, post, page, etc.).',
				'required' => true,
				'validate_callback' => function ( $value ) {
					return Plugin::elementor()->documents->get( $value );
				},
			],
			'element_id' => [
				'type' => 'string',
				'description' => 'Each note must be attached to an elementor element.',
				'required' => true,
				'sanitize_callback' => function( $value ) {
					return trim( $value );
				},
				'validate_callback' => function ( $value ) {
					return (bool) preg_match( '/^[a-z0-9]{7,9}$/', $value );
				},
			],
			'content' => [
				'type' => 'string',
				'description' => 'The content of the note.',
				'required' => true,
				'sanitize_callback' => function ( $value ) {
					return $this->sanitize_content( $value );
				},
				'validate_callback' => function ( $value ) {
					return ! empty( $value );
				},
			],
			'position' => [
				'type' => 'object',
				'properties' => [
					'x' => [
						'required' => true,
						'type' => 'number',
					],
					'y' => [
						'required' => true,
						'type' => 'number',
					],
				],
				'required' => true,
				'description' => 'The position of the note.',
			],
			'mentioned_usernames' => [
				'type' => 'array',
				'description' => 'List of user names that have been mentioned in the note\'s content.',
				'default' => [],
				'items' => [
					'type' => 'string',
					'sanitize_callback' => function ( $value ) {
						return wp_strip_all_tags( $value, true );
					},
				],
				'required' => false,
			],
			'route_post_id' => [
				'description' => 'The ID of the post that\'s associated with the route (doesn\'t always exist, e.g: home page, archive)',
				'required' => false,
				'validate_callback' => function ( $value ) {
					if ( ! $value ) {
						return true;
					}

					return is_numeric( $value ) && Plugin::elementor()->documents->get( $value );
				},
				'sanitize_callback' => function ( $value ) {
					if ( ! $value ) {
						return null;
					}

					return intval( $value );
				},
			],
			'route_url' => [
				'type' => 'string',
				'description' => 'The URL of the route where the note was created at.',
				'required' => false,
				'validate_callback' => function ( $value ) {
					return Utils::validate_url_or_relative_url( $value );
				},
				'sanitize_callback' => function ( $value ) {
					return Utils::clean_url( $value );
				},
			],
			'route_title' => [
				'type' => 'string',
				'description' => 'The title of the route where the note was created at.',
				'required' => false,
				'sanitize_callback' => function ( $value ) {
					return wp_strip_all_tags( $value, true );
				},
			],
			'parent_id' => [
				'type' => 'integer',
				'description' => 'If the new note is a reply to another note, the parent_id should be the thread\'s id.',
				'required' => false,
				'default' => 0,
			],
			'is_public' => [
				'type' => 'boolean',
				'description' => 'Should this note be visible for everyone or just for its author.',
				'required' => false,
			],
		] );

		$this->index_endpoint->register_item_route( \WP_REST_Server::EDITABLE, [
			'id' => [
				'type' => 'integer',
				'description' => 'The id the note.',
				'required' => true,
			],
			'content' => [
				'type' => 'string',
				'description' => 'The content of the note.',
				'required' => false,
				'sanitize_callback' => function ( $value ) {
					return $this->sanitize_content( $value );
				},
			],
			'mentioned_usernames' => [
				'type' => 'array',
				'description' => 'List of user names that have been mentioned in the note\'s content.',
				'items' => [
					'type' => 'string',
					'sanitize_callback' => function ( $value ) {
						return wp_strip_all_tags( $value, true );
					},
				],
				'required' => false,
			],
			'status' => [
				'type' => 'string',
				'description' => 'Note status can be draft or publish.',
				'required' => false,
				'enum' => [
					Note::STATUS_PUBLISH,
					Note::STATUS_DRAFT,
				],
			],
			'is_public' => [
				'type' => 'boolean',
				'description' => 'Should this note be visible for everyone or just for its author.',
				'required' => false,
			],
			'is_resolved' => [
				'type' => 'boolean',
				'description' => 'Is this note resolved and should be hidden.',
				'required' => false,
			],
		] );

		$this->index_endpoint->register_item_route( \WP_REST_Server::DELETABLE, [
			'id' => [
				'type' => 'integer',
				'description' => 'The id of the note.',
				'required' => true,
			],
			'force' => [
				'type' => 'boolean',
				'description' => 'Determine if it should be deleted permanently or change the status to trash.',
				'default' => false,
				'required' => false,
			],
		] );
	}

	/**
	 * Notes index route params.
	 *
	 * @return array[]
	 */
	public function get_collection_params() {
		return [
			'route_url' => [
				'type' => 'string',
				'description' => 'The URL of the route where the note was created at.',
				'required' => false,
				'validate_callback' => function ( $value ) {
					return Utils::validate_url_or_relative_url( $value );
				},
				'sanitize_callback' => function ( $value ) {
					return Utils::clean_url( $value );
				},
			],
			'status' => [
				'type' => 'string',
				'description' => 'The note status (e.g. "publish", "draft").',
				'required' => false,
				'enum' => [
					Note::STATUS_PUBLISH,
					Note::STATUS_DRAFT,
				],
				'default' => Note::STATUS_PUBLISH,
			],
			'is_resolved' => [
				'type' => 'boolean',
				'description' => 'Whether the note is resolved or not.',
				'required' => false,
			],
			'parent_id' => [
				'type' => 'integer',
				'description' => 'The note\'s parent id (use 0 for top-level).',
				'required' => false,
			],
			'post_id' => [
				'type' => 'integer',
				'description' => 'The ID of the post that the note is attached to.',
				'required' => false,
				'validate_callback' => function ( $value ) {
					return Plugin::elementor()->documents->get( $value );
				},
			],
			'only_unread' => [
				'type' => 'boolean',
				'description' => 'Show only unread notes (represents an unread thread if one of its replies is unread).',
				'required' => false,
			],
			'only_relevant' => [
				'type' => 'boolean',
				'description' => 'Show only notes that are relevant to the current user.',
				'required' => false,
			],
			'order_by' => [
				'type' => 'string',
				'description' => 'A column to order the results by.',
				'required' => false,
				'default' => 'last_activity_at',
				'enum' => [
					'last_activity_at',
					'created_at',
				],
			],
			'order' => [
				'type' => 'string',
				'description' => 'Results order direction.',
				'required' => false,
				'default' => 'desc',
				'enum' => [
					'asc',
					'desc',
				],
			],
		];
	}

	/**
	 * Get all Notes by filters.
	 *
	 * GET `/notes`
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return array
	 */
	public function get_items( $request ) {
		$user_id = get_current_user_id();

		$notes_query = Note::query()
			->with_replies_count()
			->with_unread_replies_count( $user_id )
			->with_is_read( $user_id )
			->with_author()
			->only_visible( $user_id )
			->order_by(
				$request->get_param( 'order_by' ),
				$request->get_param( 'order' )
			);

		foreach ( $this->get_filters() as $param => $callback ) {
			if ( $request->has_param( $param ) ) {
				call_user_func( $callback, $notes_query, $request->get_param( $param ) );
			}
		}

		$notes = $notes_query->get()->filter( function ( Note $note ) {
			return current_user_can( Capabilities::READ_NOTES, $note );
		} )->map( function ( Note $note ) {
			return $this->transform_users( $note );
		} );

		return [
			'data' => $notes,
			'meta' => [],
		];
	}

	/**
	 * Get a single note.
	 *
	 * GET `/notes/{id}`
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return array
	 */
	public function get_item( $request ) {
		$user_id = get_current_user_id();

		/**
		 * @var $note Note|null
		 */
		$note = Note::query()
			->where( 'id', '=', $request->get_param( 'id' ) )
			->with_replies( function ( Note_Query_Builder $q ) use ( $user_id ) {
				$q->with_author()->with_is_read( $user_id )->with_readers();
			} )
			->with_replies_count()
			->with_unread_replies_count( $user_id )
			->with_is_read( $user_id )
			->with_author()
			->with_readers()
			->with_document()
			->first();

		if ( ! $note ) {
			throw new Error_404();
		}

		$note = $this->transform_users( $note );
		$note->attach_user_capabilities( $user_id );

		return [
			'data' => $note,
			'meta' => [],
		];
	}

	/**
	 * Run all user models in the note through user transformer.
	 *
	 * @param Note $note
	 *
	 * @return Note
	 */
	protected function transform_users( Note $note ) {
		if ( ! empty( $note->author ) ) {
			$note->author = $this->user_transformer->transform( $note->author );
		}

		if ( ! $note->readers->is_empty() ) {
			$note->readers = $note->readers->map( function ( User $user ) {
				return $this->user_transformer->transform( $user );
			} );
		}

		// If the note has replies, recursively run the function for each reply note.
		if ( ! $note->replies->is_empty() ) {
			$note->replies = $note->replies->map( function ( Note $reply ) {
				return $this->transform_users( $reply );
			} );
		}

		return $note;
	}

	/**
	 * Create a note.
	 *
	 * POST `/notes`
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return array
	 * @throws \Exception
	 */
	public function create_items( $request ) {
		$this->validate_create_items( $request );

		$now = gmdate( 'Y-m-d H:i:s' );

		$values = ( new Collection( $request->get_body_params() ) )
			->only( [
				'post_id',
				'element_id',
				'content',
				'route_post_id',
				'route_url',
				'route_title',
				'status',
				'parent_id',
				'is_public',
			] )
			->merge( [
				'author_id' => get_current_user_id(),
				'created_at' => $now,
				'updated_at' => $now,
				'last_activity_at' => $now,
				'position' => wp_json_encode( $request->get_param( 'position' ) ),
			] )
			->all();

		$id = Note::query()->insert( $values );

		/** @var Note $note */
		$note = Note::query()->with_author()->find( $id );

		$note = $this->transform_users( $note );

		$mentioned = $note->sync_mentions(
			$request->get_param( 'mentioned_usernames' ),
			'user_nicename'
		);

		// Set the note as read by its author.
		$note->add_readers( [ get_current_user_id() ] );

		// If it's a reply, the thread's `last_activity_at` should be updated as well.
		if ( $note->is_reply() ) {
			Note::query()
				->where( 'id', '=', $note->parent_id )
				->update( [ 'last_activity_at' => $now ] );
		}

		// TODO: Use events system.
		$this->on_note_created( [
			'note' => $note,
			'mentioned' => $mentioned,
			'actor' => User::from_wp_user( wp_get_current_user() ),
		] );

		return [
			'data' => $note,
			'meta' => [],
		];
	}

	/**
	 * Update a note.
	 *
	 * PATCH `/notes/{id}`
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return array
	 */
	public function update_item( $request ) {
		$this->validate_update_items( $request );

		$now = gmdate( 'Y-m-d H:i:s' );

		$values = ( new Collection( $request->get_params() ) )
			->only( [
				'content',
				'status',
				'is_public',
				'is_resolved',
			] )
			->merge( [
				'updated_at' => $now,
			] )
			->merge( $request->has_param( 'is_resolved' ) ? [
				'last_activity_at' => $now,
			] : [] )
			->all();

		Note::query()
			->where( 'id', '=', $request->get_param( 'id' ) )
			->update( $values );

		// Need to refetch the note after update
		/** @var Note $note */
		$note = Note::query()->with_author()->find( $request->get_param( 'id' ) );

		if ( $request->has_param( 'mentioned_usernames' ) ) {
			$mentioned = $note->sync_mentions(
				$request->get_param( 'mentioned_usernames' ),
				'user_nicename'
			);
		}

		// TODO: Use events system.
		$this->on_note_updated( [
			'note' => $note,
			'actor' => User::from_wp_user( wp_get_current_user() ),
			'mentioned' => isset( $mentioned ) ? $mentioned : null,
			'resolved' => ! ! $request->get_param( 'is_resolved' ),
		] );

		return [
			'data' => $note,
			'meta' => [],
		];
	}

	/**
	 * Delete a note.
	 *
	 * DELETE `/notes/{id}`
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @return \WP_REST_Response
	 * @throws \Elementor\Data\V2\Base\Exceptions\Error_404
	 */
	public function delete_item( $request ) {
		/** @var Note $note */
		$note = Note::query()->find( $request->get_param( 'id' ) );

		if ( ! $note ) {
			throw new Error_404();
		}

		Note::query()
			->where( 'id', '=', $note->id )
			->when(
				$request->get_param( 'force' ),
				function ( Note_Query_Builder $query ) {
					$query->delete( true );
				},
				function ( Note_Query_Builder $query ) {
					$query->trash();
				}
			);

		// TODO: Should return status 204 when the $e.data will support it
		return new \WP_REST_Response( [], Http_Status::OK );
	}

	/**
	 * @inheritDoc
	 */
	public function get_permission_callback( $request ) {
		$capability = null;
		$id = $request->get_param( 'id' );

		switch ( $request->get_method() ) {
			case 'GET':
				$capability = Capabilities::READ_NOTES;
				break;
			case 'POST':
				// When creating a note it checks if the user can create note for the parent note if 'parent_id' is provided.
				$id = $request->get_param( 'parent_id' );
				$capability = Capabilities::CREATE_NOTES;
				break;
			case 'PUT':
			case 'PATCH':
				$capability = Capabilities::EDIT_NOTES;
				break;
			case 'DELETE':
				$capability = Capabilities::DELETE_NOTES;
				break;
		}

		return $capability && current_user_can( $capability, $id );
	}

	/**
	 * Get the Notes filters.
	 *
	 * @return array
	 */
	public function get_filters() {
		return [
			'route_url' => function ( Note_Query_Builder $q, $url ) {
				$q->where( 'route_url', '=', $url );
			},
			'is_resolved' => function ( Note_Query_Builder $q, $is_resolved ) {
				$q->where( 'is_resolved', '=', $is_resolved );
			},
			'parent_id' => function ( Note_Query_Builder $q, $parent_id ) {
				$q->where( 'parent_id', '=', $parent_id );
			},
			'post_id' => function ( Note_Query_Builder $q, $post_id ) {
				$q->where( 'post_id', '=', $post_id );
			},
			'only_unread' => function ( Note_Query_Builder $q ) {
				$q->only_unread( get_current_user_id() );
			},
			'only_relevant' => function ( Note_Query_Builder $q ) {
				$q->only_relevant( get_current_user_id() );
			},
		];
	}

	/**
	 * Validates the create items endpoint.
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @throws Data_Exception
	 * @throws Error_404
	 */
	private function validate_create_items( \WP_REST_Request $request ) {
		$parent_id = $request->get_param( 'parent_id' );

		if ( ! $parent_id ) {
			// The validation is related only if the new note should be reply.
			return;
		}

		/** @var Note $parent */
		$parent = Note::query()->find( $parent_id );

		if ( ! $parent ) {
			throw new Error_404();
		}

		if ( $parent->is_reply() ) {
			throw new Data_Exception(
				'Cannot create reply on reply.',
				'rest_invalid_param',
				[ 'status' => Http_Status::BAD_REQUEST ]
			);
		}

		if ( $request->has_param( 'is_public' ) ) {
			throw new Data_Exception(
				"Cannot update 'is_public' on reply.",
				'rest_invalid_param',
				[ 'status' => Http_Status::BAD_REQUEST ]
			);
		}
	}

	/**
	 * Validates the update item endpoint.
	 *
	 * @param \WP_REST_Request $request
	 *
	 * @throws Data_Exception
	 * @throws Error_404
	 */
	private function validate_update_items( \WP_REST_Request $request ) {
		/** @var Note $note */
		$note = Note::query()->find( $request->get_param( 'id' ) );

		if ( ! $note ) {
			throw new Error_404();
		}

		$has_invalid_reply_attributes = $request->has_param( 'is_resolved' ) || $request->has_param( 'is_public' );

		if ( $note->is_reply() && $has_invalid_reply_attributes ) {
			throw new Data_Exception(
				"Cannot update 'is_resolved' or 'is_public' on reply.",
				'rest_invalid_param',
				[ 'status' => Http_Status::BAD_REQUEST ]
			);
		}

		// For notifications - To make sure that there are no redundant resolve notifications.
		if ( $note->is_resolved === $request->get_param( 'is_resolved' ) ) {
			throw new Data_Exception(
				"'is_resolved' was already set on '{$note->is_resolved}'.",
				'rest_invalid_param',
				[ 'status' => Http_Status::BAD_REQUEST ]
			);
		}
	}

	/**
	 * Handle note creation side-effects.
	 *
	 * @param array $event
	 *
	 * @return void
	 */
	protected function on_note_created( array $event ) {
		foreach ( $event['mentioned'] as $user ) {
			$user->notify( new User_Mentioned_Notification( $event['note'], $event['actor'] ) );
		}

		if ( $event['note']->is_reply() ) {
			$relevant = User::query()->only_relevant_to_note( $event['note'] )->get();

			foreach ( $relevant as $user ) {
				$user->notify( new User_Replied_Notification(
					$event['note'],
					$event['actor'],
					$event['mentioned']->pluck( 'ID' )->all()
				) );
			}
		}
	}

	/**
	 * Handle note update side-effects.
	 *
	 * @param array $event
	 *
	 * @return void
	 */
	protected function on_note_updated( array $event ) {
		if ( ! empty( $event['mentioned'] ) ) {
			foreach ( $event['mentioned'] as $user ) {
				$user->notify( new User_Mentioned_Notification( $event['note'], $event['actor'] ) );
			}
		}

		if ( ! empty( $event['resolved'] ) ) {
			$relevant = User::query()->only_relevant_to_note( $event['note'] )->get();

			foreach ( $relevant as $user ) {
				$user->notify( new User_Resolved_Notification(
					$event['note'],
					$event['actor']
				) );
			}
		}
	}

	/**
	 * Sanitize note content.
	 *
	 *  - Trims empty lines & spaces from start/end of the string.
	 *  - Encodes HTML entities.
	 *
	 * @param string $raw_content
	 *
	 * @return string
	 */
	private function sanitize_content( $raw_content ) {
		return htmlentities( preg_replace( '/(^[\n\s]+|[\n\s]+$)/', '', $raw_content ) );
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit