Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Daily Roundtable API
- * Plugin URI: https://app.codeable.io/tasks/new?preferredContractor=41145
- * Description: Exposes the daily roundtable to be fetched by other website
- * Version: 1.1.0
- * Author: Maciej Krawczyk
- * Author URI: https://app.codeable.io/tasks/new?preferredContractor=41145
- * License: MIT
- **/
- //exit if accessed directly
- defined('ABSPATH') or die();
- class Jewish_Journal_Connector {
- public function __construct() {
- //full url to production site
- $production_url = 'https://roundtable.jewishjournal.com/';
- //full url to development site
- $dev_url = 'https://dailyroundtabl.staging.wpengine.com/';
- //this is used to determine whether production or dev url will be used
- $development_server_name = 'roundtable.jewishjournal.com';
- //default to production url
- $this->jj_url = $production_url;
- //if we are on the staging site, use the dev url
- if ($_SERVER['SERVER_NAME'] === $development_server_name)
- $this->jj_url = $dev_url;
- $this->jj_url = trailingslashit( $this->jj_url );
- add_action('wp_footer', array($this, 'widget_scripts'));
- add_action('wp_head', array($this, 'widget_styles'));
- //output data for the first widget on Jewish Journal site
- add_action( 'wp_ajax_get-daily-roundtable', array($this, 'widget1_data_for_jj') );
- add_action( 'wp_ajax_nopriv_get-daily-roundtable', array($this, 'widget1_data_for_jj') );
- //output data for the second widget on Jewish Journal site
- add_action( 'wp_ajax_get-daily-roundtable-takes', array($this, 'widget2_data_for_jj') );
- add_action( 'wp_ajax_nopriv_get-daily-roundtable-takes', array($this, 'widget2_data_for_jj') );
- //fetch data from Jewish Journal for the widgets on the site
- add_action( 'wp_ajax_get-jj-widgets-data', array($this, 'widgets_ajax') );
- add_action( 'wp_ajax_nopriv_get-jj-widgets-data', array($this, 'widgets_ajax') );
- //register widgets
- add_shortcode('jewish_journal_podcasts', array($this, 'podcast_widget'));
- add_shortcode('jewish_journal_print_issue', array($this, 'print_issue_widget'));
- add_shortcode('jj_widgets_tmp', array($this, 'widgets_wrapper_shortcode'));
- $this->is_rendered = false;
- }
- //this is temporary - to display the widgets, you will most likely not need it later
- function widgets_wrapper_shortcode() {
- ob_start();
- ?><div id="mk-jj-widgets-wrapper">
- <?php echo $this->podcast_widget(); ?>
- <?php echo $this->print_issue_widget(); ?>
- </div><?php
- return ob_get_clean();
- }
- function podcast_widget() {
- $this->is_rendered = true;
- ob_start();
- ?>
- <div id="podcasts">
- <div class="inner">
- <h4><a href="<?php echo $this->jj_url; ?>podcasts">Podcast Network</a></h4>
- <div id="podcast-wrapper"><!--podcasts_content--></div>
- </div>
- </div>
- <?php
- return ob_get_clean();
- }
- function podcasts_content($podcasts) {
- $rows = $podcasts['rows'];
- ob_start();
- foreach ($rows as $row) {
- ?>
- <div class="podcast-row">
- <div class="podcast-left">
- <a href="<?php echo $row['category_url']; ?>"><img src="<?php echo $row['category_img']; ?>" /></a>
- </div>
- <div class="podcast-right">
- <div class="podcast">
- <a href="<?php echo $row['podcast_url']; ?>" title="<?php echo esc_attr($row['title']); ?>">
- <img src="<?php echo $row['podcast_img']; ?>" />
- </a>
- <h5><a href="<?php echo $row['podcast_url']; ?>"><?php echo $row['title']; ?></a></h5>
- </div>
- </div>
- </div><?php
- }
- return ob_get_clean();
- }
- function print_issue_widget() {
- $this->is_rendered = true;
- ob_start();
- ?>
- <!-- <div id="print-issue-container"> -->
- <!-- <div id="podcasts-loader-wrap"></div> -->
- <div id="print-issue">
- <div class="inner">
- <h4><a href="<?php echo $this->jj_url; ?>print">Print</a></h4>
- <a id="print-issue-wrapper" href=""><img id="print-issue-img" src="" style="display:none;" /></a>
- </div>
- </div> <!-- print -->
- <!-- </div> -->
- <?php
- return ob_get_clean();
- }
- public function widget_styles() {
- ?><style>
- /* Podcasts */
- #podcasts {
- background-color: #dfdfd5;
- background-color: #C0C0C0;
- background-color: #cdcbbd;
- box-shadow: inset 0 0 6px -2px #000000;
- padding: 15px;
- margin-bottom: 35px;
- }
- #podcasts h4 {
- width: 100%;
- margin: 0 0 30px;
- padding-bottom: 0;
- }
- #podcasts h4 a {
- text-indent: 100%;
- white-space: nowrap;
- overflow: hidden;
- background: url('<?php echo $this->jj_url.'wp-content/themes/JJ'; ?>/images/headers/header-podcasts.png') no-repeat center center;
- width: 100%;
- margin: 0 0 10px;
- padding: 0;
- background-size: contain;
- min-height: 73px;
- display: block;
- }
- #podcasts h3 .wrap:before {
- border-top: 1px dotted #8997a1;
- content: "";
- margin: 0 auto;
- /* this centers the line to the full width specified */
- position: absolute;
- /* positioning must be absolute here, and relative positioning must be applied to the parent */
- top: 50%;
- left: 0;
- right: 0;
- bottom: 0;
- width: 100%;
- z-index: -1;
- }
- #podcast-wrapper {
- min-height: 290px;
- }
- #podcasts #podcast-wrapper {
- /* display: flex;
- flex-direction: row-reverse;
- flex-wrap: wrap-reverse;
- justify-content: space-between;*/
- display: flex;
- flex-direction: column;
- justify-content: space-between;
- }
- #podcasts #podcast-wrapper .podcast-row {
- margin-bottom: 20px;
- display: inline-flex;
- flex-direction: row;
- justify-content: space-between;
- }
- #podcasts #podcast-wrapper .podcast-row:last-child {
- margin-bottom: 0;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-left {
- width: 33%;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-left a {
- display: block;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-right {
- width: 64%;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-right .podcast {
- display: flex;
- flex-direction: row;
- justify-content: space-between;
- height: 100%;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-right .podcast > a {
- display: block;
- width: 70%;
- margin-right: 8px;
- max-height: 135px;
- overflow: hidden;
- }
- #podcasts #podcast-wrapper .podcast-row .podcast-right .podcast h5 {
- width: 55%;
- margin: 0;
- font-size: 16px;
- display: inline-flex;
- align-self: center;
- }
- #podcasts #podcast-wrapper a img {
- display: block;
- width: 100%;
- height: auto;
- }
- #podcasts #podcast-wrapper a:first-child img {
- display: block;
- width: 100%;
- height: auto;
- }
- /* Print */
- #print-issue {
- background-color: #dfdfd5;
- background-color: #C0C0C0;
- background-color: #cdcbbd;
- box-shadow: inset 0 0 6px -2px #000000;
- padding: 15px;
- }
- #print-issue h4 {
- width: 100%;
- margin: 0 0 30px;
- padding-bottom: 0;
- }
- #print-issue h4 a {
- text-indent: 100%;
- white-space: nowrap;
- overflow: hidden;
- background: url('<?php echo $this->jj_url.'wp-content/themes/JJ'; ?>/images/headers/header-print.png') no-repeat center center;
- width: 100%;
- margin: 0 0 10px;
- padding: 0;
- background-size: contain;
- min-height: 71px;
- display: block;
- }
- #print-issue h3 {
- width: 100%;
- text-align: center;
- margin: 20px 0;
- font-size: 18px;
- color: #306b84;
- }
- #print-issue h3 .wrap {
- position: relative;
- z-index: 1;
- display: block;
- max-width: 60%;
- margin: 0 auto;
- }
- #print-issue h3 .wrap span {
- background-color: #dfdfd5;
- background-color: #C0C0C0;
- background-color: #cdcbbd;
- padding: 0 5px;
- }
- #print-issue h3 .wrap:before {
- border-top: 1px dotted #8997a1;
- content: "";
- margin: 0 auto;
- /* this centers the line to the full width specified */
- position: absolute;
- /* positioning must be absolute here, and relative positioning must be applied to the parent */
- top: 50%;
- left: 0;
- right: 0;
- bottom: 0;
- width: 100%;
- z-index: -1;
- }
- #print-issue img {
- width: 100%;
- height: auto;
- box-shadow: 3px 3px 5px #666;
- }
- #print-issue-wrapper {
- // min-height: 430px;
- }
- </style><?php
- }
- public function widget_scripts() {
- //exit if the shortcode was not used on this page
- if (!$this->is_rendered)
- return;
- ?><script>(function() {
- //containers where the dynamic content is inserted
- var printIssueWrapper = document.getElementById('print-issue-wrapper'),
- podcastWrapper = document.getElementById('podcast-wrapper');
- //if the widget1 is present, load its content
- if (printIssueWrapper || podcastWrapper)
- loadData();
- //used to escape data from the remote server
- function escapeHTML(s) {
- return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/\//g, "/");
- }
- function loadData() {
- jQuery.ajax({
- type: 'GET',
- url: <?php echo json_encode(admin_url( 'admin-ajax.php?action=get-jj-widgets-data' )); ?>,
- contentType: 'application/json',
- success: ajaxSuccess,
- error: ajaxError
- });
- }
- function ajaxSuccess(data) {
- if (data.error) {
- console.log('Warning: error retrieving roundtable posts: ' + data.error);
- return;
- }
- if (printIssueWrapper)
- renderPrintIssue(data.print_issue);
- if (podcastWrapper)
- renderPodcasts(data.podcasts_html);
- }
- function renderPodcasts(html) {
- podcastWrapper.innerHTML = html;
- }
- function renderPrintIssue(printIssue) {
- printIssueWrapper.href = printIssue.issue_url;
- document.getElementById('print-issue-img').src = printIssue.issue_img;
- document.getElementById('print-issue-img').style.display = '';
- return;
- widget2Container.innerHTML = data.html;
- widget2Container.style.opacity = 1;
- var loaderWrap = document.getElementById('drw2-loader-wrap')
- loaderWrap.style.opacity = 0;
- loaderWrap.addEventListener('transitionend', function(e) {
- loaderWrap.style.display = 'none';
- });
- }
- function ajaxError(jqXHR) {
- console.log('Warning: error retrieving roundtable posts');
- }
- }());
- </script><?php
- }
- //get the data from Jewish Journal to display the widgets here
- public function widgets_ajax() {
- $response = array (
- 'success' => false
- );
- header('Content-Type: application/json');
- $api_url = $this->jj_url . 'wp-admin/admin-ajax.php?action=daily-roundtable-widgets-data';
- $request = wp_remote_get( $api_url );
- if( is_wp_error( $request ) ) {
- $response['error'] = 'Unable to connect to remote server';
- echo json_encode( $response );
- wp_die();
- }
- $body = wp_remote_retrieve_body( $request );
- $data = json_decode($body, true);
- $response['success'] = true;
- //build the html from the data and the template and output it
- $response['podcasts_html'] = $this->podcasts_content($data['podcasts']);
- //output print issue data
- $response['print_issue'] = $data['print_issue'];
- echo json_encode($response);
- wp_die();
- }
- //return the current roundtable on the homepage as json
- function widget1_data_for_jj() {
- /* example output:
- {
- "success": true,
- "posts": [
- { "title": "Post 1" },
- { "title": "Post 2" },
- { "title": "Post 3" },
- { "title": "Post 4" },
- { "title": "Post 5" },
- ]
- }
- */
- $response = array (
- 'success' => false
- );
- header('Content-Type: application/json');
- //get the id of the roundtable displayed on the homepage
- //I got this code partially from the JJ Daily theme, file: page-homepage.php
- $parent_id = null;
- $parent = new WP_Query( array(
- 'post_type' => 'daily_roundtable',
- 'post_parent' => 0
- ) );
- while ( $parent->have_posts() ) {
- $parent->the_post();
- $parent_id = get_the_ID();
- }
- if (!$parent_id) {
- $response['error'] = 'Parent post not found';
- echo json_encode($response);
- wp_die();
- }
- //once we have the parent post id, we can fetch its children
- $roundtable_posts = new WP_Query( array(
- 'post_type' => 'any',
- 'posts_per_page' => 3,
- //'posts_per_page' => -1,
- 'post_parent' => $parent_id,
- 'order' => 'ASC',
- 'orderby' => 'menu_order'
- ) );
- //check if we have any posts
- if (!$roundtable_posts->have_posts()) {
- $response['error'] = 'No posts';
- echo json_encode($response);
- wp_die();
- }
- $response['posts'] = array();
- //iterate the roundtable posts
- while ( $roundtable_posts->have_posts() ) {
- $roundtable_posts->the_post();
- array_push($response['posts'], array ( 'title' => html_entity_decode(get_the_title()) ));
- }
- wp_reset_postdata();
- $response['success'] = true;
- echo json_encode($response);
- wp_die();
- }
- function widget2_data_for_jj() {
- $response = array (
- 'success' => false
- );
- header('Content-Type: application/json');
- //get the id of the roundtable displayed on the homepage
- //I got this code partially from the JJ Daily theme, file: page-homepage.php
- $parent_id = null;
- $parent = new WP_Query( array(
- 'post_type' => 'daily_roundtable',
- 'post_parent' => 0
- ) );
- while ( $parent->have_posts() ) {
- $parent->the_post();
- $parent_id = get_the_ID();
- }
- if (!$parent_id) {
- $response['error'] = 'Parent post not found';
- echo json_encode($response);
- wp_die();
- }
- //once we have the parent post id, we can fetch its children
- $roundtable_posts = new WP_Query( array(
- 'post_type' => 'any',
- 'posts_per_page' => -1,
- 'post_parent' => $parent_id,
- 'order' => 'ASC',
- 'orderby' => 'menu_order',
- 'posts_per_page' => 3
- ) );
- //check if we have any posts
- if (!$roundtable_posts->have_posts()) {
- $response['error'] = 'No posts';
- echo json_encode($response);
- wp_die();
- }
- $roundtable = array();
- //iterate the roundtable posts
- while ( $roundtable_posts->have_posts() ) {
- $roundtable_posts->the_post();
- $stories = array();
- //iterate the custom article field "sub-stories"
- while (have_rows('article')) {
- the_row();
- array_push($stories, array(
- 'title' => get_sub_field('title'),
- 'link' => get_sub_field('link'),
- 'source' => get_sub_field('source'),
- 'image' => get_sub_field('image'),
- 'summary' => get_sub_field('summary'),
- 'by_line' => get_sub_field('by_line'),
- 'quote' => get_sub_field('quote'),
- ));
- }
- array_push($roundtable, array (
- 'title' => html_entity_decode(get_the_title()),
- 'take' => get_field('our_take'),
- 'id' => get_the_ID(),
- 'stories' => $stories
- ));
- }
- wp_reset_postdata();
- $response['success'] = true;
- $response['roundtable'] = $roundtable;
- echo json_encode($response);
- wp_die();
- }
- }
- $Jewish_Journal_Connector = new Jewish_Journal_Connector();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement