Download Monitor for Membership 2 Pro

With Membership 2 Pro is there plugin/hook that works as Download Monitor?

I.e. I'd like to for force downloads for protected media files and be able to see download count and by what user?

  • Nastia

    Hello Webprouk , I hope you are doing well today!

    Thank you for your question. I am afraid I am not a code expert so I have flagged a developer here to provide feedback.

    Once there is an update the developer or myself will post back in here.

    Please note that the developer response usually takes more time compare to the staff members. This is because they ae dealing with complex issues on a daily basis.

    Kind regards,
    Nastia

  • Mahlamusa

    Hello Webprouk,

    I hope you are doing great today and thank you for your patience so far. I am sorry for the delayed reply from our end.

    I have looked into the code of the Membership 2 Pro Plugin and found that there is no hook that was built specifically for this function but have found 2 action hooks that can help you achieve exactly what you want.

    There two hooks that run when serving a file as a download, one runs before the file is served and the other runs after the file is served. Here are the hooks:

    ms_rule_media_model_output_file_before //runs before serving file
    ms_rule_media_model_output_file_before //runs after serving file

    These can be used as follows:

    add_action('ms_rule_media_model_output_file_before','monitor_download_function',10,2);
    add_action('ms_rule_media_model_output_file_after','monitor_download_function',10,2);

    Both action hooks give you access to two parameters, the $file and the media addon class. I have written a basic mu plugin that you can modify to achieve your final goal. Copy the following code, paste in a new file and save it as .php file. Upload it to your worpress install '/wp-content/mu-plugins', so you may have something like:

    wp-content/mu-plugins/m2-download-manager.php

    The file should have the following code:

    <?php
    //do_action( 'ms_rule_media_model_output_file_before', $file, $this );
    //do_action( 'ms_rule_media_model_output_file_before', $file, $this );
    
    function monitor_ms2_file_download($file,$this){
    	if (is_file($file)){
    		$mime = wp_check_filetype( $file );
    		if ( empty( $mime['type'] ) && function_exists( 'mime_content_type' ) ) {
    			$mime['type'] = mime_content_type( $file );
    		}
    
    		if ( $mime['type'] ) {
    			$mimetype = $mime['type'];
    		} else {
    			$mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
    		}
    
    		$last_modified = date_i18n( 'D, d M Y H:i:s', filemtime( $file ) );
    		$modified_timestamp = strtotime( $last_modified );
    
    		$user = get_user_by('id',get_current_user_id());
    
    		$user_name = isset($user->login_name)?$user->login_name: "Guest";
    
    		$downloaded_file = array(
    			'user'=>$user_name,
    			'file'=>array(
    				'url'=>$file,
    				'last_modified' => $last_modified,
    				'type' => $mimetype
    			)
    		);
    
    		$files = get_option('ms2_downloaded_files');
    		$files[]=$downloaded_file;
    
    		$files_updated = array_merge($files,$downloaded_file);
    
    		update_option('ms2_downloaded_files',$files_updated);
    
    	}
    }
    
    function ms2_download_monitor_admin_menus(){
    	add_menu_page(
    		__( 'M2 Downloads', 'downloadmonitor' ),
    		__( 'M2 Downloads', 'downloadmonitor' ),
    		'manage_options',
    		'm2-download-monitor',
    		'm2_dm_render_main_admin_page',
    		'dashicons-chart-line',
    		'17.91258'
    	);
    }
    
    function m2_dm_render_main_admin_page(){
    
    	/**$demofiles = array(
    		array(
    			'user'=>"Lindeni",
    			'file'=>array(
    				'url'=>'url to image',
    				'last_modified'=>'2014-11-10',
    				'type'=>'Image/jpeg'
    			)
    		),
    		array('user'=>"Guest",'file'=>array('url'=>'url to image','last_modified'=>'2014-11-10','type'=>'Image/jpeg')),
    		array('user'=>"Samuel",'file'=>array('url'=>'url to image','last_modified'=>'2014-11-10','type'=>'Image/jpeg'))
    	);
    	update_option('ms2_downloaded_files',$demofiles);**/
    
    	$files = get_option('ms2_downloaded_files');
    	?>
    	<h2>Download Monitor</h2>
    	<?php
    	if (count($files)>1){?>
    		<table>
    			<thead><tr><th>Username</th><th>File Info</th></tr></thead>
    			<tbody>
    				<?php
    				foreach ( $files as $file):
    				echo '<tr>
    					<td>'.$file['user'].'<td>
    					<td>'.$file['file']['url'].', Type: '.$file['file']['type'].', Modified: '.$file['file']['last_modified'].
    				'</tr>';
    				endforeach;
    				?>
    			</tbody>
    		</table>
    
    	<?php
    	}else{
    		echo "<p><strong>No files downloaded.</strong></p>";
    	}
    }
    add_action('admin_menu','ms2_download_monitor_admin_menus');
    add_action('ms_rule_media_model_output_file_before','ms2_download_monitor_admin_menus',10,2);

    Once you have downloaded that file, go to your website dashboard and you will notice a new menu item called "M2 Downloads". You should see the download information on this page.

    Please note that the code is not final and may not work at first use, in this case you will have to modify the code yourself and make it do what you want to do.

    I hope this helps. Please let us know if you need any more help.

    Cheers,
    Mahlamusa