fb_messenger_customer_chat_plugin.module in Facebook Messenger Customer Chat Plugin 7
Facebook Messenger Customer Chat Plugin module.
File
fb_messenger_customer_chat_plugin.moduleView source
<?php
/**
* @file
* Facebook Messenger Customer Chat Plugin module.
*/
/**
* Implements hook_menu().
*/
function fb_messenger_customer_chat_plugin_menu() {
$items = array();
$items['admin/config/system/fb-messenger/customer-chat-plugin'] = [
'title' => 'Facebook Messenger',
'description' => 'Configuration Facebook Messenger Customer Chat Plugin.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'facebook_messenger_customer_chat_plugin_admin_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
];
return $items;
}
/**
* Implements hook_page_build().
*/
function fb_messenger_customer_chat_plugin_page_build(&$page) {
$template_variables = [
'page_id' => variable_get('fb_messenger_page_id', NULL),
'optional_webhook_parameter' => variable_get('fb_messenger_optional_webhook_parameter', NULL),
'minimized' => variable_get('fb_messenger_mininimized', TRUE) ? 'true' : 'false',
];
$page['content']['fb_messenger_customer_chat_plugin'] = [
'#markup' => theme('fb_messenger_customer_chat_template', $template_variables),
'#weight' => 25,
];
}
/**
* Implements hook_theme().
*/
function fb_messenger_customer_chat_plugin_theme() {
$themes = [
'fb_messenger_customer_chat_template' => [
'template' => 'fb-messenger-customer-chat-plugin',
'arguments' => [
'page_id' => NULL,
'optional_webhook_parameter' => NULL,
'minimized' => 'true',
],
],
'fb_messenger_js_sdk_template' => [
'template' => 'fb-messenger-customer-chat-plugin-js-sdk',
'arguments' => [
'app_id' => NULL,
'locale' => 'en_US',
],
],
];
return $themes;
}
function facebook_messenger_customer_chat_plugin_admin_form($form, &$form_state) {
$form['fb_messenger_page_id'] = array(
'#type' => 'textfield',
'#title' => t('Facebook Page ID'),
'#default_value' => variable_get('fb_messenger_page_id', NULL),
'#size' => 100,
'#maxlength' => 100,
'#description' => t('The Page ID of your Facebook Page'),
);
$form['fb_messenger_optional_webhook_parameter'] = array(
'#type' => 'textfield',
'#title' => t('Optional Webhook Parameter'),
'#default_value' => variable_get('fb_messenger_optional_webhook_parameter', NULL),
'#size' => 100,
'#maxlength' => 100,
);
$form['fb_messenger_mininimized'] = array(
'#type' => 'checkbox',
'#title' => t('Facebook Messenger is minimized'),
'#default_value' => variable_get('fb_messenger_mininimized', TRUE),
);
$form['fb_messenger_include_javascript_sdk'] = array(
'#type' => 'checkbox',
'#title' => t('Include Javascript SDK'),
'#default_value' => variable_get('fb_messenger_include_javascript_sdk', TRUE),
);
$form['fb_messenger_app_id'] = array(
'#type' => 'textfield',
'#title' => t('App Id'),
'#default_value' => variable_get('fb_messenger_app_id', NULL),
'#description' => t('Add App ID if Javascript SDK is included.'),
);
$form['fb_messenger_locale'] = array(
'#type' => 'textfield',
'#title' => t('Locale'),
'#default_value' => variable_get('fb_messenger_locale', 'en_US'),
'#description' => t('Site locale, ' . l('click here for more information.', 'https://developers.facebook.com/docs/messenger-platform/discovery/customer-chat-plugin#localization')),
);
// Per-path visibility.
$form['visibility']['path'] = array(
'#type' => 'fieldset',
'#title' => t('Pages'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 0,
);
$options = array(
BLOCK_VISIBILITY_NOTLISTED => t('All pages except those listed'),
BLOCK_VISIBILITY_LISTED => t('Only the listed pages'),
);
$description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
'%blog' => 'blog',
'%blog-wildcard' => 'blog/*',
'%front' => '<front>',
));
$title = t('Pages');
$form['visibility']['path']['fb_messenger_chat_plugin_visibility'] = array(
'#type' => 'radios',
'#title' => t('Show plugin on specific pages'),
'#options' => $options,
'#default_value' => variable_get('fb_messenger_chat_plugin_visibility', BLOCK_VISIBILITY_NOTLISTED),
);
$form['visibility']['path']['fb_messenger_chat_plugin_pages'] = array(
'#type' => 'textarea',
'#title' => '<span class="element-invisible">' . $title . '</span>',
'#default_value' => variable_get('fb_messenger_chat_plugin_pages', ''),
'#description' => $description,
);
$form['visibility']['path']['fb_messenger_admin_pages'] = array(
'#type' => 'checkbox',
'#title' => t('Show on admin pages'),
'#default_value' => variable_get('fb_messenger_admin_pages'),
);
return system_settings_form($form);
}
/**
* Implements hook_page_alter().
*
* Adds a post_render callback
*/
function fb_messenger_customer_chat_plugin_page_alter(&$page) {
if (variable_get('fb_messenger_include_javascript_sdk', TRUE) && fb_messenger_customer_chat_plugin_is_included()) {
$page['#post_render'][] = 'fb_messenger_customer_chat_plugin_add_js_sdk';
}
}
/**
* Implements callback_post_render().
*
* Inserts JavaScript snippet immediately after the opening body tag.
*/
function fb_messenger_customer_chat_plugin_add_js_sdk(&$children, $elements) {
$script = theme('fb_messenger_js_sdk_template', [
'app_id' => variable_get('fb_messenger_app_id', NULL),
'locale' => variable_get('fb_messenger_locale', 'en_US'),
]);
$children = preg_replace('@<body[^>]*>@', '$0' . $script, $children, 1);
return $children;
}
/**
* Check page visibility from settings.
*
* @return bool
* Returns TRUE if page visibility matches, FALSE if not.
*/
function fb_messenger_customer_chat_plugin_is_included() {
if (variable_get('fb_messenger_admin_pages') != TRUE && path_is_admin(current_path())) {
return FALSE;
}
// Match path if necessary.
if (variable_get('fb_messenger_chat_plugin_pages', '')) {
// Convert path to lowercase. This allows comparison of the same path
// with different case. Ex: /Page, /page, /PAGE.
$pages = drupal_strtolower(variable_get('fb_messenger_chat_plugin_pages', ''));
if (variable_get('fb_messenger_chat_plugin_visibility', '') < BLOCK_VISIBILITY_PHP) {
// Convert the Drupal path to lowercase.
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
// When $block->visibility has a value of 0
// (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages
// except those listed in $block->pages. When set to 1
// (BLOCK_VISIBILITY_LISTED), it is displayed only on those pages
// listed in $block->pages.
$page_match = !(variable_get('fb_messenger_chat_plugin_visibility', '') xor $page_match);
}
else {
$page_match = FALSE;
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
Functions
Name![]() |
Description |
---|---|
facebook_messenger_customer_chat_plugin_admin_form | |
fb_messenger_customer_chat_plugin_add_js_sdk | Implements callback_post_render(). |
fb_messenger_customer_chat_plugin_is_included | Check page visibility from settings. |
fb_messenger_customer_chat_plugin_menu | Implements hook_menu(). |
fb_messenger_customer_chat_plugin_page_alter | Implements hook_page_alter(). |
fb_messenger_customer_chat_plugin_page_build | Implements hook_page_build(). |
fb_messenger_customer_chat_plugin_theme | Implements hook_theme(). |