Newer
Older
<?php
/*
+--------------------------------------------------------------------+
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* Define CiviCRM_For_WordPress_Shortcodes_Modal Class.
*
* @since 4.6
*/
class CiviCRM_For_WordPress_Shortcodes_Modal {
/**
* Plugin object reference.
*
* @since 4.6
* @access public
* @var object $civi The plugin object reference.
// Adds the CiviCRM button to post and page edit screens
// Use priority 100 to position button to the farright
add_action( 'media_buttons', array( $this, 'add_form_button' ), 100 );
// Add the javascript and styles to make it all happen
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'));
}
/**
* Add button to editor for WP selected post types.
* Callback method for 'media_buttons' hook as set in register_hooks().
*
* @since 4.7
// Add button to WP selected post types, if allowed
$civilogo = file_get_contents( plugin_dir_path( __FILE__ ) . '../assets/civilogo.svg.b64' );
$url = admin_url( 'admin.php?page=CiviCRM&q=civicrm/shortcode&reset=1' );
echo '<a href= "' . $url . '" class="button crm-shortcode-button" 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>';
* Add core resources.
*
* Callback method as set in register_hooks().
*/
public function add_core_resources() {
if ($this->civi->initialize()) {
Civi::resources()
->addCoreResources()
->addScriptFile('civicrm', 'js/crm.insert-shortcode.js', 0, 'html-header');
}
}
/**
* Does a WordPress post type have the CiviCRM button on it?
*
* @since 4.6
*
* @return bool $has_button True if the post type has the button, false otherwise.
$capable_post_types = $this->get_post_types_with_editor();
// Default allowed to true on all capable post types
$allowed = ( in_array( $screen->post_type, $capable_post_types ) ) ? true : false;
/**
* 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.
*/
$allowed = apply_filters( 'civicrm_restrict_button_appearance', $allowed, $screen );
return $allowed;
}
/**
* Get WordPress post types that support the editor.
*
* @since 4.6
* @return array $supported_post_types Array of post types that have an editor.
*/
public function get_post_types_with_editor() {
static $supported_post_types = array();
if ( !empty( $supported_post_types) ) {
return $supported_post_types;
}
$args = array(
'public' => true,
'show_ui' => true,
);
$post_types = get_post_types($args);
foreach ($post_types AS $post_type) {
if (post_type_supports($post_type, 'editor')) {
$supported_post_types[] = $post_type;
}
}
return $supported_post_types;
}
} // Class CiviCRM_For_WordPress_Shortcodes_Modal ends