1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
<?php
namespace hji\listingstoblog\controllers;
use \hji\listingstoblog\ListingsToBlog;
use \hji\membership\Membership;
use \hji\ResponsiveIDX\controllers\Settings as rIDXSettings;
class Settings
{
private $pluginOptionsKey = 'hji_ltb_options';
static $generalSettingsKey = 'hji_ltp_general_settings';
private $loadedSettings = null;
private $defaultGeneralSettings = array(
'post_title' => 'New Exclusive Listing',
'post_category' => false,
'post_author' => 1,
'post_template' => "<p class=\"property-address\">{address}</p>\n<p class=\"listing-agent\">Listing Agent: {listingAgent_name}</p>\n<p class=\"property-description\">{property_description}</p>\n<p><a class=\"property-link\" href=\"{listing_url}\">View Listing</a></p>\n",
'import_by_agents' => false,
'import' => false,
);
private $pluginSettingsTabs;
function __construct()
{
$this->loadSettings();
}
function setActionHooks()
{
add_action('wp', array($this, 'scheduleDailyImport'));
if (is_admin())
{
add_action('admin_menu', array($this, 'addMenu'));
add_action('admin_init', array($this, 'registerGeneralSettings'));
add_action('admin_notices', array($this, 'adminNoticesAction'));
}
}
function addNoticeMembershipRequired()
{
echo '<div id="message" class="error"><p><strong style="color: #f00">ERROR (' . ListingsToBlog::NAME . '):</strong> HJI Membership plugin is required. If you don\'t have HJI Membership installed, please contact our support department at <a href="mailto:help@homejunction.com">help@homejunction.com</a> or at (858) 777-9533 Ext. 4.</p></div>';
}
function adminNoticesAction()
{
settings_errors(self::$generalSettingsKey);
}
function loadSettings()
{
$this->generalSettings = (array)get_option(self::$generalSettingsKey);
$this->generalSettings = array_merge($this->defaultGeneralSettings, $this->generalSettings);
$this->loadedSettings[self::$generalSettingsKey] = $this->generalSettings;
}
function addMenu()
{
$page = add_menu_page(
'HJI Listing Socializer',
'Listing Socializer',
'manage_options',
$this->pluginOptionsKey,
array($this, 'renderPluginOptionsPage'),
plugins_url('/hji-membership/resources/images/admin-menu.png', 'hji-membership.php')
);
}
function renderPluginOptionsPage()
{
if (!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.'));
}
$tab = isset($_GET['tab']) ? $_GET['tab'] : self::$generalSettingsKey;
echo '<div class="wrap">';
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == true)
{
echo '<div id="message" class="updated"><p>' . _('Settings saved.') . '</p></div>';
}
echo '<div id="icon-options-general" class="icon32"><br></div>';
echo '<h1>Listing Socializer by Home Junction®</h1>';
if (!is_plugin_active('hji-responsive-idx/responsive-idx.php'))
{
echo '<div style="border: 1px solid red; background: pink; padding: 0 1em;"><p>In order for <strong>Listings To Blog</strong> plugin to work, <strong>HJI Responsive IDX 2.4 or newer</strong> has to be installed and activated.</p></div>';
return;
}
if (!ListingsToBlog::isResponsiveIdxCompatible())
{
echo '<div style="border: 1px solid red; background: pink; padding: 0 1em;"><p><strong>HJI Responsive IDX 2.4 or newer is required.</strong> Current version of HJI Responsive IDX is not compatible with Listings To Blog plugin.</p></div>';
return;
}
$this->pluginOptionsTabs();
echo '<form method="post" action="options.php">';
wp_nonce_field('update-options');
settings_fields($tab);
do_settings_sections($tab);
submit_button();
echo '</form>';
echo '</div>';
}
function pluginOptionsTabs()
{
$currentTab = isset($_GET['tab']) ? $_GET['tab'] : self::$generalSettingsKey;
echo '<h2 class="nav-tab-wrapper">';
foreach ($this->pluginSettingsTabs as $tabKey => $tabCaption)
{
$active = $currentTab == $tabKey ? 'nav-tab-active' : '';
echo '<a class="nav-tab ' . $active . '" href="?page=' . $this->pluginOptionsKey . '&tab=' . $tabKey . '">' . $tabCaption . '</a>';
}
echo '</h2>';
}
function registerGeneralSettings()
{
$this->pluginSettingsTabs[self::$generalSettingsKey] = 'General';
register_setting(self::$generalSettingsKey, self::$generalSettingsKey, array(
$this,
'validateGeneralSettings'
));
add_settings_section('section_general', false, array(
$this, 'sectionGeneral'
), self::$generalSettingsKey);
}
function validateGeneralSettings($input)
{
return $input;
}
function sectionGeneral()
{
echo '<h3>Listings To Blog Import Criteria</h3>';
$markets = Membership::getInstance()->getMarkets();
$agentsOffices = get_option(rIDXSettings::$offices_agents_settings_key);
$noOffices = false;
foreach ($markets as $market)
{
if (!isset($agentsOffices[$market][$this->getImportEntity()]))
{
$noOffices[] = strtoupper($market);
}
}
if ($noOffices)
{
echo '<div class="error"><p class="error message">You don\'t have any ' . $this->getImportEntity() . ' selected in ' . implode(', ', $noOffices) . ' market. <a href="' . admin_url('admin.php?page=hjpl_options&tab=hjpl_offices_agents_settings') . '">Please add at least one office.</a></p></div>';
}
echo '<p>New property listings that match your market, office IDs and/or agent IDs will be requested via Responsive IDX and imported into your blog daily.</p>';
echo '<table class="form-table"><tbody>';
echo '<tr valign="top">';
echo '<th scope="row"><label>Post Title</label></th>';
echo '<td><input type="text" id="post_title" name="' . self::$generalSettingsKey . '[post_title]" class="regular-text empty" value="' . esc_attr($this->generalSettings['post_title']) . '" /><br /><span class="description">Supported tags: {address}, {listing_id}, {price}, {listingAgent_name}.</span></td>';
echo '</tr>';
$userListAtts = array(
'id' => 'post_author',
'name' => self::$generalSettingsKey . '[post_author]'
);
echo '<tr valign="top">';
echo '<th scope="row"><label>Post Author</label></th>';
echo '<td>' . $this->usersSelectionList($this->generalSettings['post_author'], $userListAtts) . '</td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row"><label>Category ID</label></th>';
echo '<td><input type="text" id="post_category" name="' . self::$generalSettingsKey . '[post_category]" class="regular-text empty" value="' . esc_attr($this->generalSettings['post_category']) . '" /><br /><span class="description">Separate multiple categories with comma.</span></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row"><label>Post Template</label></th>';
echo '<td><textarea id="post_template" style="width: 25em; height: 14em" name="' . self::$generalSettingsKey . '[post_template]">' . esc_html($this->generalSettings['post_template']) . '</textarea><br /><span class="description">Supported tags: {address}, {listing_id}, {property_description}, {listing_url}, {price}, {listingAgent_name}.</span></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row"><label>Activate Import</label></th>';
echo '<td><input type="checkbox" id="import" name="' . self::$generalSettingsKey . '[import]" ' . checked($this->generalSettings['import'], 1, false) .' value="1" />' . '<span class="description">New office listings will be imported on a daily basis automatically.</span></td>';
echo '</tr>';
echo '</tbody></table>';
}
function getImportEntity()
{
return ($this->generalSettings['import_by_agents'] === true) ? 'agents' : 'offices';
}
function usersSelectionList($selectedId = null, $atts = array())
{
$id = (isset($atts['id'])) ? "id='{$atts['id']}'" : '';
$name = (isset($atts['name'])) ? "name='{$atts['name']}'" : '';
$args = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'date_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'nicename',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
$users = get_users($args);
$result = "<select {$id} {$name}>";
foreach ($users as $user)
{
$selected = ($selectedId && $selectedId == $user->ID) ? ' selected="selected"' : '';
$result .= "<option value='{$user->ID}'{$selected}>{$user->data->display_name}</option>";
}
$result .= '</select>';
return $result;
}
function scheduleDailyImport()
{
if (isset($this->generalSettings['import']) && !empty($this->generalSettings['import']))
{
if (!wp_next_scheduled('dailylistingstoblog'))
{
$start = time();
wp_schedule_event($start, 'daily', 'dailylistingstoblog');
}
}
else
{
wp_clear_scheduled_hook('dailylistingstoblog');
}
}
}