Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
themefuse committed Nov 27, 2014
0 parents commit 6acee19
Show file tree
Hide file tree
Showing 28 changed files with 3,985 additions and 0 deletions.
278 changes: 278 additions & 0 deletions class-fw-extension-sidebars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
<?php if (!defined('FW')) die('Forbidden');

class FW_Extension_Sidebars extends FW_Extension
{
/** @var _FW_Extension_Sidebars_Backend */
private $backend;

/** @var _FW_Extension_Sidebars_Frontend */
private $frontend;

/**
* key for saving/get data from wp_option
*/
public function get_fw_option_sidebars_settings_key(){
$current_theme = wp_get_theme();
return $current_theme->get_stylesheet() . '-fw-sidebars-options';
}


public function is_missing_config() {
$positions = $this->backend->config->get_sidebar_positions();
if (empty($positions) or false === is_array($positions)) {
return true;
}
return false;
}

/**
* @internal
*/
protected function _init()
{
$this->backend = new _FW_Extension_Sidebars_Backend();

$this->backend->init_sidebars();

if (is_admin()) {
$this->add_admin_actions();
} else {
$this->add_theme_actions();
}
}

private function add_theme_actions() {
if ($this->is_missing_config()) {
add_filter( 'sidebars_widgets', array( $this->get_frontend_instance(), 'replace_sidebars' ) );
}
}

private function add_admin_actions()
{
add_action( 'admin_enqueue_scripts', array($this, '_admin_action_enqueue_scripts'));
add_action( 'sidebar_admin_page', array($this, '_admin_action_render_partial'));

if (current_user_can('edit_theme_options')) {
add_action( 'wp_ajax_add_new_sidebar_ajax', array($this, '_admin_action_add_new_sidebar_ajax'));
add_action( 'wp_ajax_sidebar_autocomplete_ajax', array($this, '_admin_action_sidebar_autocomplete_ajax'));
add_action( 'wp_ajax_save_sidebar_preset_ajax', array($this, '_admin_action_save_sidebar_preset_ajax'));
add_action( 'wp_ajax_remove_sidebar_preset_ajax', array($this, '_admin_action_remove_sidebar_preset_ajax'));
add_action( 'wp_ajax_delete_sidebar_ajax', array($this, '_admin_action_delete_sidebar_ajax'));
add_action( 'wp_ajax_load_sidebar_preset_ajax', array($this, '_admin_action_load_sidebar_preset_ajax'));
}
}

/**
* @internal
*/
public function _admin_action_enqueue_scripts($hook)
{
if ( 'widgets.php' === $hook ) {
wp_enqueue_style(
'fw-extension-'. $this->get_name() .'-css',
$this->get_declared_URI('/static/css/sidebar.css'),
array('fw', 'fw-selectize', 'fw-backend-options'),
fw()->manifest->get_version()
);

wp_enqueue_script('fw-extension-'. $this->get_name() .'-autocomplete-js',
$this->get_declared_URI('/static/js/sidebar-autocomplete.js'),
array('fw-events', 'jquery', 'jquery-ui-autocomplete', 'fw'),
fw()->manifest->get_version()
);
wp_localize_script( 'fw-extension-'. $this->get_name() .'-autocomplete-js', 'noMatchesFoundMsg', __('No matches found', 'fw'));

wp_enqueue_script('fw-extension-'. $this->get_name() .'-general-js',
$this->get_declared_URI('/static/js/sidebar-general.js'),
array('fw-events','jquery', 'fw', 'fw-selectize', 'jquery-ui-tabs'),
fw()->manifest->get_version()
);
wp_localize_script( 'fw-extension-'. $this->get_name() .'-general-js', 'PhpVar', array(
'confirmMessage' => __('Do you realy want to change without saving?','fw'),
'dynamicSidebars' => $this->backend->get_dynamic_sidebars_ids(),
'missingIdMessage' => __('Missing ID. Check that you provided all mandatory data.', 'fw'),
'createdTabName' => __('Created','fw'),
'groupedTabDesc' => __('(For Grouped Pages)', 'fw'),
'specificTabDesc' => __('(For Specific Pages)','fw'),
'missingSidebarName' => __('No sidebar name specified', 'fw'),
'newSidebarPlaceholder' => __('Sidebar Name','fw'),
'newSidebarLabel' => __('New Sidebar', 'fw'),
'addSidebarButtonTxt' => __('Add','fw')
));
}
}

/**
* @internal
*/
public function _admin_action_delete_sidebar_ajax()
{
$sidebar_id = FW_Request::POST('sidebar');
$result = $this->backend->delete_sidebar($sidebar_id);
$this->ajax_response($result);
}

/**
* @internal
*/
public function _admin_action_load_sidebar_preset_ajax()
{
$params = FW_Request::POST('params');
$result = $this->backend->get_preset($params);
$this->ajax_response($result);
}

/**
* @internal
*/
public function _admin_action_remove_sidebar_preset_ajax()
{
$args = FW_Request::POST('data');
$result = $this->backend->remove_preset($args);
$this->ajax_response($result);
}

/**
* @internal
*/
public function _admin_action_save_sidebar_preset_ajax()
{
$settings = FW_Request::POST('settings');
$result = $this->backend->save_sidebar_settings($settings);
$this->ajax_response($result);
}

/**
* @internal
*/
public function _admin_action_sidebar_autocomplete_ajax()
{
$search_type = FW_Request::POST('searchType');
$search_term = FW_Request::POST('searchTerm');

$result = $this->backend->get_autocomplete_results($search_type, $search_term);
$this->ajax_response($result);
}

private function get_data_positions_options()
{
$sidebar_positions = $this->backend->config->get_sidebar_positions();

$choices = array();
foreach($sidebar_positions as $key => $position)
{
$choices[$key] = array(
'label' => false,
'small' => $position['icon_url'],
'data' => array(
'colors' => fw_akg(_FW_Extension_Sidebars_Config::SIDEBARS_NR_KEY,$position)
)
);
}

$data_positions_options = array(
'type' => 'image-picker',
'choices' => $choices,
'value' => '',
'attr' => array('class'=>'fw-ext-sidebars-positions')
);

return $data_positions_options;
}

/**
* Render partial on widgets.php page
* @internal
*/
public function _admin_action_render_partial()
{
$specific_options = array(
'type' => 'select',
'choices' => $this->backend->config->get_specific_labels(),
'value' => ''
);

$grouped_options = array(
'type' => 'select',
'choices' => $this->backend->config->get_grouped_labels(),
'value' => ''
);

$created_sidebars = $this->backend->get_presets_sidebars();

$sidebars = $this->backend->get_all_sidebars();

echo $this->render_view('backend-main-view', array(
'grouped_options' => $grouped_options, //options for select grouped pages tab
'specific_options' => $specific_options, //options for select specific page tab
'created_sidebars' => $created_sidebars, //used for removable items on created tab
'data_positions_options' => $this->get_data_positions_options(), //used for image-picker
'sidebars' => $sidebars, //used for selectize options on grouped and specific tabs
));
}

/**
* @internal
*/
public function _admin_action_add_new_sidebar_ajax()
{
$name = FW_Request::POST('name');
$result = $this->backend->save_new_sidebar($name);
$this->ajax_response($result);
}

/**
* return standards WP AJAX responses
*/
private function ajax_response($result)
{
if (isset($result['status'])){
if ($result['status']) {
unset($result['status']);
wp_send_json_success($result);
}else{
unset($result['status']);
wp_send_json_error($result);
}
} else {
wp_send_json($result);
}
}

private function get_frontend_instance()
{
if (!$this->frontend) {
$this->frontend = new _FW_Extension_Sidebars_Frontend();
}

return $this->frontend;
}

public function render_sidebar($color)
{
return $this->get_frontend_instance()->render_sidebar($color);
}

public function get_current_preset() {
return $this->get_frontend_instance()->get_current_page_preset();
}

public function get_current_positon() {
return $this->get_frontend_instance()->get_preset_position();
}

/**
* @return array
*/
public function get_allowed_places() {
return $this->backend->_fw_get_allowed_places();
}

/**
* {@inheritdoc}
*/
public function _get_link()
{
return 'widgets.php';
}
}
35 changes: 35 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$cfg = array();

/**
* Config example
*/

/*
$cfg['sidebar_positions'] = array(
'full' => array(
'icon_url' => 'full.png',
'sidebars_number' => 0
),
'left' => array(
'icon_url' => 'left.png',
'sidebars_number' => 1
),
'right' => array(
'icon_url' => 'right.png',
'sidebars_number' => 1
),
'left_right' => array(
'icon_url' => 'left_right.png',
'sidebars_number' => 2
),
);
$cfg['dynamic_sidebar_args'] = array(
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
);
*/
28 changes: 28 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php if (!defined('FW')) die('Forbidden');

/**
* Returns the sidebar HTML or warning message
*
* @param $color look in _FW_Extension_Sidebars_Config::$allowed_colors
*/
function fw_ext_sidebars_show($color)
{
return fw()->extensions->get('sidebars')->render_sidebar($color);
}

/**
* Returns string (position-id) if DB has preset for current page else return false
*
*/
function fw_ext_sidebars_get_current_position()
{
return fw()->extensions->get('sidebars')->get_current_positon();
}

/**
* Returns array if DB has preset for current page else return null
*/
function fw_ext_sidebars_get_current_preset()
{
return fw()->extensions->get('sidebars')->get_current_preset();
}
19 changes: 19 additions & 0 deletions hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Add ability to WP_Query to find out posts by title for the sidebars auto-complete
* @internal
*/
function _filter_fw_ext_sidebars_title_like_posts_where( $where, &$wp_query ) {
/**
* @var WPDB $wpdb
*/
global $wpdb;

if ( $post_title_like = $wp_query->get( 'fw_ext_sidebars_post_title_like' ) ) {
$where .= $wpdb->prepare(' AND ' . $wpdb->posts . '.post_title LIKE %s', '%'. $wpdb->esc_like( $post_title_like ) .'%' );
}

return $where;
}
add_filter( 'posts_where', '_filter_fw_ext_sidebars_title_like_posts_where', 10, 2);
Loading

0 comments on commit 6acee19

Please sign in to comment.