<?php
function rw_trim_excerpt( $text='' )
{
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);
    $excerpt_length = apply_filters('excerpt_length', 25);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}

function change_wp_search_size($query) {
    if ( $query->is_search ) // Make sure it is a search page
        $query->query_vars['posts_per_page'] = -1; // Change 10 to the number of posts you would like to show

    return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_wp_search_size'); // Hook our custom function onto the request filter

function home_activation_hook()
{
	$options = array(
					// 'home_article_une',
                    // 'home_article_actu',
                    // 'home_actu_affiche_defaut',
                    // 'home_actu_article',
                    'home_dossier'
                    // 'home_name_bloc',
                    // 'home_faq',
                    // 'home_description'
				);
	foreach ($options as $option_name) {
		if (!get_option($option_name))
			add_option($option_name, '', '', 'yes');
	}
}

function home_getArticles($args = NULL)
{
	return get_posts();
}

function save_article_une($id=NULL)
{
    update_option('home_article_une',$id);
    return true;
}

function save_actu_article($id=null)
{
    if(is_array($id))
        update_option('home_actu_article',$id);
    else
        update_option('home_actu_article',$id);
    return true;
}


function save_nom_actu($nom_bloc)
{
    update_option('home_name_bloc',$nom_bloc);
}



function home_display_warning_message($type = 'notice', $text = null)
{
    if (!$text) {
        $text = $_SESSION['home_warning_message'][$type];
        unset($_SESSION['home_warning_message'][$type]);
    }

    if (!$text || (is_array($text) && count($text) == 0))
        return false;

    $lines = '';
    if (is_array($text)) {
        foreach ($text as $line)
            $lines .= $line . "<br />\n";
    } else {
        $lines = $text;
    }

    if ($type == 'error')
        echo '<div style="padding: 3px 5px 3px 5px; margin: 10px 0 15px 0; border: 2px solid #DD0000; background-color: #FF7F7F;">' . $lines . '</div>';
    else if ($type == 'notice')
        echo '<div style="padding: 3px 5px 3px 5px; margin: 10px 0 15px 0; border: 2px solid #00DD00; background-color: #A5FF7F;">' . $lines . '</div>';
}


function save_dossier($id)
{
    if($id!='')
    update_option('home_dossier',$id);
}



function get_mode_affichage(){
    return get_option('home_article_actu');
}

function save_mode_affichage($mode=null){
    update_option('home_article_actu',$mode);
    return true;
}

function save_faq($faq){
   update_option('home_faq',$faq);
}

function save_description($description)
{
    update_option('home_description',$description);
}



function cccc_custom_remove_menu_bar_items() {
	global $wp_admin_bar;
	// Menu: wp-logo
	$wp_admin_bar->remove_menu('wp-logo');
		$wp_admin_bar->remove_menu('about');
		$wp_admin_bar->remove_menu('wp-logo-external');
			$wp_admin_bar->remove_menu('wporg');
			$wp_admin_bar->remove_menu('documentation');
			$wp_admin_bar->remove_menu('support-forums');
			$wp_admin_bar->remove_menu('feedback');

	// Menu: my-account
	$wp_admin_bar->remove_menu('my-account');
		$wp_admin_bar->remove_menu('user-actions');
			$wp_admin_bar->remove_menu('user-info');
			$wp_admin_bar->remove_menu('edit-profile');
			$wp_admin_bar->remove_menu('logout');

	// Menu: site-name
	$wp_admin_bar->remove_menu('site-name');
		$wp_admin_bar->remove_menu('view-site');
		$wp_admin_bar->remove_menu('dashboard');
		$wp_admin_bar->remove_menu('menus');
		$wp_admin_bar->remove_menu('widgets');
		$wp_admin_bar->remove_menu('background');
		$wp_admin_bar->remove_menu('themes');
		$wp_admin_bar->remove_menu('header');

	// Menu: new-content
	$wp_admin_bar->remove_menu('new-content');
		$wp_admin_bar->remove_menu('new-post');
		$wp_admin_bar->remove_menu('new-media');
		$wp_admin_bar->remove_menu('new-link');
		$wp_admin_bar->remove_menu('new-page');
		$wp_admin_bar->remove_menu('new-user');

	// Menu: comments
	$wp_admin_bar->remove_menu('comments');

	// Menu: updates
	$wp_admin_bar->remove_menu('updates');

	// Menu: search
	$wp_admin_bar->remove_menu('search');
}



###
# Hook permettant d'enlerver les éléments du menu admin non nécessaires.
###
function cccc_custom_remove_menu_pages() {
	@remove_submenu_page('index.php', 'update-core.php');
	@remove_submenu_page('edit.php', 'edit-tags.php?taxonomy=post_tag');
	@remove_menu_page('link-manager.php');
	@remove_menu_page('edit-comments.php');
	@remove_menu_page('themes.php');
	@remove_submenu_page('themes.php', 'themes.php');
	@remove_submenu_page('themes.php', 'customize.php');
	@remove_submenu_page('themes.php', 'widgets.php');
	@remove_submenu_page('themes.php', 'custom-header');
	@remove_submenu_page('themes.php', 'theme-editor.php');
	@remove_menu_page('edit.php?post_type=page');
	@remove_menu_page('tools.php');
	@remove_menu_page('plugins.php');
	@remove_menu_page('options-general.php');
}



###
# Hook permettant d'enlerver les éléments de l'interface admin non nécessaires.
###
function cccc_custom_remove_meta_boxes() {
	@remove_meta_box('postcustom', 'post', 'core');
	@remove_meta_box('postexcerpt', 'post', 'core');
	@remove_meta_box('tagsdiv-post_tag', 'post', 'core');
}



// Creating the widget
class deposer_dossier extends WP_Widget {
    function __construct() {
        parent::__construct(
            // Base ID of your widget
            'deposer_dossier',

            // Widget name will appear in UI
            __('Bloc colonne droite', 'deposer_dossier_domain'),

            // Widget description
            array( 'description' => __( 'Bloc affichant la partie "Déposer mon dossier"', 'deposer_dossier_domain' ), )
        );
    }

    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
?>
        <div class="bloc_dossier">
            <div class="entete_bloc">
                <span class="picto"><img src="/images/depot_dossier.svg" style="width: 30px; height: 36px;" alt="" /></span>
                <span class="titre">Déposer mon&nbsp;dossier</span>
            </div>
            <div class="contenu">
                <div class="texte">
                    <p>Vous allez accéder à un formulaire qui vous permettra de déposer votre demande de médiation en quelques minutes.</p>
                    <p>Rassemblez toutes les informations et documents dont vous disposez.</p>
                    <p>Ce recours est gratuit. Votre dossier sera étudié et une réponse vous sera apportée par le médiateur de la consommation.</p>
                    <a class="boutton_formulaire" href="/formulaire-reclamation-mediateur/">Accéder au formulaire</a>
                </div>
            </div>
        </div>

<?php
    }
    /*
    // Widget Backend
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        // Widget admin form
        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <p>
        <label for="<?php echo $this->get_field_id( 'texte' ); ?>"><?php _e( 'Texte :' ); ?></label>
        <textarea class="widefat" id="<?php echo $this->get_field_id( 'texte' ); ?>" name="<?php echo $this->get_field_name( 'texte' ); ?>" /><?php echo esc_attr( $instance['texte'] ); ?></textarea>
        </p>
        <?php
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['texte'] = ( ! empty( $new_instance['texte'] ) ) ? strip_tags( $new_instance['texte'] ) : '';
        return $instance;
    }
    */
} // Class wpb_widget ends here

// Register and load the widget
function deposer_dossier_load_widget() {
	register_widget( 'deposer_dossier' );
}
add_action( 'widgets_init', 'deposer_dossier_load_widget' );


###
# Inject les script pour l'ajout d'un bouton permettant l'envoi d'image dans la médiathèque.
###
function cccc_custom_upload_button_scripts() {
    if ($_GET['page'] != 'home/dossier.php')
        return;
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');
	wp_register_script('cccc_custom-upload', WP_PLUGIN_URL . '/home/upload.js', array('jquery','media-upload','thickbox'));
	wp_enqueue_script('cccc_custom-upload');
}


###
# Inject la css pour l'ajout d'un bouton permettant l'envoi d'image dans la médiathèque.
###
function cccc_custom_upload_button_styles() {
	wp_enqueue_style('thickbox');
}

function custom_excerpt_length( $length ) {
	return 30;
}



?>