HEX
Server: Apache/2
System: Linux server-80-13-140-150.da.direct 5.14.0-362.24.1.el9_3.0.1.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Apr 4 22:31:43 UTC 2024 x86_64
User: cpt (1004)
PHP: 8.1.24
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/cpt/domains/collectifpourtous.fr/public_html/wp-content/themes/divi-child/functions.php
<?php

use MailPoet\Subscription\SubscriptionUrlFactory;
use MailPoet\Subscribers\SubscribersRepository;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Router\Router;

function my_theme_enqueue_styles() {

    $parent_style = 'divi-style';

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );




/**
 *  Ajoute au menu la connexion 
/*
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
    if (is_user_logged_in() && $args->theme_location == 'primary-menu') {
        //$items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
		
		$manage_subscription_url = mailpoet_get_subscription_url();
		$items.='<li><a href="'.$manage_subscription_url.'"> gerer les abonnements </a></li>' ;
		
    }
    elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
    }
	
    return $items;
}
*/



/**
 *  Ajoute au menu la gestion des abonnements
 **/

 function add_submenu( $items ) {
	 
	 if ( ! $items ) {
		return false;
	}
	 if ( ! taxonomy_exists( 'nav_menu' ) ) {
		return false;
	}
	 $menu_id=1251; // menu to add subenu
	 $found_menu =false;
	 
    foreach ( $items as $item ) {
		//if( get_class($item) == 'WP_Post'  && $item->ID == $menu_id ){
		if($item->title == 'Mon compte'){
			$found_menu =true;
		}
	    //echo('<pre>');
		//print_r($item);
		//print_r( get_class($item) );
		//echo('</pre>');//print each menu item an get your parent menu item-id
    }
   if (is_user_logged_in() && $found_menu){
	   $link = array (
		   'title'            => 'Mes abonnements',
		   'menu_item_parent' => 1251,
		   'ID'               => 'menu_abonnement',
		   'url'              => mailpoet_get_subscription_url()
	  );
	   $items[] = (object) $link;
   }
  
  return $items;
}

//add_filter( 'wp_get_nav_menu_items', 'add_submenu' );
add_filter( 'wp_nav_menu_objects', 'add_submenu' );




/**
 *  Mailpoet
 *  Retourne l'url de gestion de la liste des abonnements
 **/

function mailpoet_get_subscription_url() {
	
	$current_user = wp_get_current_user();
	$email = $current_user->user_email;
	
	$subscriptionUrlFactory = SubscriptionUrlFactory::getInstance();
	//$subscribersRepository= SubscribersRepository::getInstance();
	$mailpoet_api = \MailPoet\API\API::MP('v1');
    try {
	    $subscriberArray = $mailpoet_api->getSubscriber( $email );
    } catch (\Exception $e) {
        echo  $e->getMessage().' '.$e->getCode();
        return '';
    }
	
	/*$subscriber = new SubscriberEntity();
      $subscriber->setEmail($subscriberArray['email']);
      $subscriber->setFirstName($subscriberArray['first_name']);
      $subscriber->setLastName($subscriberArray['last_name']);
      $subscriber->setLinkToken($subscriberArray['link_token']);
	  */
	if(count($subscriberArray)){
		$subscriberData = [
			'token' => $subscriberArray['link_token'],
			'email' => $subscriberArray['email'],
		  ];

		//return  print_r($subscriberArray['id'],true).'<br/>'. print_r($subscriberData,true);

		// Générez l'URL de gestion d'abonnement
		$site_url = get_site_url(); // Récupère l'URL du site WordPress
		$data= Router::encodeRequestData($subscriberData);
		$manage_url = sprintf('%s/abonnements/?mailpoet_router&endpoint=subscription&action=manage&data=%s',$site_url, $data);
	} else{
		$manage_url='';
	}
	
	return $manage_url;
}
//add_shortcode('mp_get_subscription_url', 'mailpoet_get_subscription_url');




/**
 *  Mailpoet
 *  Supprime des champs inutiles du formulaire de gestion des abonnement de  
 **/
add_filter( 'mailpoet_manage_subscription_page_form_fields', 'mp_remove_manage_fields', 10);
function mp_remove_manage_fields( $form ) {	

	unset($form[0]); // First Name
	unset($form[1]); // Last Name
    	
	return $form;
}



/**
 * Custom shortcode to display WPForms form entries in table view.
 *
 * Basic usage: [wpforms_entries_table id="FORMID"].
 *
 * Possible shortcode attributes:
 * id (required)  Form ID of which to show entries.
 * user           User ID, or "current" to default to current logged in user.
 * fields         Comma separated list of form field IDs.
 * number         Number of entries to show, defaults to 30.
 *
 * @link https://wpforms.com/developers/how-to-display-form-entries/
 *
 * Realtime counts could be delayed due to any caching setup on the site
 *
 * @param array $atts Shortcode attributes.
 *
 * @return string
 */

function wpf_entries_table( $atts ) {

    // Pull ID shortcode attributes.
    $atts = shortcode_atts(
        [
            'id'     => '',
            'user'   => '',
            'fields' => '',
            'number' => '',
            'type'   => 'all', // all, unread, read, or starred.
            'sort'   => '',
            'order'  => 'asc',
        ],
        $atts
    );

    // Check for an ID attribute (required) and that WPForms is in fact
    // installed and activated.
    if ( empty( $atts[ 'id' ] ) || ! function_exists( 'wpforms' ) ) {
        return;
    }

    // Get the form, from the ID provided in the shortcode.
    $form = wpforms()->form->get( absint( $atts[ 'id' ] ) );

    // If the form doesn't exists, abort.
    if ( empty( $form ) ) {
        return;
    }

    // Pull and format the form data out of the form object.
    $form_data = ! empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';

    // Check to see if we are showing all allowed fields, or only specific ones.
    $form_field_ids = isset( $atts[ 'fields' ] ) && $atts[ 'fields' ] !== '' ? explode( ',', str_replace( ' ', '', $atts[ 'fields' ] ) ) : [];

    // Setup the form fields.
    if ( empty( $form_field_ids ) ) {
        $form_fields = $form_data[ 'fields' ];
    } else {
        $form_fields = [];
        foreach ( $form_field_ids as $field_id ) {
            if ( isset( $form_data[ 'fields' ][ $field_id ] ) ) {
                $form_fields[ $field_id ] = $form_data[ 'fields' ][ $field_id ];
            }
        }
    }

    if ( empty( $form_fields ) ) {
        return;
    }

    // Here we define what the types of form fields we do NOT want to include,
    // instead they should be ignored entirely.
    $form_fields_disallow = apply_filters( 'wpforms_frontend_entries_table_disallow', [ 'divider', 'html', 'pagebreak', 'captcha' ] );

    // Loop through all form fields and remove any field types not allowed.
    foreach ( $form_fields as $field_id => $form_field ) {
        if ( in_array( $form_field[ 'type' ], $form_fields_disallow, true ) ) {
            unset( $form_fields[ $field_id ] );
        }
    }

    $entries_args = [
        'form_id' => absint( $atts[ 'id' ] ),
    ];

    // Narrow entries by user if user_id shortcode attribute was used.
    if ( ! empty( $atts[ 'user' ] ) ) {
        if ( $atts[ 'user' ] === 'current' && is_user_logged_in() ) {
            $entries_args[ 'user_id' ] = get_current_user_id();
        } else {
            $entries_args[ 'user_id' ] = absint( $atts[ 'user' ] );
        }
    }

    // Number of entries to show. If empty, defaults to 30.
    if ( ! empty( $atts[ 'number' ] ) ) {
        $entries_args[ 'number' ] = absint( $atts[ 'number' ] );
    }

    // Filter the type of entries all, unread, read, or starred
    if ( $atts[ 'type' ] === 'unread' ) {
        $entries_args[ 'viewed' ] = '0';
    } elseif( $atts[ 'type' ] === 'read' ) {
        $entries_args[ 'viewed' ] = '1';
    } elseif ( $atts[ 'type' ] === 'starred' ) {
        $entries_args[ 'starred' ] = '1';
    }

    // Get all entries for the form, according to arguments defined.
    // There are many options available to query entries. To see more, check out
    // the get_entries() function inside class-entry.php (https://a.cl.ly/bLuGnkGx).
    $entries = json_decode(json_encode(wpforms()->entry->get_entries( $entries_args )), true);

    if ( empty( $entries ) ) {
        return '<p>Pas de réponse trouvée.</p>';
    }

    foreach($entries as $key => $entry) {
        $entries[$key][ 'fields' ] = json_decode($entry[ 'fields' ], true);
        $entries[$key][ 'meta' ] = json_decode($entry[ 'meta' ], true);
    }

    if ( !empty($atts[ 'sort' ]) && isset($entries[0][ 'fields' ][$atts[ 'sort' ]] ) ) {
        if ( strtolower($atts[ 'order' ]) == 'asc' ) {
            usort($entries, function ($entry1, $entry2) use ($atts) {
                return strcmp($entry1[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ]);
            });
        } elseif ( strtolower($atts[ 'order' ]) == 'desc' ) {
            usort($entries, function ($entry1, $entry2) use ($atts) {
                return strcmp($entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry1[ 'fields' ][$atts[ 'sort' ]]['value']);
            });
        }
    }

    ob_start();

//    echo '<pre>'.print_r($form,true).'</pre>';
//    echo '<pre>'.print_r($form_fields,true).'</pre>';
//    echo '<pre>'.print_r($entries,true).'</pre>';



    echo '<table class="wpforms-frontend-entries">';
    echo '<thead><tr>';

    // Loop through the form data so we can output form field names in
    // the table header.
    foreach ( $form_fields as $form_field ) {

        if (isset($form_field['label_disable']) && $form_field['label_disable'] == 1){
            continue;
        }

        // Output the form field name/label.
        echo '<th>';
        echo esc_html( sanitize_text_field( $form_field[ 'label' ] ) );
        echo '</th>';
    }

    echo '</tr></thead>';

    echo '<tbody>';

    // Now, loop through all the form entries.
    foreach ( $entries as $entry ) {

        echo '<tr>';

        $entry_fields = $entry[ 'fields' ];

        foreach ( $form_fields as $form_field ) {
            if (isset($form_field['label_disable']) && $form_field['label_disable'] == 1){
                continue;
            }


            echo '<td >';

            foreach ( $entry_fields as $entry_field ) {
                if ( absint( $entry_field[ 'id' ] ) === absint( $form_field[ 'id' ] ) ) {
                    echo apply_filters( 'wpforms_html_field_value', wp_strip_all_tags( $entry_field[ 'value' ] ), $entry_field, $form_data, 'entry-frontend-table' );
                    break;
                }
            }

            echo '</td>';
        }

        echo '</tr>';
    }

    echo '</tbody>';

    echo '</table>';

    $output = ob_get_clean();

    return $output;
}
add_shortcode( 'wpforms_entries_table', 'wpf_entries_table' );



/**
 * Custom shortcode to display WPForms form entries in table view.
 *
 * Basic usage: [wpforms_entries_table id="FORMID"].
 *
 * Possible shortcode attributes:
 * id (required)  Form ID of which to show entries.
 * user           User ID, or "current" to default to current logged in user.
 * fields         Comma separated list of form field IDs.
 * number         Number of entries to show, defaults to 30.
 *
 * @link https://wpforms.com/developers/how-to-display-form-entries/
 *
 * Realtime counts could be delayed due to any caching setup on the site
 *
 * @param array $atts Shortcode attributes.
 *
 * @return string
 */

function wpf_entries_summary( $atts ) {

    // Pull ID shortcode attributes.
    $atts = shortcode_atts(
        [
            'id'     => '',
            'user'   => '',
            'fields' => '',
            'number' => '',
            'type'   => 'all', // all, unread, read, or starred.
            'sort'   => '',
            'order'  => 'asc',
        ],
        $atts
    );

    // Check for an ID attribute (required) and that WPForms is in fact
    // installed and activated.
    if ( empty( $atts[ 'id' ] ) || ! function_exists( 'wpforms' ) ) {
        return;
    }

    // Get the form, from the ID provided in the shortcode.
    $form = wpforms()->form->get( absint( $atts[ 'id' ] ) );

    // If the form doesn't exists, abort.
    if ( empty( $form ) ) {
        return;
    }

    // Pull and format the form data out of the form object.
    $form_data = ! empty( $form->post_content ) ? wpforms_decode( $form->post_content ) : '';

    // Check to see if we are showing all allowed fields, or only specific ones.
    $form_field_ids = isset( $atts[ 'fields' ] ) && $atts[ 'fields' ] !== '' ? explode( ',', str_replace( ' ', '', $atts[ 'fields' ] ) ) : [];

    // Setup the form fields.
    if ( empty( $form_field_ids ) ) {
        $form_fields = $form_data[ 'fields' ];
    } else {
        $form_fields = [];
        foreach ( $form_field_ids as $field_id ) {
            if ( isset( $form_data[ 'fields' ][ $field_id ] ) ) {
                $form_fields[ $field_id ] = $form_data[ 'fields' ][ $field_id ];
            }
        }
    }

    if ( empty( $form_fields ) ) {
        return;
    }

    // Here we define what the types of form fields we do NOT want to include,
    // instead they should be ignored entirely.
    $form_fields_disallow = apply_filters( 'wpforms_frontend_entries_table_disallow', [ 'divider', 'html', 'pagebreak', 'captcha' ] );

    // Loop through all form fields and remove any field types not allowed.
    foreach ( $form_fields as $field_id => $form_field ) {
        if ( in_array( $form_field[ 'type' ], $form_fields_disallow, true ) ) {
            unset( $form_fields[ $field_id ] );
        }
    }

    $entries_args = [
        'form_id' => absint( $atts[ 'id' ] ),
    ];

    // Narrow entries by user if user_id shortcode attribute was used.
    if ( ! empty( $atts[ 'user' ] ) ) {
        if ( $atts[ 'user' ] === 'current' && is_user_logged_in() ) {
            $entries_args[ 'user_id' ] = get_current_user_id();
        } else {
            $entries_args[ 'user_id' ] = absint( $atts[ 'user' ] );
        }
    }

    // Number of entries to show. If empty, defaults to 30.
    if ( ! empty( $atts[ 'number' ] ) ) {
        $entries_args[ 'number' ] = absint( $atts[ 'number' ] );
    }

    // Filter the type of entries all, unread, read, or starred
    if ( $atts[ 'type' ] === 'unread' ) {
        $entries_args[ 'viewed' ] = '0';
    } elseif( $atts[ 'type' ] === 'read' ) {
        $entries_args[ 'viewed' ] = '1';
    } elseif ( $atts[ 'type' ] === 'starred' ) {
        $entries_args[ 'starred' ] = '1';
    }

    // Get all entries for the form, according to arguments defined.
    // There are many options available to query entries. To see more, check out
    // the get_entries() function inside class-entry.php (https://a.cl.ly/bLuGnkGx).
    $entries = json_decode(json_encode(wpforms()->entry->get_entries( $entries_args )), true);

    if ( empty( $entries ) ) {
        return '<p>Pas de réponse trouvée.</p>';
    }

    foreach($entries as $key => $entry) {
        $entries[$key][ 'fields' ] = json_decode($entry[ 'fields' ], true);
        $entries[$key][ 'meta' ] = json_decode($entry[ 'meta' ], true);
    }

    if ( !empty($atts[ 'sort' ]) && isset($entries[0][ 'fields' ][$atts[ 'sort' ]] ) ) {
        if ( strtolower($atts[ 'order' ]) == 'asc' ) {
            usort($entries, function ($entry1, $entry2) use ($atts) {
                return strcmp($entry1[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ]);
            });
        } elseif ( strtolower($atts[ 'order' ]) == 'desc' ) {
            usort($entries, function ($entry1, $entry2) use ($atts) {
                return strcmp($entry2[ 'fields' ][$atts[ 'sort' ]][ 'value' ], $entry1[ 'fields' ][$atts[ 'sort' ]]['value']);
            });
        }
    }

    ob_start();

    //echo '<pre>'.print_r($form,true).'</pre>';
    //echo '<pre>'.print_r($form_fields,true).'</pre>';
    //    echo '<pre>'.print_r($entries,true).'</pre>';



    // preprare summary getting all question and possible values
    $entry_field_values = array();
    $entry_field_label = array();
    foreach ($form_fields as $form_field) {
        switch ($form_field['type']) {
            case 'radio':
                foreach ($form_field['choices'] as $choice) {
                    $entry_field_values[$form_field['id']][$choice['label']] = 0;
                    $entry_field_label [$form_field['id']] = $form_field['label'];
                }
        }
    }


    // feeds summary counts
    foreach ( $entries as $entry ) {
        $fields = $entry['fields'];

        foreach ($fields as $field){
            switch ($field['type']) {
                case 'radio':
                    $entry_field_values[$field['id']][$field['value']] ++;
            }
        }
    }

    echo '<dl>';
    foreach ( $entry_field_values as $id => $entry_field_value ) {
        echo '<br/><dt>'.$entry_field_label[$id].'</dt>';
        foreach ( $entry_field_value as $value_label => $value_count ) {
            echo '<dd>' . $value_label . ' : ' . $value_count . '</dd>';
        }
    }
    echo '</dl>';


    $output = ob_get_clean();

    return $output;
}
add_shortcode( 'wpf_entries_summary', 'wpf_entries_summary' );




/**
 * Custom shortcode to display WPForms form entries count for a form.
 *
 * Realtime counts could be delayed due to any caching setup on the site
 *
 * @link https://wpforms.com/developers/how-to-display-form-entries/
 *
 */

function wpforms_entries_count( $atts ) {

    // Pull ID shortcode attributes.
    $atts = shortcode_atts(
        [
            'id'     => '',
            'type' => 'all', // all, unread, read, or starred.
        ],
        $atts
    );

    if ( empty( $atts[ 'id' ] ) ) {
        return;
    }

    $args = [
        'form_id' => absint( $atts[ 'id' ] ),
    ];

    if ( $atts[ 'type' ] === 'unread' ) {
        $args[ 'viewed' ] = '0';
    } elseif( $atts[ 'type' ] === 'read' ) {
        $args[ 'viewed' ] = '1';
    } elseif ( $atts[ 'type' ] === 'starred' ) {
        $args[ 'starred' ] = '1';
    }

    return "Il y a ".wpforms()->entry->get_entries( $args, true )." réponse(s)";

}
add_shortcode( 'wpforms_entries_count', 'wpforms_entries_count' );