Created
August 17, 2016 20:37
-
-
Save obstschale/482330ada800287e28d0d687b4ad4517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Flat_Finder\Module; | |
/** | |
* Class Caldera_Form | |
* | |
* Caldera_Form is a module for using a caldera form as main input for new flats. | |
* | |
* @author Hans-Helge Buerger | |
* @package Flat_Finder\Module | |
* @since 1.0 | |
*/ | |
class Caldera_Form { | |
/** | |
* Caldera_Form constructor. | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Init method used to set up the module. | |
*/ | |
public function init() { | |
add_shortcode( 'flafi_flats', [ $this, 'flats_shortcode' ] ); | |
add_action( 'wp_enqueue_scripts', [ $this, 'register_flafi_style' ] ); | |
add_action( 'wp_enqueue_scripts', [ $this, 'register_flafi_script' ] ); | |
} | |
/** | |
* Callback method for shortcode `flafi_flats`. | |
* | |
* This method renders the output for entries, which are saved by the caldera form. | |
* | |
* @param mixed $atts Shortcode attributes. | |
* | |
* @return string HTML representation of entries | |
*/ | |
public function flats_shortcode( $atts ) { | |
// Check given attributes from shortcode. | |
$atts = shortcode_atts( [ 'caldera_form_id' => '', 'type' => '' ], $atts, 'flafi_flats' ); | |
$entries = $this->get_entries( $atts['caldera_form_id'] ); | |
if ( count( $entries ) > 0 ) { | |
$output = ''; | |
wp_enqueue_style( 'flafi_style' ); | |
wp_enqueue_script( 'flafi_script' ); | |
$output .= "<div id='flafi-filter' class='none-display flafi-btn-group'>"; | |
$output .= "<input type='button' id='flafi_btn_all' data-field='all' class='flafi-btn flafi-btn-active' value='" . __( 'All', 'flat_finder' ) . "' />"; | |
$output .= "<input type='button' id='flafi_btn_search' data-field='search' class='flafi-btn flafi-btn-default' value='" . __( 'Search', 'flat_finder' ) . "' />"; | |
$output .= "<input type='button' id='flafi_btn_offer' data-field='offer' class='flafi-btn flafi-btn-default' value='" . __( 'Offer', 'flat_finder' ) . "' />";; | |
$output .= "</div>"; | |
foreach ( $entries as $entry ) : | |
try { | |
// Prepare Entry Date. | |
$date = \DateTime::createFromFormat( 'Y/m/d', $entry['data']['flafi_entry_date'] ); | |
if ( $date !== false ) { | |
$date = $date->format( 'd. F Y' ); | |
} else { | |
$date = $entry['_date']; | |
} | |
} catch ( \Exception $ex ) { | |
echo $ex->getMessage(); | |
continue; | |
} | |
$type = ''; | |
switch ( $entry['data']['flafi_type'] ) { | |
case 'offer': | |
$type = __( 'Offer', 'flat_finder' ); | |
break; | |
case 'search': | |
$type = __( 'Search', 'flat_finder' ); | |
break; | |
} | |
$output .= '<div class="flafi_entry flafi_' . | |
esc_attr( strtolower( $entry['data']['flafi_type'] ) ) . '">'; | |
$output .= '<aside>'; | |
/* translators: %1$s is the entry type, %2$s is the date */ | |
$output .= sprintf( __( '%1$s since %2$s', 'flat_finder' ), esc_attr( $type ), $date ); | |
$output .= '</aside>'; | |
$output .= '<header id="flafi_heading">'; | |
$output .= get_avatar( $entry['data']['flafi_email'] ); | |
$output .= '<h2>' . esc_attr( $entry['data']['flafi_first_name'] . ' ' . $entry['data']['flafi_last_name'] ) . '</h2>'; | |
$output .= '<div>' . esc_attr( $entry['data']['flafi_email'] ) . '</div>'; | |
$output .= '<div>' . esc_attr( $entry['data']['flafi_telephone'] ) . '</div>'; | |
$output .= '</header>'; | |
$output .= '<div id="flafi_comment">'; | |
$output .= $entry['data']['flafi_comment']; | |
$output .= '</div>'; | |
$output .= '</div>'; | |
unset( $date ); | |
endforeach; | |
return $output; | |
} else { | |
return '<strong>' . __( 'No flats listed currently.', 'flat_finder' ) . '</strong>'; | |
} | |
} | |
/** | |
* Helper function to retrieve all entries from specific caldera form. | |
* | |
* @param string $form_id ID of caldera form. | |
* | |
* @return array Retrieved entries | |
*/ | |
private function get_entries( $form_id ) { | |
/** | |
* In front-end admin class is not included. | |
* | |
* @const CFCORE_PATH Caldera Forms absolute path | |
*/ | |
require_once( CFCORE_PATH . 'classes/admin.php' ); | |
//get all entries (page 1 with 9999999 entries per page should do it:) | |
$data = \Caldera_Forms_Admin::get_entries( $form_id, 1, 9999999 ); | |
// $data has extra meta info. $entries has just the entries | |
return ( $data['total'] > 0 ) ? $data['entries'] : [ ]; | |
} | |
/** | |
* Register Flat Finder Style, which is used later in front-end. | |
*/ | |
public function register_flafi_style() { | |
wp_register_style( 'flafi_style', FLAFI_URL . 'assets/css/flat_finder.css' ); | |
} | |
public function register_flafi_script() { | |
wp_register_script( 'flafi_script', FLAFI_URL . 'assets/js/flat_finder.js' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment