Skip to content
Snippets Groups Projects
civicrm.shortcodes.modal.php 5.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kevin Cristiano's avatar
    Kevin Cristiano committed
    <?php
    /*
     +--------------------------------------------------------------------+
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     | CiviCRM version 5                                                  |
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     +--------------------------------------------------------------------+
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     | Copyright CiviCRM LLC (c) 2004-2019                                |
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     +--------------------------------------------------------------------+
     | This file is a part of CiviCRM.                                    |
     |                                                                    |
     | CiviCRM is free software; you can copy, modify, and distribute it  |
     | under the terms of the GNU Affero General Public License           |
     | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
     |                                                                    |
     | CiviCRM is distributed in the hope that it will be useful, but     |
     | WITHOUT ANY WARRANTY; without even the implied warranty of         |
     | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
     | See the GNU Affero General Public License for more details.        |
     |                                                                    |
     | You should have received a copy of the GNU Affero General Public   |
     | License and the CiviCRM Licensing Exception along                  |
     | with this program; if not, contact CiviCRM LLC                     |
     | at info[AT]civicrm[DOT]org. If you have questions about the        |
     | GNU Affero General Public License or the licensing of CiviCRM,     |
     | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
     +--------------------------------------------------------------------+
    */
    
    /**
     *
     * @package CRM
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     * @copyright CiviCRM LLC (c) 2004-2019
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    // This file must not accessed directly
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    if ( ! defined( 'ABSPATH' ) ) exit;
    
    
    /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     * Define CiviCRM_For_WordPress_Shortcodes_Modal Class.
     *
     * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
     */
    class CiviCRM_For_WordPress_Shortcodes_Modal {
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Plugin object reference.
       *
       * @since 4.6
       * @access public
       * @var object $civi The plugin object reference.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public $civi;
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Instance constructor.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      function __construct() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Store reference to CiviCRM plugin object
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $this->civi = civi_wp();
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Register hooks to handle the shortcode modal.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function register_hooks() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Bail if CiviCRM not installed yet
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( ! CIVICRM_INSTALLED ) return;
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Adds the CiviCRM button to post and page edit screens
        // Use priority 100 to position button to the farright
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        add_action( 'media_buttons', array( $this, 'add_form_button' ), 100 );
    
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Add the javascript and styles to make it all happen
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        add_action('load-post.php', array($this, 'add_core_resources'));
        add_action('load-post-new.php', array($this, 'add_core_resources'));
        add_action('load-page.php', array($this, 'add_core_resources'));
        add_action('load-page-new.php', array($this, 'add_core_resources'));
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Add button to editor for WP selected post types.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Callback method for 'media_buttons' hook as set in register_hooks().
       *
       * @since 4.7
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function add_form_button() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Add button to WP selected post types, if allowed
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( $this->post_type_has_button() ) {
    
    
          $civilogo = file_get_contents( plugin_dir_path( __FILE__ ) . '../assets/civilogo.svg.b64' );
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    
    
          $url = admin_url( 'admin.php?page=CiviCRM&q=civicrm/shortcode&reset=1' );
    
          echo '<a href= "' . $url . '" class="button crm-popup medium-popup crm-shortcode-button" data-popup-type="page" style="padding-left: 4px;" title="' . __( 'Add CiviCRM Public Pages', 'civicrm' ) . '"><img src="' . $civilogo . '" height="15" width="15" alt="' . __( 'Add CiviCRM Public Pages', 'civicrm' ) . '" />'. __( 'CiviCRM', 'civicrm' ) .'</a>';
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Add core resources.
       *
       * Callback method as set in register_hooks().
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.7
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function add_core_resources() {
        if ($this->civi->initialize()) {
          CRM_Core_Resources::singleton()->addCoreResources();
        }
      }
    
    
      /**
       * Does a WordPress post type have the CiviCRM button on it?
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @since 4.6
       *
       * @return bool $has_button True if the post type has the button, false otherwise.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function post_type_has_button() {
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Get screen object
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $screen = get_current_screen();
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Bail if no post type (e.g. Ninja Forms)
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        if ( ! isset( $screen->post_type ) ) return;
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Get post types that support the editor
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $capable_post_types = $this->get_post_types_with_editor();
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Default allowed to true on all capable post types
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $allowed = ( in_array( $screen->post_type, $capable_post_types ) ) ? true : false;
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        /**
         * Filter the appearance of the CiviCRM button.
         *
         * @since 4.6
         *
         * @param bool $allowed True if the button is allowed, false otherwise.
         * @param object $screen The current WordPress screen object.
         * @return bool $allowed True if the button is allowed, false otherwise.
         */
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $allowed = apply_filters( 'civicrm_restrict_button_appearance', $allowed, $screen );
    
        return $allowed;
    
      }
    
    
      /**
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * Get WordPress post types that support the editor.
       *
       * @since 4.6
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       *
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       * @return array $supported_post_types Array of post types that have an editor.
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
       */
      public function get_post_types_with_editor() {
    
        static $supported_post_types = array();
        if ( !empty( $supported_post_types) ) {
          return $supported_post_types;
        }
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Get only post types with an admin UI
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $args = array(
          'public' => true,
          'show_ui' => true,
        );
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        // Get post types
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
        $post_types = get_post_types($args);
    
        foreach ($post_types AS $post_type) {
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          // Filter only those which have an editor
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
          if (post_type_supports($post_type, 'editor')) {
            $supported_post_types[] = $post_type;
          }
        }
    
        return $supported_post_types;
      }
    
    
    Kevin Cristiano's avatar
    Kevin Cristiano committed
    } // Class CiviCRM_For_WordPress_Shortcodes_Modal ends