Advertisement
verygoodplugins

Untitled

Oct 25th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7. class WPF_Divi extends WPF_Integrations_Base {
  8.  
  9.     /**
  10.      * The slug for WP Fusion's module tracking.
  11.      *
  12.      * @since 3.38.14
  13.      * @var string $slug
  14.      */
  15.  
  16.     public $slug = 'divi';
  17.  
  18.     /**
  19.      * The plugin name for WP Fusion's module tracking.
  20.      *
  21.      * @since 3.38.14
  22.      * @var string $name
  23.      */
  24.     public $name = 'Divi';
  25.  
  26.     /**
  27.      * The link to the documentation on the WP Fusion website.
  28.      *
  29.      * @since 3.38.14
  30.      * @var string $docs_url
  31.      */
  32.     public $docs_url = 'https://wpfusion.com/documentation/page-builders/divi/';
  33.  
  34.     /**
  35.      * Gets things started
  36.      *
  37.      * @since 3.17.2
  38.      */
  39.  
  40.     public function init() {
  41.  
  42.         if ( ! wpf_get_option( 'restrict_content', true ) ) {
  43.             return;
  44.         }
  45.  
  46.         add_filter( 'et_builder_get_parent_modules', array( $this, 'add_field' ) );
  47.  
  48.         add_filter( 'et_pb_module_shortcode_attributes', array( $this, 'shortcode_attributes' ), 10, 5 );
  49.     }
  50.  
  51.     /**
  52.      * Register the settings on all Divi modules.
  53.      *
  54.      * @since  3.36.5
  55.      *
  56.      * @link https://gist.github.com/awah95/2d471f049eb3b7024003109d550eebb2
  57.      *
  58.      * @param  array $modules The registered modules.
  59.      * @return array $modules The registered modules.
  60.      */
  61.     public function add_field( $modules ) {
  62.  
  63.         // Ensure we run this code only once because it's expensive.
  64.  
  65.         static $is_applied = false;
  66.  
  67.         if ( $is_applied ) {
  68.             return $modules;
  69.         }
  70.  
  71.         // Bail early if the modules list empty.
  72.  
  73.         if ( empty( $modules ) ) {
  74.             return $modules;
  75.         }
  76.  
  77.         foreach ( $modules as $module_slug => $module ) {
  78.  
  79.             // Ensure fields list exist.
  80.  
  81.             if ( ! isset( $module->fields_unprocessed ) ) {
  82.                 continue;
  83.             }
  84.  
  85.             /**
  86.              * Fields list on the module.
  87.              *
  88.              * @var array
  89.              *
  90.              * The structures:
  91.              * array(
  92.              *     'field_slug' => array(
  93.              *         'label'       => '',
  94.              *         'description' => '',
  95.              *         'type'        => '',
  96.              *         'toggle_slug' => '',
  97.              *         'tab_slug'    => '',
  98.              *     ),
  99.              *     ... Other fields.
  100.              * )
  101.              */
  102.  
  103.             $fields_list = $module->fields_unprocessed;
  104.  
  105.             if ( ! empty( $fields_list ) ) {
  106.  
  107.                 $fields_list['wpf_tag'] = array(
  108.                     'label'       => sprintf( __( 'Required %s tags (any)', 'wp-fusion' ), wp_fusion()->crm->name ),
  109.                     'type'        => 'text',
  110.                     'tab_slug'    => 'custom_css',
  111.                     'toggle_slug' => 'visibility',
  112.                     'description' => __( 'Enter a comma-separated list of tag names or IDs that are required to view this element.', 'wp-fusion' ),
  113.                 );
  114.  
  115.                 $fields_list['wpf_tags_not'] = array(
  116.                     'label'       => sprintf( __( 'Required %s Tags (Not)', 'wp-fusion' ), wp_fusion()->crm->name ),
  117.                     'type'        => 'text',
  118.                     'tab_slug'    => 'custom_css',
  119.                     'toggle_slug' => 'visibility',
  120.                     'description' => __( 'Enter a comma-separated list of tag names or IDs. If the user is logged in and has any of these tags, the content will be hidden.', 'wp-fusion' ),
  121.                 );
  122.  
  123.                 $modules[ $module_slug ]->fields_unprocessed = $fields_list;
  124.             }
  125.         }
  126.  
  127.         $is_applied = true;
  128.  
  129.         return $modules;
  130.     }
  131.  
  132.  
  133.     /**
  134.      * Shortcode attributes
  135.      *
  136.      * @return  array Shortcode atts
  137.      */
  138.  
  139.     public function shortcode_attributes( $props, $attrs, $render_slug, $_address, $content ) {
  140.  
  141.         if ( ! empty( $attrs['wpf_tag'] ) || ! empty( $attrs['wpf_tags_not'] ) ) {
  142.  
  143.             $can_access = true;
  144.  
  145.             if ( wpf_admin_override() ) {
  146.  
  147.                 $can_access = true;
  148.  
  149.             } else {
  150.  
  151.                 // If Requied Tags
  152.                 if ( ! empty( $attrs['wpf_tag'] ) ) {
  153.                     if ( ! wpf_is_user_logged_in() ) {
  154.  
  155.                         $can_access = false;
  156.  
  157.                     } else {
  158.  
  159.                         $required_tags = explode( ',', $attrs['wpf_tag'] );
  160.  
  161.                         if ( ! wpf_has_tag( $required_tags ) ) {
  162.                             $can_access = false;
  163.                         }
  164.                     }
  165.                 }
  166.  
  167.                 // If Requied NOT Tags
  168.                 if ( ! empty( $attrs['wpf_tags_not'] ) ) {
  169.  
  170.                     $required_tags_not = explode( ',', $attrs['wpf_tags_not'] );
  171.  
  172.                     if ( wpf_has_tag( $required_tags_not ) ) {
  173.                         $can_access = false;
  174.                     }
  175.                 }
  176.             }
  177.  
  178.             $can_access = apply_filters( 'wpf_user_can_access', $can_access, wpf_get_current_user_id(), false );
  179.  
  180.             $can_access = apply_filters( 'wpf_divi_can_access', $can_access, $props );
  181.  
  182.             if ( false === $can_access ) {
  183.                 $props['disabled'] = 'on';
  184.             }
  185.         }
  186.  
  187.         return $props;
  188.     }
  189. }
  190.  
  191. new WPF_Divi();
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement