Skip to content

Commit

Permalink
Merge pull request #14 from antistatique/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
knuch authored Jun 12, 2018
2 parents 1ea3585 + 16079ef commit 3812b0f
Show file tree
Hide file tree
Showing 66 changed files with 740 additions and 151 deletions.
229 changes: 229 additions & 0 deletions EPFL-plugin-news/EPFL-news-2018.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php
/**
* Plugin Name: EPFL News shortcode
* Description: provides a shortcode to display news feed
* @version: 1.0
* @copyright: Copyright (c) 2017 Ecole Polytechnique Federale de Lausanne, Switzerland
*/

declare(strict_types=1);

require_once('EPFL-news-utils.php');

define("NEWS_API_URL", "https://actu.epfl.ch/api/v1/channels/");
define("NEWS_API_URL_IFRAME", "https://actu.epfl.ch/webservice_iframe/");

/**
* Returns the number of news according to the template
* @param $template: id of template
* @return the number of news to display
*/
function epfl_news_get_limit(string $template): int
{
switch ($template):
case "1":
case "7":
$limit = 1;
break;
case "3":
$limit = 4;
break;
case "2":
case "4":
case "6":
$limit = 3;
break;
case "8":
$limit = 5;
break;
default:
$limit = 3;
endswitch;
return $limit;
}

/**
* Build api URL of news
*
* @param $channel: id of news channel
* @param $template: id of template
* @param $lang: lang of news
* @param $category: id of news category
* @param $themes: The list of news themes id. For example: 1,2,5
* @return the api URL of news
*/
function epfl_news_build_api_url(
string $channel,
string $template,
string $lang,
string $category,
string $themes
): string
{
// returns the number of news according to the template
$limit = epfl_news_get_limit($template);

// define API URL
$url = NEWS_API_URL . $channel . '/news/?format=json&lang=' . $lang . '&limit=' . $limit;

// filter by category
if ($category !== '') {
$url .= '&category=' . $category;
}

// filter by themes
if ($themes !== '') {
$themes = explode(',', $themes);
foreach ($themes as $theme) {
$url .= '&themes=' . $theme;
}
}
return $url;
}

/**
* Check the required parameters
*
* @param $channel: id of channel
* @param $lang: lang of news (fr or en)
* @return True if the required parameters are right.
*/
function epfl_news_check_required_parameters(string $channel, string $lang): bool {

// check lang
if ($lang !== "fr" && $lang !== "en" ) {
return FALSE;
}

// check channel
if ($channel === "") {
return FALSE;
}

// check that the channel exists
$url = NEWS_API_URL . $channel;
$channel_response = NewsUtils::get_items($url);
if ($channel_response->detail === "Not found.") {
return FALSE;
}
return TRUE;

}

/**
* Main function of shortcode
*
* @param $atts: attributes of the shortcode
* @param $content: the content of the shortcode. Always empty in our case.
* @param $tag: the name of shortcode. epfl_news in our case.
*/
function epfl_news_process_shortcode(
array $atts,
string $content = '',
string $tag
): string {

// extract shortcode paramepfl_newseter
$atts = extract(shortcode_atts(array(
'channel' => '',
'lang' => '',
'template' => '',
'stickers' => '',
'category' => '',
'themes' => '',
), $atts, $tag));

$url = epfl_news_build_api_url(
$channel,
$template,
$lang,
$category,
$themes
);

$actus = NewsUtils::get_items($url);
//NewsUtils::debug($actus->results);

ob_start();
do_action('epfl_shortcode_news', $actus->results);
//return the result of output buffering to display shortcode correctly
return ob_get_clean();

}

add_action( 'init', function() {

// define the shortcode
add_shortcode('epfl_news_2018', 'epfl_news_process_shortcode');

if ( function_exists( 'shortcode_ui_register_for_shortcode' ) ) :

// FIXME: How get all channels without bad tips ?limit=500
$url = "https://actu.epfl.ch/api/v1/channels/?limit=500";
$channel_response = NewsUtils::get_items($url);

$channel_options = array();
foreach ($channel_response->results as $item) {
$option = array(
'value' => strval($item->id),
'label' => esc_html__($item->name, 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')
);
array_push($channel_options, $option);
}

$lang_options = array(
array('value' => 'en', 'label' => esc_html__('English', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => 'fr', 'label' => esc_html__('Français', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
);

$template_options = array (
array('value' => '1', 'label' => esc_html__('Template portal image top', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '2', 'label' => esc_html__('Template text only', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '3', 'label' => esc_html__('Template faculty with 4 news', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '4', 'label' => esc_html__('Template labo with 3 news', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '6', 'label' => esc_html__('Template faculty with 3 news', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '7', 'label' => esc_html__('Template portal image left', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '8', 'label' => esc_html__('Template labo with 5 news', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
array('value' => '10', 'label' => esc_html__('Template all news with pagination', 'epfl_infoscience_shortcode', 'epfl_infoscience_shortcode')),
);

shortcode_ui_register_for_shortcode(

'epfl_news_2018',

array(
'label' => 'Add News shortcode',
'listItemImage' => 'dashicons-book',
'attrs' => array(
array(
'label' => 'Channel name',
'attr' => 'channel',
'type' => 'select',
'options' => $channel_options,
'description' => "The channel of news",
),
array(
'label' => 'Template name',
'attr' => 'template',
'type' => 'select',
'options' => $template_options,
'description' => "The template",
),
array(
'label' => 'Language',
'attr' => 'lang',
'type' => 'select',
'options' => $lang_options,
'description' => 'The language used to render news results',
),
),

'post_type' => array( 'post', 'page' ),
)
);

endif;

} );

?>
53 changes: 53 additions & 0 deletions EPFL-plugin-news/EPFL-news-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

Class NewsUtils
{
public static function debug($var) {
print "<pre>";
var_dump($var);
print "</pre>";
}

/**
* This allow to insert anchor before the element
* i.e. '<a name="' . $ws->get_anchor($item->title) . '"></a>';
* and also to get the item link in case it's not provided by the API.
* e.g. https://actu.epfl.ch/news/a-12-million-franc-donation-to-create-a-center-for/
*/
public static function get_anchor(string $title): string {

$unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );

$title = strtr( $title, $unwanted_array );
$title = str_replace(" ", "-", $title);
$title = str_replace("'", "-", $title);
$title = strtolower($title);
$title = substr($title, 0, 50);

return $title;
}

/**
* Call API
* @param url : the fetchable url
* @param args : array('timeout' => 10), see https://codex.wordpress.org/Function_Reference/wp_remote_get
* @return decoded JSON data
*/
public static function get_items(string $url) {

$response = wp_remote_get($url);

if (is_array($response)) {
$header = $response['headers']; // array of http header lines
$data = $response['body']; // use the content
if ( $header["content-type"] === "application/json" ) {
$items = json_decode($data);
return $items;
}
}
}
}
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ The `_load.php` automatically loads all files named `controller.php` in all subf
```
file|role
--|--
`controller.php`| - recover the plugin datas using filters<br/>- Pass data to the view<br>- Render view
`controller.php`| - recover the plugin datas using (actions)[]<br/>- Pass data to the view<br>- Render view<br>- use (Shortcake)[https://github.com/wp-shortcake/shortcake] API to declare backend UI
`view.php`|Output shortcode's html using given datas

### Types
#### EPFL shortcode
These shortcodes are used to display data coming from EPFL Apis. the EPFL provides a **plugin** responsible for fetching the data, formatting it and declaring a correct backend UI via (Shortcake)[https://github.com/wp-shortcake/shortcake].

To share data between the plugin and the theme, the plugins use wordpress *(actions)[]*.

The role of the theme is only to output correct markup for each given shortcode, using the given data, retrieved with the corresponding *action*.

### Add a shortcode
1. Create a new folder in `shortcodes/`
2. create `controller.php`
3. create `view.php`
4. implement your logic in the **controller**, your rendering in the **view**
5. enjoy!
5. enjoy!
2 changes: 1 addition & 1 deletion assets/css/base.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/css/base.css.map

Large diffs are not rendered by default.

Loading

0 comments on commit 3812b0f

Please sign in to comment.