Example #1
0
/**
 * Generic Ajax handler
 *
 * @return void
 */
function cfcp_about_admin_ajax()
{
    if (!empty($_POST['cfcp_about_action'])) {
        switch ($_POST['cfcp_about_action']) {
            case 'cfcp_image_search':
                $results = cfcp_about_image_search(array('key' => $_POST['key'], 'term' => $_POST['cfp-img-search-term'], 'exclude' => !empty($_POST['cfcp_search_exclude']) ? array_map('intval', $_POST['cfcp_search_exclude']) : array()));
                $ret = array('success' => !empty($results) ? true : false, 'key' => $_POST['key'], 'html' => !empty($results) ? $results : '<div class="cfp-img-search-no-results">' . __('No results found.', 'favepersonal') . '</div>');
                break;
        }
        header('Content-Type: application/json');
        echo json_encode($ret);
        exit;
    }
}
Example #2
0
/**
 * Generic Ajax handler
 *
 * @return void
 */
function cfcp_about_admin_ajax()
{
    if (!empty($_POST['cfcp_about_action'])) {
        switch ($_POST['cfcp_about_action']) {
            case 'cfcp_image_search':
                $results = cfcp_about_image_search(array('key' => $_POST['key'], 'term' => $_POST['cfp-img-search-term'], 'exclude' => !empty($_POST['cfcp_search_exclude']) ? array_map('intval', $_POST['cfcp_search_exclude']) : array()));
                $ret = array('success' => !empty($results) ? true : false, 'key' => $_POST['key'], 'html' => !empty($results) ? $results : '<div class="cfp-img-search-no-results">' . __('No results found.', 'favepersonal') . '</div>');
                break;
            case 'cfcp_fetch_favicon':
                usleep(500000);
                // pause for 1/2 second to allow the spinner to at least display in the admin
                $u = new CF_Favicon_Fetch(cfcp_about_get_favicon_dir());
                $favicon = $u->have_site_favicon($_POST['url']);
                if (empty($favicon)) {
                    $success = false;
                    $favicon = $u->get_site_favicon_url($_POST['url']);
                    if (!empty($favicon)) {
                        $success = true;
                        $favicon_status = 'new';
                    } else {
                        $success = false;
                        $favicon = cfcp_about_favicon_url('default');
                        $favicon_status = 'default';
                    }
                } else {
                    $success = true;
                    $favicon = cfcp_about_favicon_url($favicon);
                    $favicon_status = 'local';
                }
                $ret = array('success' => $success, 'favicon_url' => $favicon, 'favicon_status' => $favicon_status);
                break;
            case 'cfcp_save_favicon':
                $success = false;
                $error = '';
                $link = array('title' => trim($_POST['link']['title']), 'url' => trim($_POST['link']['url']), 'favicon' => trim($_POST['link']['favicon']), 'favicon_status' => trim($_POST['link']['favicon_status']));
                $qs = strpos($link['favicon'], '?');
                if ($qs !== false) {
                    $link['favicon'] = substr($link['favicon'], 0, $qs);
                }
                // fetch
                if (!empty($link['url']) && !empty($link['title'])) {
                    if ($link['favicon_status'] == 'new') {
                        $u = new CF_Favicon_Fetch(cfcp_about_get_favicon_dir());
                        $a = $u->get_favicon($link['url']);
                        if (!empty($a) && $a != 'default') {
                            $link['favicon'] = basename($a);
                            $link['favicon_status'] = 'local';
                        } else {
                            $link['favicon'] = 'default';
                            $link['favicon_status'] = 'local';
                        }
                        if (!empty($link['favicon'])) {
                            $success = true;
                        } else {
                            $error = $u->get_last_error();
                        }
                    } elseif ($link['favicon_status'] == 'local' || $link['favicon_status'] == 'default') {
                        if ($link['favicon_status'] == 'default') {
                            $link['favicon'] = 'default';
                        } else {
                            $link['favicon'] = basename($link['favicon']);
                        }
                        $success = true;
                    } elseif ($link['favicon_status'] == 'custom') {
                        $u = new CF_Favicon_Fetch(cfcp_about_get_favicon_dir());
                        // download and save favicon
                        $f_data = $u->fetch_favicon($link['favicon']);
                        $filename = $u->make_filename($link['url'], $f_data['ext']);
                        if ($u->save_file($filename, $f_data) !== false) {
                            $link['favicon'] = $filename;
                            $link['favicon_status'] = 'local';
                            $success = true;
                        }
                    }
                } else {
                    if (empty($link['title'])) {
                        $error += '<p>' . __('Please enter a valid link title.', 'favepersonal') . '</p>';
                    }
                    if (empty($link['url'])) {
                        $error += '<p>' . __('Please enter a valid link URL.', 'favepersonal') . '</p>';
                    }
                }
                // formulate response
                if ($success) {
                    $ret = array('success' => true, 'html' => cfct_template_content('functions/about/views', 'link-item', compact('link')));
                } else {
                    $ret = array('success' => false, 'error' => $error);
                }
                break;
        }
        header('Content-Type: application/json');
        echo json_encode($ret);
        exit;
    }
}