Skip to content

Plugin pronto para o desafio #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions admin/class-wbec-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
class WBEC_Admin {

public static function init() {
add_action( 'admin_menu', 'WBEC_Admin::wbec_add_menu' );
add_action('admin_enqueue_scripts', 'WBEC_Admin::wbec_load_scripts');
}

public static function wbec_add_menu() {
add_menu_page( 'Favoritar Posts', 'Favoritar Posts', 'manage_options', 'wordpress-back-end-challenge', 'WBEC_Admin::wbec_startview', 'dashicons-star-filled' );
}

public static function wbec_load_scripts() {
wp_enqueue_script( 'wbec_js', plugins_url( 'js/admin.js', __FILE__ ), array('jquery'), rand(0,500), true );
wp_localize_script( 'wbec_js', 'wbec_var',
array(
'ajaxurl' => 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();
21 changes: 21 additions & 0 deletions admin/js/admin.js
Original file line number Diff line number Diff line change
@@ -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 );
33 changes: 33 additions & 0 deletions admin/partials/wbec-start-view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


<div class="wrap wbec-wrap">
<h2>Favoritar Posts</h2>
<h3>Plugin para favoritar posts</h3>
<p>Clique na estrela para favoritar ou desfavoritar um post</p>
<div id="painel">
<table class="widefat fixed striped margin-top-bottom15">
<thead>
<tr>
<th width="40">Fav.</th>
<th>Título</th>
<th>Data</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $post): ?>
<tr>
<td>
<a class="wbec-fav-btn" href="#" data-id="<?php echo esc_attr($post['id']); ?>">
<span class="dashicons <?php echo in_array($post['id'], $favs) ? 'dashicons-star-filled' : 'dashicons-star-empty' ?>"></span>
</a>
</td>
<td><?php echo esc_attr($post['title']['rendered']); ?></td>
<td><?php echo date("d/m/Y" , strtotime($post['date'])); ?></td>
<td><?php echo esc_attr($post['status']); ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
</div>
26 changes: 26 additions & 0 deletions inc/class-wbec-active.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
class WBEC_Active {

public static function init() {
self::installDB();
}

private static function installDB() {
global $wpdb;
$charset_collate = $wpdb->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();
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php // Silence is golden
65 changes: 65 additions & 0 deletions wordpress-back-end-challenge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Plugin Name: Wordpress back end challenge
* Plugin URI:
* Description: Plugin do desafio da apiki com a função de favoritar posts
* Version: 1.0.0
* Author: Felipe Peixoto
* Author URI: http://felipepeixoto.tecnologia.ws/
*/
if ( ! defined( 'WPINC' ) ) { die; }

define( 'WBEC_WP_VERSION', '1.0.0' );
define( 'WBEC_WP_PATH', plugin_dir_path(__FILE__) );

function wbec_activate() {
require_once plugin_dir_path( __FILE__ ) . 'inc/class-wbec-active.php';
}
register_activation_hook( __FILE__, 'wbec_activate' );

if (is_admin()){
require WBEC_WP_PATH . 'admin/class-wbec-admin.php';
}

add_action( 'wp_ajax_toggle_fav', 'wbec_toggle_fav' );
function wbec_toggle_fav(){
global $wpdb;
$id = $_POST['id'];
$table_name = $wpdb->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);
}