From d4de0cabc1b61f95b81753e46e032e40f8970a0f Mon Sep 17 00:00:00 2001 From: mateusavila Date: Thu, 5 Sep 2024 16:44:48 -0300 Subject: [PATCH 1/3] feat: plugin favorite post --- .DS_Store | Bin 0 -> 8196 bytes .gitignore | 53 ++++ Apiki.postman_collection.json | 123 +++++++++ wp-content/.DS_Store | Bin 0 -> 6148 bytes wp-content/plugins/.DS_Store | Bin 0 -> 6148 bytes wp-content/plugins/mateus-avila-isidoro.zip | Bin 0 -> 4172 bytes .../Apiki.postman_collection.json | 123 +++++++++ .../mateus-avila-isidoro/classes/install.php | 48 ++++ .../mateus-avila-isidoro/classes/routes.php | 256 ++++++++++++++++++ .../classes/uninstall.php | 26 ++ .../mateus-avila-isidoro.php | 18 ++ 11 files changed, 647 insertions(+) create mode 100644 .DS_Store create mode 100644 .gitignore create mode 100644 Apiki.postman_collection.json create mode 100644 wp-content/.DS_Store create mode 100644 wp-content/plugins/.DS_Store create mode 100644 wp-content/plugins/mateus-avila-isidoro.zip create mode 100644 wp-content/plugins/mateus-avila-isidoro/Apiki.postman_collection.json create mode 100644 wp-content/plugins/mateus-avila-isidoro/classes/install.php create mode 100644 wp-content/plugins/mateus-avila-isidoro/classes/routes.php create mode 100644 wp-content/plugins/mateus-avila-isidoro/classes/uninstall.php create mode 100644 wp-content/plugins/mateus-avila-isidoro/mateus-avila-isidoro.php diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b3688f096cfd35f9c4abaec22a48e587351f20d2 GIT binary patch literal 8196 zcmeHM&2G~`5S~o}bs7qxDxw?^mbkX11wpB}gtR?y;L;#C04i}38mo>S#ZCjND&-2i z1FwKM@g(proZy??b!0mZKNm#RU1@im^?p0EpJ&D$mxx4t*xn#oCL#-+WwnAKr}2I6 zQ>~z9u0aami8?f(RccW{y{Xk^!Z2VMFbo(53QnL;m0UiH+nGI<^d9Q#3$x?H;t+eOCmGGy=kqW2 z@#!_JXM2(!l50=t#vknyg2x^#@@8=}%iu;`BW?jMmq`i9)e-?);oJXH~41jczb%M9pXz)|-Rp{Od^&cusuXbe_7M z(SCJlJMg2X>vskc;I=zh^5U86w}VkV82ar{VjJp)Rkq6e)vL$Hn;R?k-OZB~`*`Ed z*4B!B|K9DBld^T?=B)>N`-fi758i=%G@;*)w4i=Z#`n0&rQ@hE@V&ri2WD(#LfHV0 z=;ynP;Gj&B7Na-?wD4$$G7*FssyQf=sLNUxrvV?yhg~&*YarR=>y=@a+#%M>LBlB_ z*DYkQB~ce9dO6fscP>r_SP~a&>p_=0%f=#yQxX*?5>BO&OeH?FhWR>W5GB#E^M^7F zeK6b4M{?MRi;1wQ>QhEYAe_Vw$ZGs{5+B|KZyY}w(@my@T4#}x@o6}fub^X znd|@E+28+9&w-4kh5^ICUo#*|JB^(>VDVoM*&Vpnw$WdqbK`YmB_#xdEXN^bISx7b ihas+QEM-hh%dwJ}LHqX~0?hkAr-U)@f9Z{9XZQ&@;s&7r literal 0 HcmV?d00001 diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c36ffdfc --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# Wordpress - ignore core, configuration, examples, uploads and logs. +# https://github.com/github/gitignore/blob/main/WordPress.gitignore + +# Core +# +# Note: if you want to stage/commit WP core files +# you can delete this whole section/until Configuration. +/wp-admin/ +/wp-content/index.php +/wp-content/languages +/wp-content/plugins/akismet +/wp-content/plugins/hello.php +/wp-content/plugins/index.php +/wp-content/themes/index.php +/wp-includes/ +/index.php +/license.txt +/readme.html +/wp-*.php +/xmlrpc.php + +# Configuration +wp-config.php + +# docker +docker-compose.yml + +# Example themes +/wp-content/themes/twenty*/ + +# Example plugin +/wp-content/plugins/hello.php + +# Uploads +/wp-content/uploads/ + +# Log files +*.log + +# htaccess +/.htaccess + +# All plugins +# +# Note: If you wish to whitelist plugins, +# uncomment the next line +#/wp-content/plugins + +# All themes +# +# Note: If you wish to whitelist themes, +# uncomment the next line +#/wp-content/themes \ No newline at end of file diff --git a/Apiki.postman_collection.json b/Apiki.postman_collection.json new file mode 100644 index 00000000..1cafd620 --- /dev/null +++ b/Apiki.postman_collection.json @@ -0,0 +1,123 @@ +{ + "info": { + "_postman_id": "36fe6f32-50e4-408c-bc17-a4df3359024d", + "name": "Apiki", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "38156146" + }, + "item": [ + { + "name": "Login Teste APIKI", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "// Parseia a resposta como JSON", + "var jsonData = pm.response.json();", + "", + "// Verifica se o campo \"token\" existe na resposta", + "if (jsonData.token) {", + " // Define a variável global \"auth_token\" com o valor do token", + " pm.globals.set(\"auth_token\", jsonData.token);", + "}" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"user_login\": \"mateus\",\n \"user_password\": \"$fnzFI&w%H2PtUkjnQ\"\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/login", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "login" + ] + } + }, + "response": [] + }, + { + "name": "Favoritar Post", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{auth_token}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"post_id\": 1\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/favorite", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "favorite" + ] + } + }, + "response": [] + }, + { + "name": "Logout Teste APIKI", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{auth_token}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"user_login\": \"mateus\",\n \"user_password\": \"$fnzFI&w%H2PtUkjnQ\"\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/logoff", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "logoff" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/wp-content/.DS_Store b/wp-content/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..12ae8fdddb518b72d1223eed21013e2913ce873d GIT binary patch literal 6148 zcmeHKL2uJA6n^f?nld3(0t6Q%OI*uXK`^FWLK%l$34#Nlk}OddEsLWjU6HCvxdK0i zBmac|!U?`-ds5Q^CxlStCp~|*-}~(NYh%YmB!<&?K-42556;-WisBc>arQN9*&YrG zJ;n#xrGieWIL_9xU58V^De%83!0&FCQpzc&gqGf4brNMNLX@|Lr|{h$+E8OgMj<$3 zngYjQmd9Cx@nwb4!M>0od#vYL$rgA1oE^=|!amK{sL0Z?+S~gkT3hWa+a0gt?Rf8k zQ#A{!u$q>`aPo#*FO|xo%X%2SN|V{BcjKALt1!)zi6*3Rf|R$f(>zwQp_=A#skwm( zcwMhM>fM^p_XmCXXn)a{^TES|gT8$7_`zb)^={t1fB0f_mYwJ7Bdda45Zj=(YZfoz z6DlhbTm+LmQ~4MXW^Gf2jWeP1buqd&Lljj(%zLe2cX~LnmP`AHvQo>R8 literal 0 HcmV?d00001 diff --git a/wp-content/plugins/.DS_Store b/wp-content/plugins/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7ba6b2dd76a8ce60fe2bdacae071552c8a42f7fa GIT binary patch literal 6148 zcmeHKO;5r=5PgG0AznCm;9}B00EzMDsS=Fw%pVZMD6vw4K#ZrJ{WJbVeY3M6g<`_V z5Hr)vzWsW;)3?oT7l8DZ;|rh#pg~uxHJR)%ZC9^YE!G?pC5-WkM=b8L{9%+;dV9qo zDxhn3f+1co!5d2Jmyg(@pT#x3`yGrqpowR$a>ge|7c-V-w&H8XSA{Xb6l1d9cQfRC zi*d$ChFFlXWKI%35uE1IW-5(OVVj8h93|j}2a0MXN zSsjdd{b^KA5;2Wf7_x;XMkN|m;uS-T&iW+wOCuJBMu)`9hs2pB-cTgW&heAb9g-S) z>I%35n+hCRv8Ct#&F}aBO_G;f0axH(DIoR!px@&w#j~~Ya(dP#^n1FR_A3n66lTIv g%)WXQx9P!HpJ;}dMl1|jL-QYjAcH5az@IAc1JoX6$^ZZW literal 0 HcmV?d00001 diff --git a/wp-content/plugins/mateus-avila-isidoro.zip b/wp-content/plugins/mateus-avila-isidoro.zip new file mode 100644 index 0000000000000000000000000000000000000000..d712d836cf769b88f8aba93248ae631b5b7c27d4 GIT binary patch literal 4172 zcmb7H2{@E%8y;KCh?yy}YcL~3_I=56WSL^JFR3$w30cM(GGs>hiO`@drO3Xo4I-4` zB#k3m);QKI6~Zs4f2Q+0^>@`d=YQvV=K1FOuIsty{qFbqp8G~2nV7i%^aZg&*nhtG z?-w@!2Jk`$xCZ*mpo82!(K7D-?k+xlKC;$U901nJ`VnjcE$BHgm>s~tyvYCnd@hCh zvn0Xce@hDesHC$e+TY*R|BupKEhAV7TF~=%OS1vk0C0~lrQH|-0GS_^{@xZ&;M{PZ zcX4fG1RG5Ydj1BgEVlA|J1;JZp3P=9G6XqUTGg&Hl-A5lyHDR|a#0v{fS7EB8^ zYK^@wB9M})G+JJb590_C{-71(YitR!6Y$cuJjdjZGM>{X7v9a~Cfi&C^uN%Uck}NE zQwGJxc;YAJuj-zb&6i`FzfFRF^laW1xPSH&psMj*J4ci(3dzb=EGQ$2q8$@DVSk(w zI+cfgzEBBc0RSX^MCJY|argEQKzn-r$s%QX1lvsudj5vR1kzcbemMwba$VAFATNI) z0~QSElh3GX7ac5LQfOnA@Dq})OB=bfQVXkP)nNyRCE?|Jy$4yUM5GPRu7zK2*`0iF zrDDK>#kcjS?$JYdr}fk*0-!8b{-6a~x~Ldz!DFiEjJ0<1du_MXlZdUvyuwLN8E#02 zhR^e!#n?-3HblUACG}-Zx&h*BLkGMgN`nny%FRg*p;0e`B$c%Yk0(-9Z>Y#lhe1h#BI2W4_9A2&RL4 zn1aKy8QYnL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pS4D^ozX^9D<7a-k`oFr+71AnPai6e>e$s9=$)g8aq~ zyh`=qUZ+?BL7IJ{KHZYirhsI$#W--&l{f7P38GEcUZW-rQwz@xI(rg-Ei7boU>> zPgyBjkL+?TMtK=R3k1s#8Medan}BY3=y1rbz^Qk+Mwg88hsw{8i-qg&TPq1*I}nL)o!LeX&Y8*TZSiA2%Fg{&x(G2DSqRERZQ^}m2eP+1J~2==@^cRYu4r(hUB}wfmC4lDV*0a z{QXWT{g2D7P1NHCu0Ew^Ao_AbiLS(3&XC5bHR8t`kGq_ zbv8+sn(AFhbq(z50T+-$2@MBIe3;!?G`Y7mP?EbY7MzZZ*jg6p zT~i=jCOgzpmr4AArUI`veIKCmg~iDH&>QL#!PS=Wj%MzpH$k&}2yxfLJ>X{B<$?P? zWcQ(tHBrR0rUyo`Iq)n2T_qU%#@3{i#9MAK0BBH>jb(IE*nN_{#2hZnjq8a<+Ns zgG1o~>0WJr&0~ko(@^YlTJk7W*qNGK4m-zlS2bzsqkm z)n7P`uTkmh?Q2ayS8w|(FEW8f_+O&_Us~>KT)N`;`bg0g$NtKTJj?nwxc^*#ppYyF R=!Fl^KBcsW7{@{X^&hyax4QrU literal 0 HcmV?d00001 diff --git a/wp-content/plugins/mateus-avila-isidoro/Apiki.postman_collection.json b/wp-content/plugins/mateus-avila-isidoro/Apiki.postman_collection.json new file mode 100644 index 00000000..1cafd620 --- /dev/null +++ b/wp-content/plugins/mateus-avila-isidoro/Apiki.postman_collection.json @@ -0,0 +1,123 @@ +{ + "info": { + "_postman_id": "36fe6f32-50e4-408c-bc17-a4df3359024d", + "name": "Apiki", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "38156146" + }, + "item": [ + { + "name": "Login Teste APIKI", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "// Parseia a resposta como JSON", + "var jsonData = pm.response.json();", + "", + "// Verifica se o campo \"token\" existe na resposta", + "if (jsonData.token) {", + " // Define a variável global \"auth_token\" com o valor do token", + " pm.globals.set(\"auth_token\", jsonData.token);", + "}" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"user_login\": \"mateus\",\n \"user_password\": \"$fnzFI&w%H2PtUkjnQ\"\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/login", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "login" + ] + } + }, + "response": [] + }, + { + "name": "Favoritar Post", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{auth_token}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"post_id\": 1\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/favorite", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "favorite" + ] + } + }, + "response": [] + }, + { + "name": "Logout Teste APIKI", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{auth_token}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"user_login\": \"mateus\",\n \"user_password\": \"$fnzFI&w%H2PtUkjnQ\"\n}" + }, + "url": { + "raw": "http://localhost:8000/wp-json/api/logoff", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "wp-json", + "api", + "logoff" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/wp-content/plugins/mateus-avila-isidoro/classes/install.php b/wp-content/plugins/mateus-avila-isidoro/classes/install.php new file mode 100644 index 00000000..937a5326 --- /dev/null +++ b/wp-content/plugins/mateus-avila-isidoro/classes/install.php @@ -0,0 +1,48 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/mateusavilaisidoro + */ + +namespace MateusAvila; + +class InstallWordpressPlugin +{ + public function __construct() + { + // Usa o hook correto para ativação + register_activation_hook( MAINDIR . '/mateus-avila-isidoro.php', [ 'MateusAvila\InstallWordpressPlugin', 'create_table' ] ); + } + + /** + * Create new table when the user activates the plugin + * + * @return void apenas cria o banco + */ + public static function create_table() + { + global $wpdb; + $table = $wpdb->prefix . "favorite"; + $charset = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE IF NOT EXISTS $table ( + id BIGINT(20) NOT NULL AUTO_INCREMENT, + post_id BIGINT(20) NOT NULL, + user_id BIGINT(20) NOT NULL, + fav_date DATETIME NOT NULL, + PRIMARY KEY (id) + ) $charset;"; + + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + dbDelta( $sql ); + } +} + +$app = new InstallWordpressPlugin(); diff --git a/wp-content/plugins/mateus-avila-isidoro/classes/routes.php b/wp-content/plugins/mateus-avila-isidoro/classes/routes.php new file mode 100644 index 00000000..8e6110cf --- /dev/null +++ b/wp-content/plugins/mateus-avila-isidoro/classes/routes.php @@ -0,0 +1,256 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/mateusavilaisidoro + */ + +namespace MateusAvila; + +class RoutesWordpressPlugin +{ + + /** + * Construtor da classe. + * + */ + public function __construct() + { + add_action('send_headers', [$this, 'allow_cors']); + $this->add_default_routes(); + } + + /** + * allow CORS + * + * @return void + */ + public function allow_cors() + { + header("Access-Control-Allow-Origin: *"); + } + + /** + * Define the routes of the application + * + * @return void + */ + public function add_default_routes() + { + add_action('rest_api_init', function() { + register_rest_route('api', '/login', array( + 'methods' => 'POST', + 'callback' => [$this, 'login_user']), + ); + register_rest_route('api', '/logoff', array( + 'methods' => 'POST', + 'callback' => [$this, 'logoff_user']), + ); + register_rest_route('api', '/favorite', array( + 'methods' => 'POST', + 'callback' => [$this, 'favorite_post']) + ); + }); + } + + /** + * Make WP login + * + * @return void process the login in Rest API + */ + public function login_user() + { + $get = file_get_contents('php://input'); + $g = json_decode($get, true); + + if(empty($g['user_login'])) { + return wp_send_json(array( + "title" => "Erro!", + "text" => "É necessário preencher o username" + ), 422); + } + + if(empty($g['user_password'])) { + return wp_send_json(array( + "title" => "Erro!", + "text" => "É necessário preencher a senha" + ), 422); + } + + $creds = array( + 'user_login' => sanitize_text_field($g['user_login']), + 'user_password' => sanitize_text_field($g['user_password']), + 'remember' => true + ); + + $user = wp_signon($creds, false); + + if ( is_wp_error( $user ) ) { + return wp_send_json(array( + 'logged' => false, + "title" => "Erro!", + "text" => $user->get_error_message() + ), 422); + } + + $token = wp_generate_password(32, false); + update_user_meta($user->ID, 'auth_token', $token); + + return wp_send_json(array( + 'logged' => true, + 'message' => 'Login executado com sucesso!', + 'token' => $token + ), 200); + } + + /** + * Get the Token + * + * @return array|string|null returns the user token or null + */ + public function get_the_token() + { + $headers = getallheaders(); + $token = isset($headers['Authorization']) ? str_replace('Bearer ', '', $headers['Authorization']) : null; + + return $token; + } + + /** + * Logoff the user + * + * @return string with the logoff information + */ + public function logoff_user() + { + $token = $this->get_the_token(); + + if ($token) { + $this->invalidate_token($token); + } + + wp_logout(); + return wp_send_json(array( + 'logged' => false, + 'message' => 'Logoff executado com sucesso!' + ), 200); + } + + /** + * Favorite/unfavorite a post + * + * @return string favorite/unfavorite the post + */ + public function favorite_post($request) + { + $token = $this->get_the_token(); + if (!$token) { + return wp_send_json(array( + "title" => "Erro!", + "text" => 'Token de autenticação não fornecido' + ), 401); + } + + $user = $this->get_user_by_token($token); + if (!$user) { + return wp_send_json(array( + "title" => "Erro!", + "text" => 'Token de autenticação inválido' + ), 401); + } + + return $user; + + $get = file_get_contents('php://input'); + $g = json_decode($get, true); + + if(empty($g['post_id']) || !(is_numeric($g['post_id']))) { + return wp_send_json(array( + 'success' => false, + "title" => "Erro!", + "text" => "É necessário enviar a ID do POST" + ), 422); + } + + $get_post = get_post($g['post_id']); + if (!$get_post) { + return wp_send_json(array( + 'success' => false, + "title" => "Erro!", + "text" => "Este post não existe na nossa plataforma" + ), 422); + } + + // verificar se existe o registro + global $wpdb; + $table = $wpdb->prefix."favorite"; + $user_id = (int) $user->ID; + $post_id = (int) $g['post_id']; + + // Sanitização adicional + $user_id = sanitize_key($user_id); + $post_id = sanitize_key($post_id); + + $results = $wpdb->get_results($wpdb->prepare("SELECT id FROM $table WHERE `post_id`=%d and `user_id`=%d", $post_id, $user_id)); + + if ($results) { + $wpdb->delete($table, array('post_id' => $post_id, 'user_id' => $user_id)); + return wp_send_json(array( + 'success' => true, + "title" => "Sucesso!", + "text" => "Você desfavoritou este post" + ), 200); + } + + $wpdb->insert($table, array('post_id' => $post_id, 'user_id' => $user_id, 'fav_date' => current_time('mysql')), array('%d', '%d', '%s')); + return wp_send_json(array( + 'success' => true, + "title" => "Sucesso!", + "text" => "Você favoritou este post" + ), 200); + } + + /** + * Invalidate the given token + * + * @param string $token The token to invalidate + * @return void + */ + private function invalidate_token($token) + { + global $wpdb; + $user_id = $wpdb->get_var($wpdb->prepare( + "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'auth_token' AND meta_value = %s", + $token + )); + + if ($user_id) { + delete_user_meta($user_id, 'auth_token'); + } + } + + /** + * Create a valid token + * + * @param string $token The token to validate + * @return void + */ + private function get_user_by_token($token) + { + global $wpdb; + $user_id = $wpdb->get_var($wpdb->prepare( + "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'auth_token' AND meta_value = %s", + $token + )); + + return $user_id ? get_user_by('id', $user_id) : null; + } +} + +$app = new RoutesWordpressPlugin(); \ No newline at end of file diff --git a/wp-content/plugins/mateus-avila-isidoro/classes/uninstall.php b/wp-content/plugins/mateus-avila-isidoro/classes/uninstall.php new file mode 100644 index 00000000..51a2f47f --- /dev/null +++ b/wp-content/plugins/mateus-avila-isidoro/classes/uninstall.php @@ -0,0 +1,26 @@ +prefix."favorite"; + $sql = "DROP TABLE IF EXISTS $table"; + $wpdb->query( $sql ); + } +} + +$app = new UninstallWordpressPlugin(); \ No newline at end of file diff --git a/wp-content/plugins/mateus-avila-isidoro/mateus-avila-isidoro.php b/wp-content/plugins/mateus-avila-isidoro/mateus-avila-isidoro.php new file mode 100644 index 00000000..70a6f4fc --- /dev/null +++ b/wp-content/plugins/mateus-avila-isidoro/mateus-avila-isidoro.php @@ -0,0 +1,18 @@ + Date: Thu, 5 Sep 2024 20:15:48 -0300 Subject: [PATCH 2/3] feat: zip the plugin for further testing --- wp-content/plugins/.DS_Store | Bin 6148 -> 6148 bytes wp-content/plugins/mateus-avila-isidoro.zip | Bin 4172 -> 5983 bytes 2 files changed, 0 insertions(+), 0 deletions(-) diff --git a/wp-content/plugins/.DS_Store b/wp-content/plugins/.DS_Store index 7ba6b2dd76a8ce60fe2bdacae071552c8a42f7fa..0541ecf4a3c637ff1cb71039a27eddb00a49d0f0 100644 GIT binary patch delta 32 ocmZoMXfc@J&&awlU^gQp>t-G%QO3{3C+R0u2Il0}fk6 zG>J`~705D*`Qswx2*R#KOWOZtJ-Ed(0|3e-JU>C#&kO5?@beA0>f?fUa`VOEJlw8& z`Qj1Y0ls*f^G5-Y73NWl;vb#@gINI#;57yS;J1kuZN~5eIvn9$vbx!>!n$N*NC^QR)UCMahU8uju|04RB+mNG^Rj>}QKr1PxL$vq4`eni#k&3OA z<|7rHaDRudnU_x;J#Nw!94n@gp|2Qc*z(QtSlfI5{nPV3kuQHBgkt(UudY7S7LKWQ zi?}7}3@estoH8(ippDXxBJLWm#m(`MJ^+c>0ao|P_ueuMI&Dhe+fy^whlN(R?i%%C zxK%D!m=-EOK;3GO%7#UjtA=|{&snWI@SLvByGd$4;lotckl8y+%U z4oHKt3gm6(Cq@~o->>G0y$ki*tRjuqwL)`sR$?JUPk1s|-dJ*_gcL-8hoPLYBV3jn zz7In5p|2lPh5e1L-JJsyhCi{1BLI?c^xp26plem^OxCO6nDVYGwyJs_MlRj1B}UT5 zqBks{sXTV?2b`hntXDfgs-}Yifn+rgV#YRiJOy_*h&I&KT@aj8^JpLV3s0E+2rLbm z&zxS^MMY8@o?q*p=OoxMy*BHwc^Dw@;ay5m9NUi<@jkMe!&tV(Fc*)ltA%~!N|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 2024 12:58:17 -0300 Subject: [PATCH 3/3] fix: remove early return --- wp-content/plugins/mateus-avila-isidoro/classes/routes.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/wp-content/plugins/mateus-avila-isidoro/classes/routes.php b/wp-content/plugins/mateus-avila-isidoro/classes/routes.php index 8e6110cf..1f8bbd3c 100644 --- a/wp-content/plugins/mateus-avila-isidoro/classes/routes.php +++ b/wp-content/plugins/mateus-avila-isidoro/classes/routes.php @@ -165,8 +165,6 @@ public function favorite_post($request) ), 401); } - return $user; - $get = file_get_contents('php://input'); $g = json_decode($get, true);