Skip to content

Instantly share code, notes, and snippets.

@phpedinei
Created March 7, 2018 07:45
Show Gist options
  • Select an option

  • Save phpedinei/3ce851d23748e1be3b332ec175486018 to your computer and use it in GitHub Desktop.

Select an option

Save phpedinei/3ce851d23748e1be3b332ec175486018 to your computer and use it in GitHub Desktop.
re
<?php
/**
* The admin-specific functionality of the plugin.
*
* @link bruno.briante.net
* @since 1.0.0
*
* @package Firebase_Bridge
* @subpackage Firebase_Bridge/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package Firebase_Bridge
* @subpackage Firebase_Bridge/admin
* @author Bruno Briante <bruno@briante.net>
*/
class Firebase_Bridge_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The firebase instance.
*
* @since 1.0.0
* @access private
* @var FirebaseFactory $firebase The firebase instance.
*/
private $firebase;
/**
* Firebase root namespace.
*
* @since 1.0.0
* @access private
* @var string $namespace Firebase root namespace.
*/
private $namespace;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version, $namespace ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->namespace = $namespace;
$this->firebase = ( new \Firebase\Factory())
->withCredentials( plugin_dir_path( dirname( __FILE__ ) ) . 'credentials.json' )
->create();
}
public function send_data( $post_id )
{
$post = get_post ( $post_id );
$meta = get_post_meta ( $post_id );
$openingHours = get_post_meta($post_id, '_content_field_' . 10, true);
$data = [
"ID" => $post->ID,
"update" => $post->post_modified,
"slug" => $post->post_name,
"name" => $post->post_title,
"description" => $post->post_content,
"category" => [],
"featured" => $meta['_content_field_9'][0],
"subfeatured" => $meta['_content_field_14'][0],
"phoneNumber" => $meta['_content_field_6'][0],
"email" => $meta['_content_field_8'][0],
"address" => $meta['_address_line_1'][0],
"zip" => $meta['_zip_or_postal_index'][0],
"info" => $meta['_additional_info'][0],
"officeLocation" => $meta['_map_coords_1'][0] . "," . $meta['_map_coords_2'][0],
"zoom" => $meta['_map_zoom'][0],
"logo" => str_replace("www.embudasartes.net", "d3e9k87lkrgy8.cloudfront.net", wp_get_attachment_url( $meta['_attached_image_as_logo'][0] )),
"mapdata" => [
"annotations" => [
"0" => [
"latitude" => $meta['_map_coords_1'][0],
"longitude" => $meta['_map_coords_2'][0],
"title" => $post->post_title
]
]
],
"openhours" => [
'days'=> [],
'zone' => 0
],
"pictures" => [],
"openHours_24h" => $meta['_content_field_11'][0],
"openHours_sem_horario" => $meta['_content_field_12'][0]
];
foreach( wp_get_post_terms( $post_id, 'w2dc-category' ) as $category )
{
array_push($data['category'], $category->term_id . "-94d6-4fec-a1c3-c48d80b888t2");
}
foreach( $meta['_attached_image'] as $image )
{
$data['pictures'][] = str_replace("www.embudasartes.net", "d3e9k87lkrgy8.cloudfront.net", wp_get_attachment_url( $image )) ;
}
$data['tags'] = [];
foreach( wp_get_post_terms( $post_id, 'w2dc-tag' ) as $tag ) {
array_push($data['tags'], $tag->name);
}
$daysPrefix = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
foreach ($daysPrefix as $day => $prefixKey) {
$data['openhours']['days'][$day] = [
'closed' => $openingHours[$prefixKey . '_closed'],
'openAt' => strtotime($openingHours[$prefixKey . '_from'] . ' + 3 hours'),
'closeAt' => strtotime($openingHours[$prefixKey . '_to'] . ' + 3 hours'),
'day' => $day
];
}
$database = $this->firebase->getDatabase();
$database->getReference( $this->namespace . '/' . $post->ID )->set( $data );
foreach( wp_get_post_terms( $post_id, 'w2dc-category' ) as $category ) {
$database->getReference( 'categories' . '/' . $category->term_id . "-94d6-4fec-a1c3-c48d80b888t2" )->set([
"ID" => $category->term_id,
"slug" => $category->slug,
"title" => $category->name,
"thumb" => $category->description,
"icon" => "https://www.embudasartes.net/wp-content/plugins/directory-plugin/resources/images/categories_icons/icon-" . $category->slug . ".png",
"parent" => ( ( $category->parent != 0 ) ? ( $category->parent . "-94d6-4fec-a1c3-c48d80b888t2" ) : null )
]);
}
foreach( wp_get_post_terms( $post_id, 'w2dc-tag' ) as $tag ) {
$database->getReference( 'tags' . '/' . $tag->term_id )->set([
"ID" => $tag->term_id,
"slug" => $tag->slug,
"title" => $tag->name,
"group" => $tag->term_group
]);
}
}
public function remove_data ( $post_id )
{
global $post_type;
if ( $post_type != 'w2dc_listing' ) return;
$database = $this->firebase->getDatabase();
$database->getReference( $this->namespace . '/' . $post_id )->remove();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment