예제 #1
0
/**
 * Get the add-on corresponding to the current single post we are viewing
 *
 * @return CUAR_AddOn|null
 */
function cuar_get_single_content_addon()
{
    $pt = get_post_type();
    $types = cuar()->get_private_types();
    if (isset($types[$pt])) {
        return cuar_addon($types[$pt]['content-page-addon']);
    }
    return null;
}
예제 #2
0
/**
 * @param array $post_data The same array you would give to wp_insert_post to create your post. No need to set the post type, this will automatically be set.
 * @param array $owner An array containing the owner description: type ('usr', 'grp', 'prj', 'rol', etc.) and IDs of corresponding objects
 * @return int¦WP_Error the post ID if the function could insert the post, else, a WP_Error object
 *
 * ´cuar_insert_file(
 *      array(
 *          'post_title' => 'Test file',
 *          'post_content' => 'This is the content',
 *          'post_status' => 'publish'
 *      ),
 *      array(
 *          'type' => 'usr',
 *          'ids' => array(1)
 *      )
 * );´
 */
function cuar_create_private_page($post_data, $owner)
{
    // Create the post object
    $post_data['post_type'] = 'cuar_private_page';
    $post_id = wp_insert_post($post_data);
    if (is_wp_error($post_id)) {
        return $post_id;
    }
    // Assign the owner
    /** @var CUAR_PostOwnerAddOn $po_addon */
    $po_addon = cuar_addon('post-owner');
    $po_addon->save_post_owners($post_id, $owner['ids'], $owner['type']);
    return $post_id;
}
예제 #3
0
/**
 * Print an address
 */
function cuar_print_address($address, $address_id, $address_label = '', $template_prefix = '')
{
    /** @var CUAR_AddressesAddOn $ad_addon */
    $ad_addon = cuar_addon('address-manager');
    $ad_addon->print_address($address, $address_id, $address_label, $template_prefix);
}
예제 #4
0
/**
 * @param array $post_data  The same array you would give to wp_insert_post to create your post. No need to set the post
 *                          type, this will automatically be set.
 * @param array $owner      An array containing the owner description: type ('usr', 'grp', 'prj', 'rol', etc.) and IDs
 *                          of corresponding objects
 * @param array $files      An array containing the paths to the files to attache to the post object. Currently we only
 *                          support a single file.
 *
 * @return int¦WP_Error the post ID if the function could insert the post, else, a WP_Error object
 *
 * ´cuar_create_private_file(
 *      array(
 *          'post_title'   => 'Test file',
 *          'post_content' => 'This is the content',
 *          'post_status'  => 'publish'
 *      ),
 *      array(
 *          'type' => 'usr',
 *          'ids'  => array(1)
 *      ),
 *      'files'     => array(
 *          array(
 *            'name'   => 'example.txt',
 *            'path'   => '/absolute/path/to/file/',
 *            'method' => 'noop|copy|move'
 *          ),
 *          ...
 *      )
 * );´
 *
 * IMPORTANT NOTE: The files have to be located in the plugin's FTP upload folder.
 */
function cuar_create_private_file($post_data, $owner, $files)
{
    if (!isset($owner['type']) || !isset($owner['ids']) || empty($owner['ids'])) {
        return new WP_Error(0, 'cuar_create_private_file needs owner data to create the private file');
    }
    // Create the post object
    $post_data['post_type'] = 'cuar_private_file';
    $post_id = wp_insert_post($post_data);
    if (is_wp_error($post_id)) {
        return $post_id;
    }
    // Assign the owner
    /** @var CUAR_PostOwnerAddOn $po_addon */
    $po_addon = cuar_addon('post-owner');
    $po_addon->save_post_owners($post_id, $owner['ids'], $owner['type']);
    // Attach the file
    /** @var CUAR_PrivateFileAddOn $pf_addon */
    $pf_addon = cuar_addon('private-files');
    foreach ($files as $file) {
        $initial_filename = basename($file['name']);
        $filename = apply_filters('cuar/private-content/files/unique-filename?method=server', $initial_filename, $post_id, $file);
        $errors = apply_filters('cuar/private-content/files/on-attach-file?method=server', array(), $pf_addon, $initial_filename, $post_id, $filename, $filename, $file);
        $extra = array('is_protected' => $file['method'] == 'noop' ? 0 : 1, 'abs_path' => $file['method'] == 'noop' ? trailingslashit($file['path']) . $file['name'] : '');
        $pf_addon->add_attached_file($post_id, $filename, $filename, 'server', $extra);
        if (!empty($errors)) {
            wp_delete_post($post_id);
            return new WP_Error('upload_error', implode(', ', $errors));
        }
    }
    return $post_id;
}
/**
 * @param array $post_data The same array you would give to wp_insert_post to create your post. No need to set the post
 *                         type, this will automatically be set.
 * @param array $owner     An array containing the owner description: type ('usr', 'grp', 'prj', 'rol', etc.) and IDs
 *                         of corresponding objects
 * @param array $files     An array containing the paths to the files to attache to the post object. Currently we only
 *                         support a single file.
 *
 * @return int¦WP_Error the post ID if the function could insert the post, else, a WP_Error object
 *
 * ´cuar_create_private_file(
 *      array(
 *          'post_title'   => 'Test file',
 *          'post_content' => 'This is the content',
 *          'post_status'  => 'publish'
 *      ),
 *      array(
 *          'type' => 'usr',
 *          'ids'  => array(1)
 *      ),
 *      array(
 *          '/path/to/file/on/server/the-file.txt'
 *      )
 * );´
 */
function cuar_create_private_file($post_data, $owner, $files)
{
    if (count($files) != 1) {
        return new WP_Error(0, 'cuar_create_private_file only support a single private file');
    }
    if (!isset($owner['type']) || !isset($owner['ids']) || empty($owner['ids'])) {
        return new WP_Error(0, 'cuar_create_private_file needs owner data to create the private file');
    }
    // Create the post object
    $post_data['post_type'] = 'cuar_private_file';
    $post_id = wp_insert_post($post_data);
    if (is_wp_error($post_id)) {
        return $post_id;
    }
    // Assign the owner
    /** @var CUAR_PostOwnerAddOn $po_addon */
    $po_addon = cuar_addon('post-owner');
    $po_addon->save_post_owners($post_id, $owner['ids'], $owner['type']);
    // Attach the file
    /** @var CUAR_PrivateFileAddOn $pf_addon */
    $pf_addon = cuar_addon('private-files');
    foreach ($files as $file) {
        $upload_result = $pf_addon->handle_copy_private_file_from_local_folder($post_id, null, $owner, $file);
        if ($upload_result !== true) {
            wp_delete_post($post_id);
            return new WP_Error('upload_error', $upload_result['error']);
        }
    }
    return $post_id;
}