diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..29e9549 --- /dev/null +++ b/README.txt @@ -0,0 +1,31 @@ +=== Plugin Name === +Tags: teste, apiki +Requires at least: 3.0.1 +Tested up to: 6.0 +Stable tag: 1.0.0 +License: GPLv2 or later +License URI: http://www.gnu.org/licenses/gpl-2.0.html + +Plugin para favoritar posts + +== Description == + +Plugin do desafio da apiki com a função de favoritar posts + +== Installation == + +1. Faça upload da pasta `wordpress-back-end-challenge` para `/wp-content/plugins/` diretorio +2. Ative o plug-in através do menu "Plugins" no WordPress. +3. Compre uma chave e insira no local indicado. +4. Ative os gatilhos que desejar e personalize o teamplate de cada um. +5. Faça um teste criando um pedido .enjoy + +== Frequently Asked Questions == + +Se você tiver alguma dúvida, problema ou solicitação de alteração, por favor, me pergunte no Fórum. Vou tentar o meu melhor para resolvê-lo o mais cedo possível. Se você gostou deste plugin, por favor deixe um comentário e será muito apreciado. Obrigado. + + +== Changelog == + += 1.0 = +* Primeiro lançamento \ No newline at end of file diff --git a/admin/class-wbec-admin.php b/admin/class-wbec-admin.php new file mode 100644 index 0000000..5df9c1d --- /dev/null +++ b/admin/class-wbec-admin.php @@ -0,0 +1,39 @@ + admin_url( 'admin-ajax.php' ), + ) + ); + } + + public static function wbec_startview() { + + /*Buscar favoritos*/ + $request = new WP_REST_Request( 'GET', '/wbec/v1/favs' ); + $response = rest_do_request( $request ); + $data = rest_get_server()->response_to_data( $response, true ); + $favs = json_decode($data); + + /*Buscar Posts*/ + $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); + $response = rest_do_request( $request ); + $data = rest_get_server()->response_to_data( $response, true ); + + include_once(WBEC_WP_PATH.'admin/partials/wbec-start-view.php'); + } + +} +WBEC_Admin::init(); \ No newline at end of file diff --git a/admin/js/admin.js b/admin/js/admin.js new file mode 100644 index 0000000..6af5b02 --- /dev/null +++ b/admin/js/admin.js @@ -0,0 +1,21 @@ +(function( $ ) { + $('.wbec-fav-btn').each(function(index, el) { + $(el).click(function(event) { + var post_id = $(el).data('id'); + $(el).find('span').toggleClass('dashicons-star-filled'); + $(el).find('span').toggleClass('dashicons-star-empty'); + toggleFav(post_id); + }); + }); + function toggleFav(idPost){ + var ajaxurl = wbec_var['ajaxurl']; + $.ajax({ + type: "post", + url: ajaxurl, + data: {action: "toggle_fav",id: idPost}, + success: function(msg){ + alert('Alterado com sucesso!'); + } + }); + } +})( jQuery ); \ No newline at end of file diff --git a/admin/partials/wbec-start-view.php b/admin/partials/wbec-start-view.php new file mode 100644 index 0000000..e894df9 --- /dev/null +++ b/admin/partials/wbec-start-view.php @@ -0,0 +1,33 @@ + + +
+

Favoritar Posts

+

Plugin para favoritar posts

+

Clique na estrela para favoritar ou desfavoritar um post

+
+ + + + + + + + + + + + + + + + + + + +
Fav.TítuloDataStatus
+ + + +
+
+
\ No newline at end of file diff --git a/inc/class-wbec-active.php b/inc/class-wbec-active.php new file mode 100644 index 0000000..d6f85e2 --- /dev/null +++ b/inc/class-wbec-active.php @@ -0,0 +1,26 @@ +get_charset_collate(); + $table_name = $wpdb->prefix . 'wbec_fav'; + + $charset_collate = $wpdb->get_charset_collate(); + $sql = "CREATE TABLE IF NOT EXISTS $table_name ( + id int(11) NOT NULL AUTO_INCREMENT, + post_id int(11) NOT NULL DEFAULT 0, + PRIMARY KEY (id) + ) $charset_collate;"; + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + + dbDelta( $sql ); + } + + +} +WBEC_Active::init(); \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..e71af0e --- /dev/null +++ b/index.php @@ -0,0 +1 @@ +prefix . 'wbec_fav'; + + $result = $wpdb->get_results ( "SELECT * FROM $table_name WHERE post_id = $id"); + if(empty($result)){ + if ( $wpdb->insert( $table_name, array( + 'post_id' => $id, + ) ) === false ) { + echo 'false'; + } else { + echo 'true'; + } + } else { + $wpdb->delete($table_name, array( 'post_id' => $id )); + echo 'true'; + } + + + die(); +} + +add_action( 'rest_api_init', function() { + register_rest_route( 'wbec/v1' , '/favs', array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => 'wbec_get_favs', + 'permission_callback' => function() { return ''; } + )); +}); + +function wbec_get_favs(){ + global $wpdb; + $table_name = $wpdb->prefix . 'wbec_fav'; + $result = $wpdb->get_results ( "SELECT * FROM $table_name"); + $favs = array(); + foreach ($result as $r){ + $favs[] = $r->post_id; + } + return json_encode($favs); +} \ No newline at end of file