...
 
Commits (2)
/* WECHANGE PROJECTS */
.wechange-tiles {
display: flex;
width: 100%;
}
.wechange-tiles article {
flex: 1;
padding: 19px;
border: 1px solid #d8d8d8;
background-color: #fff;
word-wrap: break-word;
display: block;
float: left;
margin: 0;
position: relative;
margin-bottom: 0;
margin-left: -1px;
}
.wechange-tiles article:first-child {
margin-left: 0;
}
.wechange-tiles article .box-shadow-overlay {
box-shadow: inset 0px 0px 18px 0px rgba(0,0,0,0.3);
visibility: hidden;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.wechange-tiles article:hover .box-shadow-overlay {
visibility: visible;
}
.wechange-tiles article h2 {
font-size: 16px;
}
.wechange-tiles.grid {
flex-wrap: wrap;
}
.wechange-tiles.row article {
display: none;
}
/* Small devices (landscape phones, 576px and up) */
@media (min-width: 576px) {
.wechange-tiles.row article:nth-child(-n+3) {
display: block;
}
.wechange-tiles.grid article {
flex: 1 0 100%;
}
}
/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) {
.wechange-tiles.row article:nth-child(-n+3){
display: block;
}
.wechange-tiles.grid article {
flex: 1 0 100%;
}
}
/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) {
.wechange-tiles.row article:nth-child(-n+4){
display: block;
}
.wechange-tiles.grid article {
flex: 1 0 25%;
}
}
/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {
.wechange-tiles.row article:nth-child(-n+5){
display: block;
}
.wechange-tiles.grid article {
flex: 1 0 20%;
}
}
\ No newline at end of file
<?php
/*
Plugin Name: WECHANGE projects
Plugin URI: https://wechange.de
Description: Shortcode to display projects from a WECHANGE platform
Version: 1.0
Author: WECHANGE
Author URI: https://wechange.de
License: AGPL 3.0
License URI: https://www.gnu.org/licenses/agpl-3.0.html
*/
function wpse_load_plugin_css() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'wechange-projects-style', $plugin_url . 'style.css' );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_plugin_css' );
// Projects shortcode
function wechange_projects( $atts ) {
// Check view parameter
if (array_key_exists('view', $atts)) {
$view = $atts['view'];
unset($atts['view']);
} else {
$view = 'grid';
}
// Check url parameter or create URL by domain + parameters
$urls = array();
if (array_key_exists('url', $atts)) {
$urls = explode(' ', $atts['url']);
} else {
if (array_key_exists('domain', $atts)) {
$domain = $atts['domain'];
unset($atts['domain']);
} else {
$domain = 'https://wechange.de';
}
$url = 'https://' . $domain . '/api/v2/projects/';
if (count($atts) > 0) {
$url .= '?' . http_build_query($atts);
}
$urls[] = $url;
}
// Get projects
$projects = array();
foreach ($urls as $url) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_URL, $url);
$result = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if ($httpCode == 200) {
$result = json_decode($result);
// Add domain to each project
$domain = parse_url($url)['host'];
foreach ($result->results as $result) {
$result->domain = $domain;
$projects[] = $result;
}
}
}
if (count($projects) == 0) {
return;
}
// Post-process multiple sources
if (count($urls) > 1) {
// Reorder projects
if (array_key_exists('order_by', $atts)) {
$order_by = $atts['order_by'];
# Check if order_by starts with - (asc/desc)
$order_asc = true;
if (substr($attrs['order_by'], 0, 1) === '-') {
$order_asc = false;
$order_by = substr($order_by, 1);
}
usort($projects, function ($item1, $item2) {
if ($item1->$order_by == $item2->$order_by) {
return 0;
}
if ($order_asc == true) {
return $item1->$order_by < $item2->$order_by ? -1 : 1;
} else {
return $item1->$order_by > $item2->$order_by ? -1 : 1;
}
});
}
// Limit results
if (array_key_exists('limit', $atts)) {
$projects = array_slice($projects, 0, $atts['limit']);
}
}
// Create HTML
$container_template = <<<EOT
<div class="wechange-tiles clearfix [VIEW]">
[TILES]
</div>
EOT;
$tile_template = <<<EOT
<article>
<div class="box-shadow-overlay"></div>
[IMAGE]
<h2 class="entry-title">
<a href="https://[URL]/project/[SLUG]/">
[NAME]
</a>
</h2>
<div class="post-content">
<p>
[DESCRIPTION]
</p>
</div>
</article>
EOT;
$tile_image = <<<EOT
<div class="image">
<a href="https://[URL]/project/[SLUG]/"
class="entry-featured-image-url">
<img src="[AVATAR]"
alt="[NAME]" width="400" height="250">
</a>
</div>
EOT;
$tiles = array();
$project;
$tile;
for ($i = 0; $i < count($projects); $i++) {
$project = $projects[$i];
$tile = $tile_template;
$tile = str_replace('[NAME]', $project->name, $tile);
$tile = str_replace('[SLUG]', $project->slug, $tile);
# Show excerpt only (first 200 chars)
$description = $project->description;
if(strlen($description) > 200) $description = substr($description, 0, 200).'...';
$tile = str_replace('[DESCRIPTION]', $description, $tile);
$image = '';
if (count($project->avatar) > 0) {
$image = $tile_image;
$image = str_replace('[NAME]', $project->name, $image);
$image = str_replace('[SLUG]', $project->slug, $image);
$image = str_replace('[AVATAR]', $project->avatar, $image);
}
$tile = str_replace('[IMAGE]', $image, $tile);
$tile = str_replace('[URL]', $project->domain, $tile);
array_push($tiles, $tile);
}
$container_template = str_replace('[VIEW]', $view, $container_template);
$html = str_replace('[TILES]', join('', $tiles), $container_template);
return $html;
}
add_shortcode( 'wechange_projects', 'wechange_projects' );
?>
\ No newline at end of file